Tobias P.
Tobias P.

Reputation: 4665

nginx is not passing fastcgi query string

With some (or very much) trial and error i was able to modify my copy and pasted nginx fastcgi php configuration from somewhere years ago to be able to run my php application in a subfolder.

But the last final step i am not able to solve is how to get nginx to pass the query string to php to be able to access the GET parameters. This is my configuration mostly perfect with only the configuration parameters missing:

server {
    listen      80;
    server_name project.dev;

    location /app/ {
        alias /path/to/my/application/;
        index index.php;
        try_files $uri $uri/ /app/index.php;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        }
    }

    location / {
        # configuration for static website
    }
}

I read that there are different options you have to pass to try_files to get request parameters:

Unfortunately changing it to any of these results in my php script no longer being found because nginx resets the request to it's document root:

2016/11/25 11:54:48 [error] 45809#0: *1169 open() "/usr/local/Cellar/nginx-full/1.10.2/htmlindex.php" failed (2: No such file or directory), client: 127.0.0.1, server: project.dev, request: "GET /app/myurl?test=works HTTP/2.0", host: "project.dev", referrer: "http://project.dev/app/myurl?test=works"

Providing an absolute path for fastcgi_param SCRIPT_FILENAME does not work too producting the same error. Even setting a root configuration on the server level does not work correctly, because the separating slash for the path and the index.php is omitted everytime. But (if possible) i would prefer without setting a root directory at the server level because this project is consisting of many different folders and applications on the filesystem sharing no common directory.

Upvotes: 4

Views: 4490

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

You have an application installed under /path/to/my/app2/public and would like to access it using the URI /app.

Assuming that we can use /app2/ as an internal URI (which does not collide with any other public URIs served by this server - but importantly will not be seen by your customers).

You have one PHP file.

location ^~ /app {
    rewrite ^/app(.*)$ /app2/public$1 last;
}        

location ^~ /app2/ {
    internal;

    root /path/to/my;
    index index.php;

    try_files $uri $uri/ /app2/public/index.php$is_args$args;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /path/to/my/app2/public/index.php;
    }
}

The first location block simply alters the internal URI to match the document root (so we can use root instead of alias). The second location block serves the static content. The third location block invokes index.php.

How index.php gets the query string is program dependent. It will use one of the parameters defined in fastcgi_params. Usually either REQUEST_URI or QUERY_STRING. Either way, both variables should be preserved with the above configuration.

The ^~ modifier ensures that these location blocks take precedence over other regular expression location blocks (should any exist). See this document for details.

Upvotes: 3

Related Questions