一招搞定linux上Nginx配置http强制跳转https

记录分享一下Nginx配置怎么http强制跳转到https

Nginx配置上跳转https的方法有好几种:

使用rewrite指令方法

使用return指令方法

使用error_page指令方法

我们这里讲的是比较简单的一招,别管是什么招,一万种解决问题的方法你会一种就可以了

———————————————————————————————————————————————–

修改Nginx配置文件

先找到conf文件修改,这里用到的是一键LNMP安装的环境位置是   /usr/local/nginx/conf/vhost

 

进入nginx目录

cd /usr/local/nginx/conf/vhost

查看网站nginx配置文件

ls

修改conf文件

vi 域名.com.conf

使用一段https强制跳转脚本插入到.conf文件中,下方是代码复制即可

if ($ssl_protocol = “”) { return 301 https://$host$request_uri; }

示例

root@fffx:/usr/local/nginx/conf/vhost# vi 域名.com.conf 
server
    {
        listen 80;
        #listen [::]:80;
        server_name 域名.com ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/域名.com;

        if ($ssl_protocol = "") { return 301 https://$host$request_uri; }         #主要是这一条代码复制插入到你配置文件的位置即可

        include rewrite/wordpress.conf;
        error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }
阅读剩余
THE END