Nginx 将所有内容重定向到 www https
Nginx redirect everything to www https
我想将所有内容重定向到 https://www
这是我的配置块,我尝试将非 www 重定向到 https 但不起作用。
server {
listen 80;
server_name example.com
return 301 https://www.$server_name$request_uri;
}
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
location /auth {
...
}
location / {
...
}
ssl_certificate /root/certs/fullchain.pem;
ssl_certificate_key /root/certs/privkey.pem;
}
http://example.com 给出 404 错误
https://example.com 不会重定向到 https://www
http://www.example.com 重定向到 https://www
https://www.example 照原样工作
我认为你也应该为 https 服务器指定服务器名称:
server {
server_name www.example.com
listen 443 ssl;
...
ssl_certificate /root/certs/fullchain.pem;
ssl_certificate_key /root/certs/privkey.pem;
}
您还可以通过将它们合并为一个来简化 http 服务器:
server {
listen 80;
server_name example.com www.example.com
return 301 https://www.$host$request_uri;
}
server {
listen 80;
server_name example.com www.example.com
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com
ssl_certificate Certificate/For/example.com/fullchain
ssl_certificate_key Key/For/example.com/
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name www.example.com
ssl_certificate Certificate/For/www.example.com/fullchain
ssl_certificate_key Key/For/www.example.com/key
//do whatever you want with your https://www.example.com connection
}
请记住同时拥有两个证书,一个用于 www,一个不用于 www,就像您将示例重定向到没有证书的 www 一样,这是一个不安全的步骤,并且会显示错误。
希望我帮到了你
我想将所有内容重定向到 https://www
这是我的配置块,我尝试将非 www 重定向到 https 但不起作用。
server {
listen 80;
server_name example.com
return 301 https://www.$server_name$request_uri;
}
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
location /auth {
...
}
location / {
...
}
ssl_certificate /root/certs/fullchain.pem;
ssl_certificate_key /root/certs/privkey.pem;
}
http://example.com 给出 404 错误
https://example.com 不会重定向到 https://www
http://www.example.com 重定向到 https://www
https://www.example 照原样工作
我认为你也应该为 https 服务器指定服务器名称:
server {
server_name www.example.com
listen 443 ssl;
...
ssl_certificate /root/certs/fullchain.pem;
ssl_certificate_key /root/certs/privkey.pem;
}
您还可以通过将它们合并为一个来简化 http 服务器:
server {
listen 80;
server_name example.com www.example.com
return 301 https://www.$host$request_uri;
}
server {
listen 80;
server_name example.com www.example.com
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com
ssl_certificate Certificate/For/example.com/fullchain
ssl_certificate_key Key/For/example.com/
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name www.example.com
ssl_certificate Certificate/For/www.example.com/fullchain
ssl_certificate_key Key/For/www.example.com/key
//do whatever you want with your https://www.example.com connection
}
请记住同时拥有两个证书,一个用于 www,一个不用于 www,就像您将示例重定向到没有证书的 www 一样,这是一个不安全的步骤,并且会显示错误。
希望我帮到了你