4

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:

  • try_files $uri $uri/ /app/index.php$is_args$query_string;
  • try_files $uri $uri/ /app/index.php$is_args$args;
  • try_files $uri $uri/ /app/index.php?$query_string;

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.

7
  • What are you trying to achieve? What files do you have under /path/to/my/application/? Commented Nov 25, 2016 at 14:17
  • my complete application is saved under /path/to/my/application/ with all its assets and it's php. Loading of the assets and running the php works exactly as expected, but for some reason i can't pass the $_GET parameters to the fastcgi Commented Nov 25, 2016 at 14:58
  • The problem is alias and try_files. You want to run your application under the URI /app, things would be very simple if /path/to/my/application/ ended with /app (the same subdirectory name as the URI). Commented Nov 25, 2016 at 15:03
  • Thats sadly not possible because the location / is another website with it's own filesystem and structure i am not allowed to mess with. But try_files with alias is working perfectly: PHP gets called, assets are sent - only the request parameters are not sent to PHP. It only goes downhill as soon as i add these suffixes to get the query string. Commented Nov 25, 2016 at 15:11
  • Is there just the single PHP file under /path/to/my/application/? BTW all you need to do is make a directory called app where your files are and move them into it - I'm not asking you to change anything in /. Commented Nov 25, 2016 at 15:27

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.