【问题标题】:How to get Slim PHP Framework routes in a subdirectory on nginx如何在 nginx 的子目录中获取 Slim PHP Framework 路由
【发布时间】:2016-05-26 08:04:31
【问题描述】:

我正在尝试将 Slim 应用程序移动到子目录,以便可以通过 example.com/api/ 访问它,但我在路由正常工作时遇到了严重问题。

主脚本位于/website/workbench/api/public/index.php,因此对example.com/api/project/1 的调用应该会命中API 文件夹。但是,我还需要能够访问 example.com 的 index.html 文件(在 Angular JS 上运行)。

当我转到 example.com/api/project/1 时,它确实会命中 PHP 脚本 - 我可以 var_dump 变量并查看它们。但是路由没有生效,请求变量好像是空的。

/etc/nginx/sites-available/workbench

server {
    listen   80; ## listen for ipv4; this line is default and implied

    root /website/workbench;
    index index.php index.html index.htm;

    server_name example.com;

    location / {
        try_files $uri $uri/ index.php?$query_string;
    }

    location /api/ {
        try_files $uri $uri/ /api/public/index.php?$query_string;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param PHP_VALUE "newrelic.appname=workbench";
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

(显然example.com被替换为真实域名)

var_dump($_SERVER)的部分输出;

array(34) {
  ["USER"]=>
  string(8) "www-data"
  ["HOME"]=>
  string(8) "/var/www"
  ["FCGI_ROLE"]=>
  string(9) "RESPONDER"
  ["PHP_VALUE"]=>
  string(26) "newrelic.appname=workbench"
  ["QUERY_STRING"]=>
  string(0) ""
  ["REQUEST_METHOD"]=>
  string(3) "GET"
  ["CONTENT_TYPE"]=>
  string(0) ""
  ["CONTENT_LENGTH"]=>
  string(0) ""
  ["SCRIPT_FILENAME"]=>
  string(39) "/website/workbench/api/public/index.php"
  ["SCRIPT_NAME"]=>
  string(21) "/api/public/index.php"
  ["REQUEST_URI"]=>
  string(15) "/api/projects/1"
  ["DOCUMENT_URI"]=>
  string(21) "/api/public/index.php"
  ["DOCUMENT_ROOT"]=>
  string(18) "/website/workbench"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["SERVER_SOFTWARE"]=>
  string(11) "nginx/1.4.6"
}

var_dump($_REQUEST) 给出一个空数组。

很明显,我的设置出现了严重错误,但我很难知道是什么!将$query_string 更改为$args 也无效。

【问题讨论】:

    标签: php nginx slim


    【解决方案1】:

    在这里聚会已经很晚了,但是我在站点根目录中提供静态文件(Angular 应用程序)和在 /api 中提供一个瘦应用程序时遇到了同样的问题。正如您所指出的,Slim 确实需要 REQUEST_URI在其中包含 /api

    server {
     server_name example.com;
    
        location ~ ^/api/(.*)$ {
               alias /path/to/slim-app/public/;
               try_files $1 $1/ @php;
               index index.php;
        }
    
        location / {
                root /path/to/your/static/files;
        }
    
        location @php {
               fastcgi_split_path_info ^(/api)(/.*)$;
               fastcgi_pass localhost:9000;
               fastcgi_index index.php;
               include fastcgi_params;
               fastcgi_param SCRIPT_FILENAME /path/to/slim-app/public/index.php;
               fastcgi_param REQUEST_URI $fastcgi_path_info;
               fastcgi_read_timeout 900;
        }
     }
    

    对我来说,关键在于fastcgi_split_path_info 诡计。根据http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_split_path_info,操作所做的是将$fastcgi_path_info 变量拆分为两个 变量,分别称为$fastcgi_script_name$fastcgi_path_info

    当您输入fastcgi_split_path_info ^(/api)(/.*)$; 时,您实际上将$fastcgi_script_name 设置为/api,并将$fastcgi_path_info 设置为/my/route。我发现这足以让 Slim (v3) 按我想要的方式工作。

    但是,我还发现我的默认 Slim 应用程序将 DOCUMENT_URLSCRIPT_NAME 变量设置为 index.php。所以你也可以设置它们(尽管它似乎不是必需的):

    set $script_name "/index.php";
    fastcgi_param DOCUMENT_URI $script_name;
    fastcgi_param SCRIPT_NAME $script_name;
    

    【讨论】:

    • 在尝试了我自己的想法和不同的建议一个小时后,这个完美的工作。
    【解决方案2】:

    我认为您可以从try_files 中删除$query_string。在/api 位置添加root 指令,如下所示:

    location /api {
        root /website/workbench/api;
        try_files $uri $uri/ /public/index.php;
    }
    

    并使用fastcgi_param SCRIPT_FILENAME参数最终定义为:

     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    

    进入fastcgi_params 文件。

    【讨论】:

    • 谢谢。 Slim 会从 request_uri 中获取参数,但这总是以 api/ 开头。我还需要移动根参数,使其位于每个位置块中。有人告诉我这是不好的做法,但必须这样做,因为 fastcgi 位置块依赖于它。
    • try_files 行中维护$query_string 的Slim Framework 路由会有任何副作用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    相关资源
    最近更新 更多