Nginx配置http跳轉https訪問


Nginx強制http跳轉https訪問有以下幾個方法

nginx的rewrite方法

可以把所有的HTTP請求通過rewrite重寫到HTTPS上

配置

方法一

1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名
4    rewrite ^(.*)$  https://XXXXXX.com permanent;      
5    location ~ / {
6    index index.html index.php index.htm;
7 }
8 }

方法二

1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名
4    return 301 https://$server_name$request_uri;      
5    location ~ / {
6    index index.html index.php index.htm;
7 }
8 }

方法三

1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名
4    rewrite ^(.*)$  https://$host$1 permanent;      
5    location ~ / {
6    index index.html index.php index.htm;
7 }
8 }

nginx的497狀態碼

497 – normal request was sent to HTTPS  
當前站點只允許HTTPS訪問,當使用HTTP訪問nginx會報出497錯誤碼
可以使用error_page 把497狀態碼鏈接重新定向到HTTPS域名上

1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名   
4    error_page 497  https://$host$uri?$args;  
5    location ~ / {
6    index index.html index.php index.htm; 
7 }
8 }

meta刷新作用將http跳轉到HTTPS

index.html

<html>
 <meta http-equiv=”refresh” content=”0;url=https://XXXX.com/”>
 </html>

nginx配置

1 server{  
2    listen 80;
3    server_name XXXXX.com;  //你的域名   
4    location ~ / {
5    root /var/www/test/;  
6    index index.html index.php index.htm;
7    }
8    error_page 404 https://xxxx.com
9 }

 


免責聲明!

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



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