1

This is legacy app (Zend framework 1), we are moving from apache to nginx. We have front controller which dispatches to specific controller/action based on request URI.

Requirement

 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

Current Config

    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;
            }           

}

Here is what PHP scripts receives

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 
...
)

Problem Above config is working fine. But query string is missing, so $_GET is empty.

Hack: Generate $_GET from Request URI before front controller dispatches.

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

But I would like fix this at server level, please tell me how to pass query string in this case. Thanks in advance.

[EIDT] /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;

Full Config:

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;
        }


    }
Venu
  • 215
  • 1
  • 7

2 Answers2

0

You're missing it because you're not passing it.

fastcgi_param QUERY_STRING $query_string;

And, by the way, you need the $_SERVER['QUERY_STRING'].

drookie
  • 8,051
  • 1
  • 17
  • 27
0

You need to check that /etc/nginx/fastcgi_params is as shipped by the vendor, and has not been altered in any way. If it has been changed at all, you should restore a clean copy.


Also, when using alias in a location, you need to have matching trailing slashes.

You currently have:

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

But this should be:

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

Without this, depending on which trailing slash is missing, URLs may have double slashes (which Linux doesn't care about, but web applications might) or URLs may not have a slash at all (which always breaks).

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • ya you are right about trailing slash.. will fix it.. could you please suggest what could be causing an issue with query string? if you want, I can share whole config – Venu Nov 18 '17 at 13:08