问题原文
原帖链接 · Phung D. An · CC BY-SA 3.0
For many reasons we don't want/need to have user access php directly. Actually our php backend mostly serve Restful API. Only small portion of the code serve some MVC web. It is like describe below
- mywebsite.com/abcsomething.php -> block it ( harder to hack ?! )
- mywebsite.com/api/abc?xyz -> api.php (our API main point here)
- mywebsite.com/utility/abc?xyz -> utility.php (good old php code)
- mywebsite.com/admin/abc?xyz -> admin/admin.php (good old php code)
- mywebsite.com/everthing_else/something?xyz -> generated/index.html ( everthing else is served as single page website )
We don't really have index.php but many php main describe above.
I have googled and tried for almost 4 days but could not get it done. What I got so far:
...
root /www/app;
...
location ~ \.php$ {
deny all;
}
location ^~ /api {
}
location ^~ /utility {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index api.php;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ^~ /admin {
root /www/app/admin
fastcgi_pass 127.0.0.1:9000;
fastcgi_index api.php;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
root /www/app/generated
try_files $uri /index.html;
}
...
#1 and #5 seems to works but #2-4 I tried many techniques from try_files to rewrite, alias. But since I am inexperience, I could manage to get it works. Could someone give me a hints
已采纳解答
原帖链接 · Richard Smith · CC BY-SA 3.0
Assuming that any resource files are not relative to the same path, your approach is fine except for a couple of issues.
The SCRIPT_FILENAME needs to point to the script file to be executed (e.g. utility.php). The value for $fastcgi_script_name is inappropriate in this case, as it will be derived from the URI.
Also, fastcgi_param statements should be placed after include fastcgi_params; as the latter may contain conflicting statements.
For example:
location ^~ /api {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/api.php;
fastcgi_pass 127.0.0.1:9000;
}
location ^~ /utility {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/utility.php;
fastcgi_pass 127.0.0.1:9000;
}
location ^~ /admin {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/admin.php;
fastcgi_pass 127.0.0.1:9000;
}
Obviously you could continue to construct the SCRIPT_FILENAME using the $document_root variable as a prefix, but that seems pointless to me for a hard-wired value.
The REQUEST_METHOD should be defined in the fastcgi_params file.
EDIT:
BTW, I want to do something like remove the "api" prefix and send to php. Like "/api/customer/1" -> will send "customer/1" only to php.
You will need to identify which parameter your script uses to read the URI, but it will be one of the variables listed in the fastcgi_params file (e.g. REQUEST_URI).
You could change the location statement to a regular expression and capture the part of the URI you are interested in, for example:
location ~ ^/api(?<name>.*)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/api.php;
fastcgi_param REQUEST_URI $name$is_args$args;
fastcgi_pass 127.0.0.1:9000;
}
Be sure to place the block above any conflicting regular expression blocks, as regular expression location blocks are evaluated in order.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
