我试图实现以下重定向
http://domain.com -> https://www.domain.com
http://www.domain.com -> https://www.domain.com
https://domain.com -> https://www.domain.com
所以基本上http -> https
是non-www -> www
结合起来的。
这是我的nginx.conf
:
server {
listen 80;
server_name domain.com www.domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.domain.com;
ssl_certificate /data/unified.crt;
ssl_certificate_key /data/my-private-decrypted.key;
location / {
proxy_pass http://domain.com:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
有了它,以下重定向就会失败
https://domain.com -> https://www.domain.com
我做错了什么以及如何添加对该重定向的支持?
答案1
我必须添加另一个服务器块https://domain.com
server {
listen 80;
server_name domain.com www.domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
ssl_certificate /data/unified.crt;
ssl_certificate_key /data/my-private-decrypted.key;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.domain.com;
ssl_certificate /data/unified.crt;
ssl_certificate_key /data/my-private-decrypted.key;
location / {
proxy_pass http://domain.com:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}