Nginx:将httpredirect到https(错误:redirect过多)

运行Nginx并尝试redirect

www到非www

http到https

我意识到有类似的线程,但没有相同的情况。

我已经安装了Webmin / Virtualmin和Fast-CGI。 我在这个服务器上有很多帐号/网站。 对于mysite,这是服务器块:

server { listen my_server_IP; server_name example.com www.example.com; return 301 https://example.com$request_uri; root /home/example/public_html; index index.html index.htm index.php; access_log /var/log/virtualmin/example.com_access_log; error_log /var/log/virtualmin/example.com_error_log; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME /home/example/public_html$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT /home/example/public_html; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param HTTPS $https; location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/php-nginx/148180748420424.sock/socket; } # htaccess location / { try_files $uri $uri/ /index.php?$args; } # listen my_server_IP:443 ssl; ssl_certificate /home/example/ssl.cert; ssl_certificate_key /home/example/ssl.key; } 

现在,看起来像现在这样的configuration,除了#htaccess位(从Apache的.htaccess转换为启用Wordpress中的“漂亮链接”)和“返回301”行之外,我没有做过什么。

SSL证书来自LetsEncrypt,如果这有所作为。

我保存并重新启动了Nginx。

在Chrome中请求网站时,会告诉我“ERR_TOO_MANY_REDIRECTS”

我究竟做错了什么? 我需要添加或删除任何东西吗?

这是你当前configuration的一个简化版本(减去当前不会做任何内容的所有比特),添加一些注释以便你可以看到发生了什么。 最后两行只是把事情重新开始。 因此,许多redirect错误。

 server { listen my_server_IP; #listen on IP xxxx listen my_server_IP:443 ssl; #listen on IP xxxx on 443 server_name example.com www.example.com; #Of the requests on IP xxxx look for those with with one of these headers. send to line below return 301 https://example.com$request_uri; #Send everything to the line above } 

这就是你所需要的,你可以使用if语句,但是现在不要这样做,只要你能清楚地看到发生了什么事情。 它更多的线,但它会工作。

 server { listen my_server_IP; server_name example.com www.example.com; #Listen for non-https requests return 301 https://example.com$request_uri; #Send to the correct https address } server { listen my_server_IP:443 ssl; server_name www.example.com; #Listen for https (www) requests return 301 https://example.com$request_uri; #Send to the correct https ssl_certificate /home/example/ssl.cert; ssl_certificate_key /home/example/ssl.key; } server { listen my_server_IP:443 ssl; server_name example.com; ssl_certificate /home/example/ssl.cert; ssl_certificate_key /home/example/ssl.key; <Rest of your config from above, fastcgi etc> }