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

Posted brucetang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx服务器http重定向到https的正确写法相关的知识,希望对你有一定的参考价值。

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

rewrite ^/(.*)$ http://domain.com/$1 permanent;

或者

rewrite ^ http://domain.com$request_uri? permanent;

 

现在nginx新版本已经换了种写法,上面这些已经不再推荐。

下面是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;

    [....]
}

以上是关于nginx服务器http重定向到https的正确写法的主要内容,如果未能解决你的问题,请参考以下文章