Nginx配置http跳轉https訪問

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

nginx的rewrite方法

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

配置

方法一:

server{  
   listen 80;
   server_name XXXXX.com;  #你的域名
   rewrite ^(.*)$  https://XXXXXX.com permanent;      
   location ~ / {
       index index.html index.php index.htm;
   }
}

方法二:

server{  
   listen 80;
   server_name XXXXX.com; #你的域名
   return 301 https://$server_name$request_uri;      
   location ~ / {
     index index.html index.php index.htm;
   }
}

方法三:

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

nginx的497狀態碼

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

server{  
   listen 80;
   server_name XXXXX.com;  ####你的域名   
   error_page 497  https://$host$uri?$args;  
   location ~ / {
     index index.html index.php index.htm; 
   }
}

meta刷新作用將http跳轉到HTTPS

index.html

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

nginx配置

server{  
   listen 80;
   server_name XXXXX.com; ##你的域名 
   location ~ / {
     root /var/www/test/;
     index index.html index.php index.htm;
   }
   error_page 404 https://xxxx.com
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章