Fork me on GitHub

实现访问http 自动跳转到https

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下的任意文件

posted @ 2021-06-03 17:43  Alex-Lzy  阅读(2035)  评论(1编辑  收藏  举报