nginx将所有http请求重定向至https

断了今生、忘了曾经 提交于 2019-12-06 10:21:48

转自:https://www.cnblogs.com/lpjnote/p/10759847.html

 

可以把所有的HTTP请求通过rewrite重写到HTTPS上

配置

方法一

 server{  
    listen 80;
    server_name XXXXX.com;  //你的域名    if($ssl_protocol = "") {rewrite ^(.*)$  https://XXXXXX.com permanent;}
    location ~ / {
    index index.html index.php index.htm;
   }
 }
 

方法二

 server{  
    listen 80;
    server_name XXXXX.com;  //你的域名
    if($ssl_protocol = ""){ return 301 https://$server_name$request_uri;}      
    location ~ / {
    index index.html index.php index.htm;
   }
 }

方法三

 server{  
    listen 80;
    server_name XXXXX.com;  //你的域名
    if($ssl_protocol = ""){rewrite ^(.*)$  https://$host$1 permanent;}      
    location ~ / {
    index index.html index.php index.htm;
  }
}

 

判断参数ssl_protocol,只要不是https请求的该参数都为空

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!