tim*_*son 2 rewrite configuration nginx redirect
我查看了许多SE 线程并进行了各种谷歌搜索,但无法弄清楚为什么我无法重定向www.mysite.com
到mysite.com
我的 nginx 服务器。
第一个服务器块按照您的预期执行http://mysite.info
->https://mysite.info
重定向。所以我不确定为什么第二个服务器块对www.mysite.info
->没有做同样的事情mysite.info
。
这是我的 nginx.conf 文件的相关部分:
server {
server_name mysite.info;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
server_name www.mysite.info;
rewrite ^ https://mysite.info$request_uri? permanent;
}
server {
listen 443;
ssl on;
server_name mysite.info;
# other directives, handling PHP, etc.
}
Run Code Online (Sandbox Code Playgroud)
关于出了什么问题的任何想法?
您要重定向到$server_name
,这是www.mysite.info
第二server
块-使所有这一切做的是重定向到HTTPS,不改变主机。
rewrite ^ https://mysite.info$request_uri? permanent;
Run Code Online (Sandbox Code Playgroud)
这将处理主机的更改以及对 HTTPS 的更改。
如果您希望重定向与协议无关,则更好的方法是:
rewrite ^ $scheme://mysite.info$request_uri? permanent;
Run Code Online (Sandbox Code Playgroud)
在最新版本的 nginx 上,这也有效(并且应该快一点):
return 301 $scheme://mysite.info$request_uri;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3531 次 |
最近记录: |