Nginx強(qiáng)制跳轉(zhuǎn)Https

最近公司要上線HTTPS,需要把之前的HTTP訪問強(qiáng)制跳轉(zhuǎn)到HTTPS。

Nginx安裝注意事項(xiàng)

安裝的時(shí)候需要注意加上 --with-http_ssl_module,因?yàn)閔ttp_ssl_module不屬于Nginx的基本模塊。

1.配置
./configure 
--user=www 
--group=www 
--prefix=/usr/local/nginx 
--with-http_stub_status_module 
--with-http_ssl_module
2.編譯安裝
make && make install

配置SSL證書

如下兩個(gè)證書文件
ssl.crt
ssl.key

配置存放路徑為/usr/local/nginx/cert/

server {
          listen 443;
          server_name dev.wangsl.com;
          root /var/www/XXX/public;
 
          ssl on;
          ssl_certificate /usr/local/nginx/cert/ssl.crt;
          ssl_certificate_key /usr/local/nginx/cert/ssl.key;
          ssl_session_timeout 5m;
          ssl_protocols SSLv2 SSLv3 TLSv1;
          ssl_ciphers HIGH:!aNULL:!MD5;               //或者是ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
          ssl_prefer_server_ciphers on;
 
          access_log /var/www/vhosts/www.wangsl.com/logs/clickstream_ssl.log main;
          error_log /var/www/vhosts/www.wangsl.com/logs/clickstream_error_ssl.log;
 
         if ($remote_addr !~ ^(124.165.97.144|133.110.186.128|133.110.186.88)) {           //對(duì)訪問的來源ip做白名單限制
                rewrite ^.*$ /maintence.php last;
         }
         
         location ~ \.php$ {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_read_timeout 300;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
             #include fastcgi_params;
             include fastcgi.conf;
         }
}

Http訪問強(qiáng)制跳轉(zhuǎn)到Https的幾種方式:

一、采用nginx的rewrite方法

1) 下面是將所有的http請(qǐng)求通過rewrite重寫到https上。
    例如將所有的dev.wangsl.com域名的http訪問強(qiáng)制跳轉(zhuǎn)到https。
    下面配置均可以實(shí)現(xiàn):
 
配置1:
server {
    listen 80;
    server_name dev.wangsl.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    rewrite ^(.*)$  https://$host$1 permanent;        //這是ngixn早前的寫法,現(xiàn)在還可以使用。
  
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
 
================================================================
上面的跳轉(zhuǎn)配置rewrite ^(.*)$  https://$host$1 permanent;
也可以改為下面
rewrite ^/(.*)$ http://dev.wangsl.com/$1 permanent;
或者
rewrite ^ http://dev.wangsl.com$request_uri? permanent;
================================================================
 
配置2:
server {
    listen 80;
    server_name dev.wangsl.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
 
    return      301 https://$server_name$request_uri;      //這是nginx最新支持的寫法
  
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
 
 
配置3:這種方式適用于多域名的時(shí)候,即訪問wangsl.com的http也會(huì)強(qiáng)制跳轉(zhuǎn)到https://dev.wangsl.com上面
server {
    listen 80;
    server_name dev.wangsl.com wangsl.com *.wangsl.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    if ($host ~* "^wangsl.com$") {
    rewrite ^/(.*)$ https://dev.wangsl.com/ permanent;
    }
  
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
 
 
配置4:下面是最簡(jiǎn)單的一種配置
server {
    listen 80;
    server_name dev.wangsl.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    if ($host = "dev.wangsl.com") {
       rewrite ^/(.*)$ http://dev.wangsl.com permanent;
    }
 
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }

二、采用nginx的497狀態(tài)碼

497 - normal request was sent to HTTPS 
解釋:當(dāng)網(wǎng)站只允許https訪問時(shí),當(dāng)用http訪問時(shí)nginx會(huì)報(bào)出497錯(cuò)誤碼
  
思路:
利用error_page命令將497狀態(tài)碼的鏈接重定向到https://dev.wangsl.com這個(gè)域名上
 
配置實(shí)例:
如下訪問dev.wangsl.com或者wangsl.com的http都會(huì)被強(qiáng)制跳轉(zhuǎn)到https
server {
    listen 80;
    server_name dev.wangsl.com wangsl.com *.wangsl.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    error_page 497  https://$host$uri?$args; 
  
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
 
 
也可以將80和443的配置放在一起:
server { 
    listen       127.0.0.1:443;  #ssl端口 
    listen       127.0.0.1:80;   #用戶習(xí)慣用http訪問,加上80,后面通過497狀態(tài)碼讓它自動(dòng)跳到443端口 
    server_name  dev.wangsl.com; 
    #為一個(gè)server{......}開啟ssl支持 
    ssl                  on; 
    #指定PEM格式的證書文件  
    ssl_certificate      /etc/nginx/wangsl.pem;  
    #指定PEM格式的私鑰文件 
    ssl_certificate_key  /etc/nginx/wangsl.key; 
       
    #讓http請(qǐng)求重定向到https請(qǐng)求  
    error_page 497  https://$host$uri?$args; 
 
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }

三、利用meta的刷新作用將http跳轉(zhuǎn)到https

上述的方法均會(huì)耗費(fèi)服務(wù)器的資源,可以借鑒百度使用的方法:巧妙的利用meta的刷新作用,將http跳轉(zhuǎn)到https
可以基于http://dev.wangsl.com的虛擬主機(jī)路徑下寫一個(gè)index.html,內(nèi)容就是http向https的跳轉(zhuǎn)
 
將下面的內(nèi)容追加到index.html首頁文件內(nèi)
[root@localhost ~]# cat /var/www/html/8080/index.html
<html> 
<meta http-equiv="refresh" content="0;url=https://dev.wangsl.com/"> 
</html>
 
[root@localhost ~]# cat /usr/local/nginx/conf/vhosts/test.conf
server {
    listen 80;
    server_name dev.wangsl.com wangsl.com *.wangsl.com;
    index index.html index.php index.htm;
   
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
     
    #將404的頁面重定向到https的首頁 
    error_page  404 https://dev.wangsl.com/;  
  
    location ~ / {
    root /var/www/html/8080;         
    index index.html index.php index.htm;
    }
    }

四、通過proxy_redirec方式

解決辦法:
# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容