繁体   English   中英

PHP Wordpress 和 Nginx 配置

[英]PHP Wordpress and Nginx Configuration

我的 Wordpress 的 NGINX 配置发生了一些奇怪的事情。 这是它的样子:

     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;
     }

现在,如果我导航到: 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 $1显然是空的。

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

问题是你是如何结束的try_files $uri $uri/ /home/index.php?$args /home/index.php?q=$1; 因为它在某些方面没有什么意义。

$1用于正则表达式捕获组,但你没有。 所以确实它永远是空的。

$args应该与$is_args结合,如果没有 arguments 和? 否则。

只有一个事实上的标准结构适用于包括 WordPress 在内的许多 CMS 框架,它是这样的:

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

这是针对 WordPress 位于网站根目录中的情况。

对于您的情况(在home子目录中):

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

这应该足以将 arguments 正确传播到 PHP 中的$_GET变量。

如果您想要优化此配置,您可以查看try_files -less configuration ,出于性能原因。

暂无
暂无

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

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