配置证书后,配置默认请求https

很多站长在部署了https后,默认访问的网站仍然是http,需要手动去访问https才行。

今天小白在这个告诉大家一个简单的代码,在nginx站点配置中加一段简单的判断跳转就可以实现,不需要使用伪静态跳转。

在nginx的站点配置中加上一段

if ($server_port !~ 443){
            rewrite ^/.*$ https://$host$uri;
        }
在配置文件中

server
    {
        listen 80;
        listen 443 ssl;
        server_name 123.123.123.123 123.56.161.140;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/123.56.161.140;
        #error_page 404/404.html;
        ssl_certificate          key/123.56.161.140/key.csr;
        ssl_certificate_key  key/123.56.161.140/key.key;
    if ($server_port !~ 443){
            rewrite ^/.*$ https://$host$uri;
        }
        error_page 497  https://$host$uri;

        error_page 404 /404.html;
        error_page 502 /502.html;

        include enable-php-71.conf;
        include rewrite/123.56.161.140.conf;
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
            access_log off; 
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
            access_log off; 
        }
        access_log  /www/wwwlogs/123.56.161.140.log;
    }
这样就可以判断当访问不是80端口,自动加上https。
posted @ 2020-03-27 10:39  祈愿仙帝  阅读(620)  评论(0编辑  收藏  举报