You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx中www域名跳转至根域名异常问题排查

Troubleshooting: Nginx Shows Default Page When Redirecting www to Non-www (Works with curl but not Browser)

Hey there, let's break down why this is happening—curl working but browsers showing the default Nginx page is a common gotcha, usually tied to caching or configuration priority. Here's what to check step by step:

1. Browser Cache is Almost Certainly the Culprit First

301 redirects are permanently cached by browsers—even if you fixed your config later, your browser might still be clinging to an old response. Test this first:

  • Open an incognito/private window and visit www.example.com—if it redirects correctly here, the issue is just your regular browser cache. Clear it (or use Ctrl+Shift+R to force a hard refresh) and you should be good.

2. Check Nginx Server Block Priority

Nginx matches server blocks in the order they're listed in your config files. If your www.example.com redirect block comes after the default Nginx server block (the one with server_name _ or no explicit server_name), Nginx will hit the default block first and serve that page instead of redirecting.

  • Fix this: Move your redirect server block to the top of your Nginx config (or the included config file) so it's the first block Nginx checks.

3. Double-Check Your Nginx Config is Applied

Even if curl works, it's worth verifying you reloaded Nginx after making changes:

  • Run sudo nginx -t to confirm your config has no syntax errors.
  • Run sudo systemctl reload nginx to apply any changes (restarting works too, but reload is gentler).

4. Don't Forget HTTPS Configuration

You mentioned HTTPS doesn't work for the redirect—browsers often default to HTTPS now, so if you don't have a server block handling www.example.com over port 443, Nginx will fall back to the default HTTPS block (or the default page if you don't have one set up).

  • Add an HTTPS redirect block for www:
server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /path/to/your/fullchain.pem;
    ssl_certificate_key /path/to/your/privkey.pem;
    return 301 $scheme://example.com$request_uri;
}
  • Alternatively, if you use a tool like Certbot, it might have created a default HTTPS block that's catching these requests—make sure your www redirect block comes before that too.

5. Verify DNS (Just to Rule It Out)

While curl working makes this unlikely, it's worth confirming your local DNS isn't pointing to an old IP:

  • Run nslookup www.example.com or dig www.example.com on your local machine—ensure the IP matches your DigitalOcean Droplet.
  • If it doesn't, flush your local DNS cache (Windows: ipconfig /flushdns, Mac: sudo dscacheutil -flushcache, Linux varies by distro).

Start with the browser cache and server block order—those are the most frequent fixes for this exact issue.

内容的提问来源于stack exchange,提问作者kidman01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:52:04