简体   繁体   English

PHP Wordpress 和 Nginx 配置

[英]PHP Wordpress and Nginx Configuration

I have something funky going on with my NGINX configuration for Wordpress.我的 Wordpress 的 NGINX 配置发生了一些奇怪的事情。 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:现在,如果我导航到: https://example.com/home/cart/?ec_page=checkout_info并检查 $_REQUEST 和 $_SERVER 自动变量,我会发现这种明显的不一致:

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. 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 $1显然是空的。

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).我认为这并不重要,但是我在这里遇到的麻烦与 wp-easycart 插件有关,并且我的 wordpress 站点配置为Post name永久链接(我不想更改) . 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.如果我确实将 Wordpress 的永久链接设置更改为Plain ,那么上面的 NGINX 配置似乎有效,但这只是因为它有效地使用了更改 URL 的方式,因此没有路由参数,一切都变成了查询字符串参数。

The question is rather how you ended up with try_files $uri $uri/ /home/index.php?$args /home/index.php?q=$1;问题是你是如何结束的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. $1用于正则表达式捕获组,但你没有。 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 ? $args应该与$is_args结合,如果没有 arguments 和? otherwise.否则。

There is only one de-facto standard construct that is applicable for many CMS frameworks including WordPress, and it goes like this:只有一个事实上的标准结构适用于包括 WordPress 在内的许多 CMS 框架,它是这样的:

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

This is for a case when WordPress is in a root of the website.这是针对 WordPress 位于网站根目录中的情况。

For your case (where it's in home subdirectory):对于您的情况(在home子目录中):

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.这应该足以将 arguments 正确传播到 PHP 中的$_GET变量。

If you will want to ever optimize this configuration you can look at try_files -less configuration , for performance reasons.如果您想要优化此配置,您可以查看try_files -less configuration ,出于性能原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM