运行 Nginx 并尝试重定向:
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;
}
现在,除了#htaccess 位(从 Apache 的 .htaccess 转换以在 Wordpress 中启用“漂亮链接”)和“返回 301”行之外,现在看起来的配置不是我做的。
如果有区别的话,SSL 证书来自 LetsEncrypt。
我已保存并重新启动了 Nginx。
在 Chrome 中请求该网站时,它告诉我“ERR_TOO_MANY_REDIRECTS”
我做错了什么?我需要添加或删除任何内容吗?
答案1
这是您当前配置的简化版本(减去目前不会执行任何内容操作的所有位),并添加了一些注释,以便您可以看到正在发生的事情。最后两行只是不断将内容发送回开始。因此有很多重定向错误。
server {
listen my_server_IP; #listen on IP x.x.x.x
listen my_server_IP:443 ssl; #listen on IP x.x.x.x on 443
server_name example.com www.example.com; #Of the requests on IP x.x.x.x 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>
}