Nginx无法正确重定向至指定路径问题求助
Hey there, let's figure out why your Nginx redirects aren't working as expected. Looking at your config, I can spot a few potential issues and fixes to try:
location / Configuration Your default location / block is cut off, which will definitely cause unexpected behavior for requests that don't match /auth-foo. Here's a complete, basic setup you can adapt for your backend:
location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://your-backend-ip:your-port; # Replace with your actual backend address # Fix backend redirects that might point to internal IPs proxy_redirect off; proxy_set_header X-Forwarded-Proto $scheme; }
/auth-foo proxy_pass Right now, your proxy_pass http://192.168.52.89:2085; doesn't have a trailing slash. This means Nginx will pass the full /auth-foo path to your backend. For example, a request to https://192.168.52.89/auth-foo/login gets sent to http://192.168.52.89:2085/auth-foo/login.
If your backend expects the path without /auth-foo (like just /login), add the trailing slash:
location /auth-foo { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass_header Set-Cookie; proxy_pass http://192.168.52.89:2085/; # Notice the trailing slash here }
Your config includes deprecated TLS versions (TLSv1 and TLSv1.1), which are insecure and might cause compatibility issues with modern clients. Update this line to use only secure protocols:
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; # Add this to enforce strong cipher suites
- Validate your config: Run
sudo nginx -tto check for syntax errors. It'll tell you exactly where things are broken if there's an issue. - Check error logs: Look at
/var/log/nginx/error.log(default path) for clues about failed redirects or proxy issues. - Test with curl: Use
curl -v https://192.168.52.89/auth-footo see the full request/response chain and confirm where the redirect is going wrong.
内容的提问来源于stack exchange,提问作者Ayo K

