Node.js应用子域名迁移后单个页面仍访问旧URL问题排查
Hey there! Let's walk through the most likely reasons why that one page is stuck hitting my_application.com/sites instead of your new subdomain sites.my_application.com, plus how to fix each issue:
1. Hardcoded Old URLs in Static Files
Since your public folder holds static HTML, JS, and CSS, this is the first place to check. That problem page's HTML or associated scripts might have hardcoded absolute URLs pointing to the old domain.
How to fix:
- Navigate to
user/app/publicand open the problematic page's HTML file (e.g.,problem-page.html). - Search for any instances of
https://my_application.com/sitesor just/sitesif you're using root-relative paths that don't account for the subdomain. - Replace them with either the full new domain (
https://sites.my_application.com) or cleaner relative paths (e.g., change/sites/contactto/contactif your subdomain serves as the new root).
Example of what to look for:
Old code:
<script src="https://my_application.com/sites/js/script.js"></script><a href="/sites/checkout">Proceed to Checkout</a>Fixed code:
<script src="/js/script.js"></script><a href="/checkout">Proceed to Checkout</a>- Navigate to
2. Server-Side Routing/Template Hardcoding
If your index.js uses a template engine (EJS, Pug, Handlebars) or directly sends HTML responses, the old URL might be baked into the server-side logic.
How to fix:
- Open
user/app/index.jsand search for routes that render or send the problematic page. - Check for any hardcoded URLs in the response content or template variables.
- Update those references to use the new subdomain or relative paths.
Example server-side fix:
// Old route with hardcoded URL app.get('/problem-page', (req, res) => { res.render('problem-page', { checkoutUrl: 'https://my_application.com/sites/checkout' }); }); // Fixed route using new domain or relative path app.get('/problem-page', (req, res) => { res.render('problem-page', { checkoutUrl: 'https://sites.my_application.com/checkout' // Or use relative path: checkoutUrl: '/checkout' }); });- Open
3. Browser or CDN Caching
Caching is a common culprit for lingering old content. The user's browser might have cached the old page, or if you're using AWS CloudFront (or another CDN), its cache might still serve the outdated version.
- How to fix:
- For end users: Instruct them to perform a hard refresh (Ctrl+Shift+R on Windows/Linux, Cmd+Shift+R on Mac) to bypass browser cache.
- For AWS CloudFront: Go to the CloudFront console, select your distribution, and trigger a full cache invalidation with the path
/*. - Add cache-control headers to your static files in Node.js to prevent long-term caching of dynamic pages:
app.use(express.static('public', { maxAge: '1h', // Shorten for frequently updated pages setHeaders: (res, path) => { if (path.endsWith('.html')) { res.setHeader('Cache-Control', 'no-cache, must-revalidate'); } } }));
4. AWS Configuration Residues
If you're using AWS services like Application Load Balancer (ALB) or Route 53, double-check for any leftover rules pointing to the old URL structure.
- How to fix:
- In Route 53: Ensure your
sites.my_application.comrecord points to the correct target (your EC2 instance or ALB) and there are no leftover redirect rules for the oldmy_application.com/sitespath. - In ALB: Verify that listener rules don't have any redirect actions sending traffic to the old domain path.
- In Route 53: Ensure your
5. Third-Party Scripts or Resources
The problematic page might be loading third-party tools (analytics, payment gateways, etc.) that are configured to use the old URL.
- How to fix:
- Inspect the page using your browser's DevTools (Network tab) to look for requests to
my_application.com/sites. - Check any third-party service dashboards you're using on that page and update their callback URLs or asset paths to the new subdomain.
- Inspect the page using your browser's DevTools (Network tab) to look for requests to
Start with the simplest checks (static files and caching) first—those are usually the fastest fixes. If none of these work, feel free to share more details about the page's structure or any server-side logic tied to it!
内容的提问来源于stack exchange,提问作者monikr_on_east

