nginx服务器http重定向到https的正确写法

880 views次阅读
没有评论

把http重定向到https使用了nginx的重定向命令。那么应该如何写重定向?之前老版本的nginx可能使用了以下类似的格式。

rewrite ^/(.*)$ http://domain.com/$1 permanent;
或者
rewrite ^ http://domain.com$request_uri? permanent;

现在nginx新版本已经换了种写法,上面这些已经不再推荐。现在网上可能还有很多文章写的是第一种。比如博主的这篇文章《LNMP环境实现301重定向的方法》。

下面是nginx http页面重定向到https页面最新支持的写法:

server {
       listen         80;
       server_name    my.domain.com;
       return         301 https://$server_name$request_uri;
}

server {
       listen         443 ssl;
       server_name    my.domain.com;

       [....]
}

 

参考网址:

http://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

正文完
微信搜一搜“奇悦电脑科技”或扫描二维码关注我们
post-qrcode
 0
章郎虫
版权声明:本站原创文章,由 章郎虫 于2014-11-26发表,共计640字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。