Jon*_*nny 1 nginx 301-redirect
我的 nginx 文件没有文件失败,我不确定如何。我已经成功地从 http 重定向到 https 很好。但我无法让 www 重定向到非 www 版本。我究竟做错了什么?
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
}
# added for let's encrypt
location /.well-known/ {
root /home/sammy/myproject;
allow all;
}
}
server {
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myproject/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
}
# added for let's encrypt
location /.well-known/ {
root /home/sammy/myproject;
allow all;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了这个额外的块:
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
...
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。我想也许我应该改变它来监听 443,但不确定这会如何影响 ssl 服务器块和 default_server 指令?
该$server_name
是指你在虚拟主机块中定义的服务器名称。因此,您的附加块会导致重定向循环重定向回自身。
您必须在那里使用文字域名而不是变量。
对于带有 的 SSL 域www
,您必须添加listen 443 ssl;
块和证书值。
所以,这应该是你的第三个块:
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4935 次 |
最近记录: |