Nginx 配置 http 強制跳轉到 https


 

個人真實配置

      架構:Nginx 反向代理 + Nginx 前端(LNMP)

      在 Nginx 反向代理的 虛擬機主機配置文件中,作如下配置:

upstream ilexa_cn {
     server 192.168.1.100:80;
     keepalive 50;
}


server {
   listen 80;
   server_name ilexa.cn;
   return 301 https://$server_name$request_uri;
}


server {
    listen 443 ssl;
    server_name ilexa.cn;
    access_log  logs/ilexa_cn.log  main;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header  Connection "keep-alive";
    ssl on;
    ssl_session_timeout  5m;

    ssl_certificate      /etc/nginx/pki/ilexa_cn_bundle.crt;
    ssl_certificate_key  /etc/nginx/pki/ilexa_cn.key;

    location / {
        proxy_pass http://ilexa_cn;
        proxy_set_header Host $host;
        # needed for HTTPS
        proxy_set_header X_FORWARDED_PROTO https;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    error_page 497 https://$host$uri?$args;
}

 

 

【網絡資料】Nginx 配置 http 強制跳轉到 https 

 Nginx 的 return 301 跳轉

      項目的虛擬主機配置文件:

#先監聽80端口,為http請求,然后強制轉發到https監聽的443端口上面
server {
   listen       80;
   root         /var/www/html/ilexa/;
   server_name  ilexa.cn;
   return  301  https://$server_name$request_uri;
}

#監聽443端口,https請求
server {
   listen        443 ssl;
   root          /var/www/html/ilexa/;
   index         index.php index.html index.htm;
   server_name   ilexa.cn;

   ssl  on;
   ssl_certificate        /etc/nginx/pki/ilexa_cn_bundle.crt;
   ssl_certificate_key    /etc/nginx/pki/ilexa_cn.key;

   ssl_session_timeout 5m;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #按照這個協議配置
   ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;  #按照這個套件配置
   ssl_prefer_server_ciphers on;
  
   location ~ \.php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_param HTTPS $https if_not_empty; # 網上資料有的這行也不增加 include fastcgi_params; } }

 

 

 

Nginx 的 Rewrite 方法

       將所有的 http 請求通過 rewrite 重寫到 https 上即可

 

Nginx前端的配置:

#先監聽80端口,為http請求,然后強制轉發到https監聽的443端口上面
server {
   listen       80;
   root         /var/www/html/ilexa/;
   server_name  ilexa.cn;
   rewrite ^(.*) https://$server_name$1 permanent;
}

#監聽443端口,https請求
server {
   listen                 443 ssl;
   root                   /var/www/html/ilexa/;
   server_name            ilexa.cn;
   ssl_certificate        /etc/nginx/pki/ilexa_cn_bundle.crt;
   ssl_certificate_key    /etc/nginx/pki/ilexa_cn.key;
  
   location ~ \.php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
   }
}

 

      搭建此虛擬主機完成后,就可以將 http://ilexa.cn 的請求全部重寫到 https://ilexa.cn 上了

 

 

Nginx 的 497 狀態碼

497 - normal request was sent to HTTPS  

       解釋:當此虛擬站點只允許https訪問時,當用 http 訪問時 nginx 會報出497錯誤碼

 

思路:

       利用error_page命令將497狀態碼的鏈接重定向到 https://ilexa.cn 這個域名上

 

配置:

server {  
    listen 192.168.1.100:443;  #ssl端口  
    listen 192.168.1.100:80;   #用戶習慣用http訪問,加上80,后面通過497狀態碼讓它自動跳到443端口  
    server_name  ilexa.cn;  
    
    ssl  on;      #為一個server{......}開啟ssl支持  
     
    ssl_certificate      /etc/nginx/ilexa_cn.crt;   #指定crt格式的證書文件  
    ssl_certificate_key  /etc/nginx/ilexa_cn.key;   #指定crt格式的私鑰文件  #讓http請求重定向到https請求 
    error_page 497  https://$host$uri?$args;  
} 

 

 

利用 meta 的刷新作用(index.html首頁跳轉)

       以上兩種方法均會耗費服務器的資源,我們用 curl 訪問baidu.com試一下,看百度的公司是如何實現 baidu.com 向 www.baidu.com 的跳轉

       可以看到百度很巧妙的利用 meta 的刷新作用,將 baidu.com跳轉到www.baidu.com.因此我們可以基於 http://ilexa.cn 的虛擬主機路徑下也寫一個index.html,內容就是http向https的跳轉

 

index.html 文件內容:

<html>  
    <meta http-equiv="refresh" content="0;url=https://ilexa.cn/">  
</html>  

 

nginx 虛擬主機配置:

server {  
    listen 192.168.1.100:80;  
    server_name ilexa.cn;  
      
    location / {  
        root /srv/www/ilexa/;     #index.html放在虛擬主機監聽的根目錄下  
    }  

    error_page  404 https://ilexa.cn/;   #將404的頁面重定向到https的首頁  
}  

 

 

Aapache 下 http 強制跳轉到 https 的配置

       根目錄建立 .htaccess 文件。內容如下

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

       當用戶在瀏覽器直接輸入域名 ilexa.cn 的時候,這時候域名直接會轉向https://ilexa.cn,而且服務器這樣配置的效率也蠻高的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM