Nginx

Nginx 強制 https 僅用於索引頁面

  • December 22, 2016

設置 nginx conf 以僅在 index.php 頁面上強制 https 並且每個其他頁面都強制為 http 的最佳方法是什麼。

謝謝

嘗試這樣的事情應該接近工作。我還沒有測試它,所以可能會有錯別字,但它至少應該給你一些指導。

# You might address PHP some other way
upstream php {
 server 127.0.0.1:9001;
}

server {
 server_name www.example.com example.com;
 listen 443 ssl http2;
 ssl_certificate /path/fullchain;
 ssl_certificate_key /path/privkey;

 # the index.php?$args is for wordpress, you can remove it
 try_files $uri $uri/ /index.php?$args; 

 # Single URL
 location = /index.php {
   # plus other PHP settings and parameters
   fastcgi_pass php; 
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 }

 # Every other URL
 location / {
   return 301 http://www.example.com$request_uri;
 }

 # Run PHP scripts
 location ~ \.php$ {
   # plus other PHP settings and parameters
   fastcgi_pass php; 
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 }
}

# Insecure server
server {
 server_name www.example.com example.com;
 listen 80 default_server;

 # the index.php?$args is for wordpress, you can remove it
 try_files $uri $uri/ /index.php?$args; 

 location = /index.php {
   return 301 https://www.example.com/index.php;
 }

 location / {
   root /var/www/site;
 }

 location ~ \.php$ {
   # plus other PHP settings and parameters
   fastcgi_pass php; 
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 }
}

引用自:https://serverfault.com/questions/822359