Nginx配置http跳转https访问

3 篇文章 0 订阅
1 篇文章 0 订阅

Nginx强制http跳转https访问有以下几个方法

nginx的rewrite方法

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

配置

方法一:

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

方法二:

server{  
   listen 80;
   server_name XXXXX.com; #你的域名
   return 301 https://$server_name$request_uri;      
   location ~ / {
     index index.html index.php index.htm;
   }
}

方法三:

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

nginx的497状态码

497 – normal request was sent to HTTPS  
当前站点只允许HTTPS访问,当使用HTTP访问nginx会报出497错误码
可以使用error_page 把497状态码链接重新定向到HTTPS域名上

server{  
   listen 80;
   server_name XXXXX.com;  ####你的域名   
   error_page 497  https://$host$uri?$args;  
   location ~ / {
     index index.html index.php index.htm; 
   }
}

meta刷新作用将http跳转到HTTPS

index.html

<html>
 <meta http-equiv=”refresh” content=”0;url=https://XXXX.com/”>
 </html>

nginx配置

server{  
   listen 80;
   server_name XXXXX.com; ##你的域名 
   location ~ / {
     root /var/www/test/;
     index index.html index.php index.htm;
   }
   error_page 404 https://xxxx.com
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值