实现访问http 自动跳转到https

实现效果:

http://www.xiaocantongxue.xyz ⇒ https://www.xiaocantongxue.xyz
xiaocantongxue.xyz ⇒ https://www.xiaocantongxue.xyz

ngx_http_rewrite_module模块:
将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换

一、使用if判断,通过return,将http跳转到https

vim /etc/nginx/conf.d/test.conf 
server {
        listen 443 ssl;
        listen 80;
        server_name www.a.net;
        root /data/site1/;
        ssl_certificate /etc/nginx/ssl/www.a.net.crt;
        ssl_certificate_key /etc/nginx/ssl/www.a.net.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        access_log /var/log/nginx/a.net.log443 main;
        location / {
            if ( $scheme = http ){
                return 301 https://www.a.net/;
                }
        }

}

二、rewrite

server {
        listen 443 ssl;
        listen 80;
        server_name www.a.net;
        root /data/site1/;
        ssl_certificate /etc/nginx/ssl/www.a.net.crt;
        ssl_certificate_key /etc/nginx/ssl/www.a.net.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        access_log /var/log/nginx/a.net.log443 main;
        location / {
        if ( $scheme = http ) {
        rewrite ^/(.*)$ https://www.a.net/$1 redirect;
                }
        }
}

当访问的是http下的任意文件,自动跳转到https下的任意文件


以上来源于这篇教程:
https://www.cnblogs.com/alexlv/p/14845267.html

参考上面的解决方案,我用的是第一种
实现访问http 自动跳转到https_第1张图片

你可能感兴趣的:(https,http,nginx)