我知道这个问题已经被无数次的问到了。 仍然不能让我的工作与我目前看到的答案。
我试图强制redirecthttp到https与nginx。 当我访问https // subdomain.example.com时,一切正常,但访问http://subdomain.example.com给了我
"This Webpage has a redirect loop"
我试过了
rewrite ^(.*) https://$host$1 permanent;
和
return 301 https://www.mydomain.com$request_uri;
试着
proxy_set_header X-Forwarded-Proto $scheme;
没有解决这个问题。 请问我该如何解决这个无限循环问题?
这是我的nginx.conf
upstream unicorn { server unix:/tmp/unicorn.example.sock fail_timeout=0; } server { server_name subdomain.example.com; listen 80; return 301 https://$host$request_uri; root /home/deploy/apps/example/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; proxy_set_header X-Forwarded-Proto $scheme; } try_files $uri/index.html $uri @unicorn; location @unicorn { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; } server { server_name subdomain.example.com; listen 443; root /home/deploy/apps/example/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; ssl on; ssl_certificate /home/deploy/apps/example/shared/ssl_cert.crt; ssl_certificate_key /home/deploy/apps/example/shared/ssl_private_key.key; }#
这可能是由于您的代理目标,因为您禁用了proxy_redirect 。
另外,为什么不用HTTPS提供任何东西?
将这两者混合在一起很可能会将访问者与浏览器警告混为一谈。
upstream unicorn { server unix:/tmp/unicorn.example.sock fail_timeout=0; } server { server_name _; listen 80 default_server; return 301 https://subdomain.example.com$request_uri; } server { server_name subdomain.example.com; listen 443 ssl; ssl_certificate /home/deploy/apps/example/shared/ssl_cert.crt; ssl_certificate_key /home/deploy/apps/example/shared/ssl_private_key.key; root /home/deploy/apps/example/current/public; client_max_body_size 4G; error_page 500 502 503 504 /500.html; keepalive_timeout 10; location /assets { expires max; gzip_static on; add_header Cache-Control public; } location / { try_files $uri/index.html $uri @unicorn; } location @unicorn { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://unicorn; } }
这应该这样做,除非你的代理目标是做了奇怪的东西,缺lessX-Forwarded-Proto头,在这种情况下,更改回退到这个,并希望它正确处理redirect:
location @unicorn { proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://unicorn; proxy_redirect off; }