6

I am currently setting up a machine for local development using Vagrant. Everything runs as it should, expect query parameters aren't passed to PHP on subpages.

That means on www.example.com/?a=b, the query parameter is accessible, but on www.example.com/subpage/?a=b it's not.

The general reply I found using Google for this problem is to modify the try_files directive, but that isn't working for me. I've also checked the request_order & variables_order in php.ini – everything is setup correctly there.

This is my config:

 server {
     listen                80;
     server_name           example.com www.example.com;
     root                  /var/www/public;

     location / {
         index   index.html index.htm index.php;
         try_files $uri $uri/ /index.php?$query_string;
         include /etc/nginx/fastcgi_params;
     }

     location ~ \.php$ {
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $request_filename;
         include /etc/nginx/fastcgi_params;
     }

     sendfile off;
}

Since I don't know much about server setup & administration, I am hitting a brick wall here, still here are a few things I also checked:

  • $query_string is set in /etc/nginx/fastcgi_params as fastcgi_param QUERY_STRING $query_string; which seems correct to me.
  • The path to fastcgi_params is correct

Since it works when not being on a subpage, I am now suspecting the location blocks not matching, but I really don't understand how this could be the case – please help.

4
  • It's a long shot, but make sure you're not being redirected before you handle the request. You can easily see this in Chrome's DevTools Network tab, use Preserve log. Commented Apr 25, 2015 at 11:10
  • @DaliborKarlović Thanks for the suggestion – I checked & I am not being redirected.
    – Sven
    Commented Apr 25, 2015 at 11:24
  • Turning on debugging and watching the log would help. nginx.org/en/docs/debugging_log.html . Most distributions come with debugging pre-compiled. Commented Apr 27, 2015 at 2:10
  • Could you make this work? @Sven
    – elif
    Commented Mar 7, 2016 at 17:43

2 Answers 2

12

You need to use $is_args for question mark & $args or $query_string for query string afterwards question mark.

here it's the last combination.

try_files $uri $uri/ /index.php$is_args$query_string;

Also be sure that you have set

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;

then pass it fastcgi;

fastcgi_pass 127.0.0.1:9000;
3
  • check for $is_args in documentation. nginx.org/en/docs/http/ngx_http_core_module.html
    – risyasin
    Commented Apr 25, 2015 at 11:20
  • Cool, I think I understand. Still, query string are not present on subpages.
    – Sven
    Commented Apr 25, 2015 at 11:33
  • 1
    Man, $is_args$query_string saved my life for a query parameters in pages. Thanks a lot.
    – Francisco
    Commented Nov 19, 2016 at 5:05
0

A query string starts with a question mark.

Then set in the second block :

fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Remove include /etc/nginx/fastcgi_params; in the first block since you don't have any fastcgi_pass directive here.

7
  • Do I need to put this in both location blocks?
    – Sven
    Commented Apr 25, 2015 at 11:12
  • @Sven Why would you put this in the first block ? There's no fastcgi_pass directive here. Commented Apr 25, 2015 at 11:15
  • Okay I see. Unfortunately, it still doesn't work. Is the order of the directives in the location block important?
    – Sven
    Commented Apr 25, 2015 at 11:25
  • @Sven No it's not except if your include directive override a previous configuration line using the same directive. Can you clarify the it doesn't work ? This really seems to come from your app. Also make sure you are not hitting browser cache when testing. Commented Apr 25, 2015 at 11:27
  • Okay, thanks. For clarification: _SERVER["QUERY_STRING"] is still empty on a subpage with query parameters present (example.com/subpage/?a=b). On example.com?a=b the query string is present in _SERVER["QUERY_STRING"]. Do I need to do something else that reloading Nginx for changes to take effect?
    – Sven
    Commented Apr 25, 2015 at 11:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .