Wordpress在Nginx子目录部署时$_GET参数为空问题求解
问题根源
你当前配置的错误主要来自3个核心问题:
- 路径映射错误:
location /home块内使用root指令会自动拼接location前缀,导致实际查找路径变为/var/www/html/home/home/xxx,和实际文件存放路径不符 - try_files规则冗余且错误:额外的
/home/index.php?q=$1规则中,$1没有对应的正则捕获源,必然是空值,同时多写的规则会覆盖之前的$args参数,导致GET参数丢失 - PHP路径解析错误:匹配PHP的location规则同样存在路径拼接错误,导致PHP文件解析异常
正确配置
如果你的WordPress安装在服务器/var/www/html/home路径,对外访问前缀是https://example.com/home/,推荐使用以下配置,无需修改WordPress固定链接设置:
server { # 保留你原有server块的其他配置,比如监听端口、server_name、日志规则等 root /var/www/html; index index.php index.html index.htm; location /home { try_files $uri $uri/ /home/index.php?$args; } location ~ ^/home/.*\.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
如果你必须把路径配置写在location块内,就用alias替代root:
location /home { alias /var/www/html/home; try_files $uri $uri/ /home/index.php?$args; } location ~ ^/home/.*\.php$ { alias /var/www/html/home; include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $request_filename; }
生效方式
修改配置后执行nginx -t检查配置语法无误后,执行nginx -s reload重载配置即可生效,此时访问对应页面即可正常获取ec_page等GET参数,wp-easycart插件可正常运行。
内容的提问来源于stack exchange,提问作者vicatcu
相关产品推荐
相关产品推荐

