问题原文
原帖链接 · guidev · CC BY-SA 3.0
I'm trying to forward every request to the same php script.
I came up with this configuration:
server {
listen 80;
server_name example.com;
root /var/www/html;
index api.php;
location / {
try_files $uri /api.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index api.php;
fastcgi_pass 127.0.0.1:9000;
}
}
While it works, it still allows direct access to resources if the correct $uri is provided.
If I try to change the first location block to this:
location / {
try_files /api.php$is_args$args =404;
}
It doesn't work, always returns 404.
已采纳解答
原帖链接 · Pothi Kalimuthu · CC BY-SA 3.0
server {
server_name example.com;
root "/var/www/html";
location / {
include "fastcgi_params";
fastcgi_param SCRIPT_FILENAME $document_root/api.php;
fastcgi_param SCRIPT_NAME api.php;
fastcgi_index api.php;
fastcgi_pass 127.0.0.1:9000;
}
}
If the aim is to forward all the requests to a single PHP script and if that single PHP script is known in advance by its name, then we don't need a separate location block. Only one location block that sends all the requests to that known PHP script is enough.
fastcgi_split_path_info is not needed, when the PHP script to be executed is not known in advance and is available only as part of the URI.
Also, try_files $uri =404 isn't needed since that known PHP script is already available at a fixed / known place.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
