咨询热线:4006-75-4006

售前:9:00-23:30    备案:9:00-18:00    技术:7*24h

Nginx自动跳转到HTTPS

2017-11-16 18:19:59 7790次

方法一:

        rewrite ^(.*)$ https://$host$1 permanent;

方法二:

适用于 80端口和443 配置在同一个 server{}内

        if ($server_port = 80) {
            rewrite ^(.*)$ https://$host$1 permanent;
        }

其他情况, 站点绑定很多域名,只让某个域名跳转:

        set $redirect_https 1;
        if ($server_port = 80) {
            set $redirect_https "${redirect_https}2";
        }
        if ($http_host = 'www.landui.com') {
            set $redirect_https "${redirect_https}3";
        }
        if ($http_host = 'niaoyundemo.com') {
            set $redirect_https "${redirect_https}3";
        }
        if ($redirect_https = "123") {
            rewrite ^(.*)$ https://$host$1 permanent;
        }

配置示例:

        server {
            listen       80;
            server_name  www.landui.com;
            rewrite ^ https://$http_host$request_uri? permanent;
        }
        server {
            listen      443;
            ssl on;
            ssl_certificate /etc/ssl/cacert.pem;
            ssl_certificate_key /etc/ssl/privkey.pem;
            server_name www.landui.com;  
            server_tokens off;
            location / {
                fastcgi_param   HTTPS               on;
                fastcgi_param   HTTP_SCHEME         https;
            }
        }


首页
最新活动
个人中心