大网数据广告活动
服务器运维问答

How to block php extension while still route many php endpoint in nginx

· 52CE 我爱测

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 …

问题原文

原帖链接 · 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

  1. mywebsite.com/abcsomething.php -> block it ( harder to hack ?! )
  2. mywebsite.com/api/abc?xyz -> api.php (our API main point here)
  3. mywebsite.com/utility/abc?xyz -> utility.php (good old php code)
  4. mywebsite.com/admin/abc?xyz -> admin/admin.php (good old php code)
  5. 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。