![Nginx 强制 https 仅适用于索引页](https://linux22.com/image/695779/Nginx%20%E5%BC%BA%E5%88%B6%20https%20%E4%BB%85%E9%80%82%E7%94%A8%E4%BA%8E%E7%B4%A2%E5%BC%95%E9%A1%B5.png)
设置 nginx conf 的最佳方法是什么,以便仅在 index.php 页面上强制使用 https,而其他每个页面都强制使用 http。
谢谢
答案1
尝试一下类似这样的方法应该差不多可以了。我还没有测试过,所以可能会有拼写错误,但它至少应该能给你一些指导。
# 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;
}
}