NGINX设置http强制跳转https

Posted MargoHu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NGINX设置http强制跳转https相关的知识,希望对你有一定的参考价值。

nginx设置http强制跳转https

 

 

方法一:

打开nginx.conf或虚拟机配置文件

server {
    listen 80;
    server_name dev.wangshibo.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    if ($host = "dev.wangshibo.com") {
       rewrite ^/(.*)$ http://dev.wangshibo.com permanent;
    }
 
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
 
 

 

方法二:这种方式适用于多域名的时候,即访问wangshibo.com的http也会强制跳转到https://dev.wangshibo.com上面

打开nginx.conf或虚拟机配置文件

server {
    listen 80;
    server_name dev.wangshibo.com wangshibo.com *.wangshibo.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    if ($host ~* "^wangshibo.com$") {
    rewrite ^/(.*)$ https://dev.wangshibo.com/ permanent;
    }
  
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
 
 
 
资料来源:https://www.cnblogs.com/kevingrace/p/6187072.html
 
 
 

以上是关于NGINX设置http强制跳转https的主要内容,如果未能解决你的问题,请参考以下文章

宝塔Linux nginx http强制跳转https几种设置方法

Nginx http强转https

Nginx强制http跳转https访问

Nginx强制跳转Https配置

nginx http301强制跳转https域名跳转到带www

nginx http强制跳转https