NGINX:使用查询字符串将特定路径组件的别名路由到 index.php

NGINX:使用查询字符串将特定路径组件的别名路由到 index.php

这是旧版应用程序(Zend Framework 1),我们正在从 Apache 迁移到 Nginx。我们有前端控制器,可根据请求 URI 调度到特定控制器/操作。

要求

 1. www.example.com         --> /home/user/www/public/index.php
 2. www.example.com/a/b     --> /home/user/www/public/index.php
 3. www.example.com/api     --> /home/user/api/public/index.php
 4. www.example.com/api/a/b --> /home/user/api/public/index.php
 5. www.example.com/api/a/b?x=1 --> /home/user/api/public/index.php

当前配置

    location /api {
            alias /home/user/api/public/;
            try_files $uri /api/index.php;
             location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
            }           

}

以下是 PHP 脚本接收的内容

array
(
    [USER] => www-data
    [HOME] => /var/www
    [FCGI_ROLE] => RESPONDER
    [SCRIPT_FILENAME] => /home/user/api/public//index.php
    [QUERY_STRING] => 
    [REQUEST_METHOD] => GET
    [CONTENT_TYPE] => 
    [CONTENT_LENGTH] => 
    [SCRIPT_NAME] => /api/index.php
    [REQUEST_URI] => /api/account/get?id=1
    [DOCUMENT_URI] => /api/index.php 
...
)

问题 上述配置运行正常。但缺少查询字符串,因此 $_GET 为空。

黑客:在前端控制器调度之前从请求 URI 生成 $_GET。

$arr = explode("?",$_SERVER['REQUEST_URI']);
if (count($arr) == 2 && count($_GET) == 0){
    parse_str($arr[1], $_GET);
} 

但我想在服务器级别修复此问题,请告诉我在这种情况下如何传递查询字符串。提前致谢。

[编辑] /etc/nginx/fastcgi_params

fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;

fastcgi_param   SCRIPT_FILENAME         $request_filename;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;

fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;

fastcgi_param   HTTPS                   $https if_not_empty;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS         200;

完整配置:

server {
    listen *:80;
    server_name dev.xyz.com;

    # Character Set
    charset utf-8;

    # Logs
    access_log /var/log/nginx/access;
    error_log /var/log/nginx/error;

    # Document Root
    root /home/user/www/src;
    index  index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }

    # Error Pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location /api/ {
        alias /home/user/api/public/;
        try_files $uri /api/index.php;
         location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
        }


    }

答案1

你之所以错失机会是因为你尚未通过考试。

fastcgi_param QUERY_STRING $query_string;

顺便说一句,您需要$_SERVER['QUERY_STRING']

答案2

您需要检查它/etc/nginx/fastcgi_params是否与供应商发货时一样,并且没有经过任何形式的更改。如果已经更改,您应该恢复一份干净的副本。


alias此外,在 a 中使用时location,需要有匹配的尾部斜杠。

您目前有:

    location /api {
            alias /home/user/api/public/;

但这应该是:

    location /api/ {
            alias /home/user/api/public/;

如果没有这个,根据缺少哪个尾随斜杠,URL 可能会有双斜杠(Linux 不关心,但 Web 应用程序可能会关心)或者 URL 可能根本没有斜杠(总是会中断)。

相关内容