Nginx配置http跳转https访问

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
}
上一篇 Nginx配置限制IP访问
下一篇 FastDFS && Nginx实现分布式文件服务器
目录
文章列表
1 Spring Security OAuth2 JWT 认证服务器配置
Spring Security OAuth2 JWT 认证服务器配置
2
ELK部署记录
ELK部署记录
3
Python之dict(或对象)与json之间的互相转化
Python之dict(或对象)与json之间的互相转化
4
设计模式(5)工厂方法模式
设计模式(5)工厂方法模式
5
使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。