NGINX: route to index.php within alias for specific path component with query string

Venu asked:

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.


 1.         --> /home/user/www/public/index.php
 2.     --> /home/user/www/public/index.php
 3.     --> /home/user/api/public/index.php
 4. --> /home/user/api/public/index.php
 5. --> /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

    [USER] => www-data
    [HOME] => /var/www
    [SCRIPT_FILENAME] => /home/user/api/public//index.php
    [QUERY_STRING] => 
    [CONTENT_TYPE] => 
    [SCRIPT_NAME] => /api/index.php
    [REQUEST_URI] => /api/account/get?id=1
    [DOCUMENT_URI] => /api/index.php 

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.

My answer:

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

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.