CentOS7下Nginx代理Node.js出现空白页或ERR_TOO_MANY_REDIRECTS求助
Hey there, let's tackle those frustrating intermittent issues with your site—random blank pages and ERR_TOO_MANY_REDIRECTS errors are the worst, especially when most of the time things work fine. Given your Node/Express + Nginx + CentOS7 + Namecheap setup, here's where to start digging:
1. Fix Nginx Redirect Loops (First Priority for ERR_TOO_MANY_REDIRECTS)
Redirect loops almost always stem from conflicting HTTPS enforcement between Nginx and your Express app. Here's what to check:
- Avoid double HTTPS enforcement: If Nginx is handling HTTPS termination (with your SSL cert), don't also force HTTPS redirects in your Express app—this creates a loop. Pick one layer to handle the redirect.
- Pass protocol context to Express: Add these lines to your Nginx location block so Express knows the user is accessing via HTTPS, preventing unnecessary internal redirects:
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $remote_addr; - Clean up server blocks: Make sure you don't have duplicate or conflicting server blocks listening on ports 80/443. Here's a reliable baseline config to reference:
# HTTP server (redirect all traffic to HTTPS) server { listen 80; server_name sungryeol.com www.sungryeol.com; return 301 https://$server_name$request_uri; } # HTTPS server (reverse proxy to Express) server { listen 443 ssl http2; server_name sungryeol.com www.sungryeol.com; # Insert your SSL cert paths here (e.g., from Certbot) ssl_certificate /path/to/fullchain.pem; ssl_certificate_key /path/to/privkey.pem; location / { proxy_pass http://localhost:3000; # Match your Express app's port proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; } } - Validate and reload Nginx: After editing, run
nginx -tto check for syntax errors, thensystemctl reload nginxto apply changes without downtime.
2. Diagnose Blank Pages
Blank pages (no HTML content) typically point to one of these issues:
- Express isn't returning content: Check your app's logs for silent failures. If you use PM2 to manage Express, run
pm2 logs your-app-nameto see real-time errors—look for unhandled exceptions, database timeouts, or misconfigured routes that might send empty responses. - Nginx can't reach Express: Tail Nginx's error log with
tail -f /var/log/nginx/error.logto spot502 Bad Gatewayor504 Gateway Timeouterrors. These mean Nginx couldn't connect to your Express server—maybe it crashed, or the port in your config is wrong. - Server resource exhaustion: Use
toporhtopon your CentOS server to check CPU/memory usage. If your server hits 100% CPU or runs out of RAM, Express might freeze temporarily and return empty pages.
3. Check Namecheap DNS Configuration
Occasional access blips can tie to DNS inconsistencies:
- Verify A/AAAA records: Double-check that your domain's A record points directly to your Vultr IP, with no duplicate or stale records in Namecheap's DNS settings.
- Lower TTL for debugging: If your DNS TTL is set to a high value (like 86400 seconds), old records might be cached by ISPs. Temporarily set TTL to 300 (5 minutes) to speed up propagation while you test fixes.
- Stick to one DNS provider: Ensure you're only using Namecheap's DNS servers—mixing with Vultr's DNS or another service can cause inconsistent domain resolution.
4. Quick Edge Case Checks
- Flush local DNS: On your test device, clear cached DNS records (e.g.,
ipconfig /flushdnson Windows,sudo dscacheutil -flushcacheon macOS) to rule out stale local data. - Test both domain variants: Make sure both
sungryeol.comandwww.sungryeol.comare configured correctly in Nginx and DNS—missing one can cause partial access issues. - Check firewall rules: Run
firewall-cmd --list-allon CentOS to confirm ports 80, 443, and your Express app's port are allowed through firewalld.
Start with the Nginx redirect checks first—those are the most likely culprit for the loop errors. Once that's sorted, dig into the logs for the blank page issues. If you spot specific error messages, you can narrow down the fix even faster!
内容的提问来源于stack exchange,提问作者Sungryeol Park

