欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Nginx配置http跳转https访问 有大用

Nginx配置http跳转https访问

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

nginx的rewrite方法

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

配置

方法一

复制代码
1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名
4    rewrite ^(.*)$  https://XXXXXX.com permanent;      
5    location ~ / {
6    index index.html index.php index.htm;
7 }
8 }
复制代码

方法二

复制代码
1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名
4    return 301 https://$server_name$request_uri;      
5    location ~ / {
6    index index.html index.php index.htm;
7 }
8 }
复制代码

方法三

复制代码
1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名
4    rewrite ^(.*)$  https://$host$1 permanent;      
5    location ~ / {
6    index index.html index.php index.htm;
7 }
8 }
复制代码

nginx的497状态码

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

复制代码
1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名   
4    error_page 497  https://$host$uri?$args;  
5    location ~ / {
6    index index.html index.php index.htm; 
7 }
8 }
复制代码

meta刷新作用将http跳转到HTTPS

index.html

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

nginx配置

复制代码
1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名   
4    location ~ / {
5    root /var/www/test/;  
6    index index.html index.php index.htm;
7    }
8    error_page 404 https://xxxx.com
9 }
复制代码

 

分类: Nginx
标签: Nginx



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

普通分类: