0

I have something funky going on with my NGINX configuration for Wordpress. Here's what it looks like:

     location /home {
       root /var/www/html/home;
       try_files $uri $uri/ /home/index.php?$args /home/index.php?q=$1;
     }
     location ~ home\/.*\.php$ {
       root /var/www/html/home;
       include snippets/fastcgi-php.conf;
       fastcgi_pass    unix:/var/run/php/php7.4-fpm.sock;
       fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }

Now if I navigate to: https://example.com/home/cart/?ec_page=checkout_info and inspect the $_REQUEST and $_SERVER automatic variables I get this apparent inconsistency that:

print_r($_REQUEST) has [REQUEST_URI] => /home/cart/?ec_page=checkout_info
print_r($_GET) has [q] => 

So it seems "obvious" to me that somehow my NGINX configuration isn't invoking PHP correctly so that $_GET is popolated with [ec_page] => checkout_info, and having stared at my NGINX rules, I kind of see it must be because /home/index.php?$args resolves to /home/index.php?/cart/?ec_page=checkout_info or some such nonsense, that results in a 404 error, which consequently delegates to /home/index.php?q=$1 and $1 is apparently empty.

What is the correct way to do this? I don't think this matters, but for what it's worth the trouble I'm running into here is related to the wp-easycart plugin, and my wordpress site is configured for Post name Permalinks (which I don't want to change). If I do change the Permalinks setting of Wordpress to Plain then the NGINX configuration above seems to work, but only because that effectively uses changes the way the URLs so that there are no route parameters and everything becomes a querystring parameter.

2
  • You have document root of /var/www/html/home but you also want /home in your URLs? Commented May 25, 2021 at 21:11
  • This is also not a programming question, as a server configuration question it belongs on Server Fault. Commented May 25, 2021 at 21:12

1 Answer 1

1

The question is rather how you ended up with try_files $uri $uri/ /home/index.php?$args /home/index.php?q=$1; as it makes little sense in some ways.

The $1 is used in regex capture groups, but there you have none. So indeed it will always be empty.

The $args should be combined with $is_args which resolves to empty string if there are no arguments, and ? otherwise.

There is only one de-facto standard construct that is applicable for many CMS frameworks including WordPress, and it goes like this:

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

This is for a case when WordPress is in a root of the website.

For your case (where it's in home subdirectory):

location = /home {
    return 301 /home/;
}
location /home/ {
    try_files $uri $uri/ /home/index.php$is_args$args;
}

This should be fairly sufficient to propagate the arguments correctly to the $_GET variables in PHP.

If you will want to ever optimize this configuration you can look at try_files-less configuration, for performance reasons.

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.