Nginx sub_filter对WordPress/Elementor未压缩JSON不生效问题排查
Nginx sub_filter对部分WordPress JSON响应不生效问题排查
我已花费大量时间搜索该问题,甚至反复咨询ChatGPT仍未解决,因此寻求技术帮助。
我在PHP-FPM(WordPress)服务器前部署了Nginx反向代理,使用sub_filter修改响应内容。该功能对部分JSON响应生效,但对另一些则无效。
Nginx相关配置片段
# Reverse proxy server { ... # 证书、日志等配置已简化移除 root /var/www/html; index index.php index.html index.htm; ... location / { gzip off; ... # proxy_set_header相关配置已移除 proxy_set_header Accept-Encoding ""; proxy_pass http://127.0.0.1:8082; sub_filter_once off; sub_filter_types *; sub_filter "target_vlue" "replacement_value"; } } # Application http server { ... # 监听、日志等配置已移除 location ~ \.php$ { gzip off; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass test-wordpress:9000; # 基于Docker部署 } }
我采用双服务器架构(原因暂不赘述,与当前问题无关)。
注意:配置中已关闭gzip,并设置Accept-Encoding为空白。PHP-FPM中也已关闭压缩:
9dedac8fdba4:/var/www/html# php -i |grep zlib zlib.output_compression => Off => Off zlib.output_compression_level => -1 => -1 zlib.output_handler => no value => no value
我已确认响应的Content-Type头为application/json; charset=utf-8,且application/json已包含在sub_filter_types配置中。
进一步调试情况
sub_filter未生效的JSON由WordPress的Elementor插件生成,以下是响应片段:
{ "success": true, "data": { "responses": { "app_site_editor_template_types": { "success": true, "code": 200, "data": [ { "type": "header", ... "urls": { ... "thumbnail": "https://<TARGET_VALUE>/wp-content/plugins/elementor/assets/images/app/site-editor/header.svg", // <-- 此处target_value未被sub_filter替换 "EXTRA_DEBUG_TEST": "DEADBEEF" // <--- 我注入的测试值,同样未被替换 } }, ...
如注释所示,TARGET_VALUE未按预期被sub_filter替换;即使将sub_filter的目标改为DEADBEEF,同样无效。我已编辑插件的.php文件注入EXTRA_DEBUG_TEST字段。
但如果将这段JSON的简化版本放入单独的.php文件,通过同一服务器架构请求时,值能被正常替换:
{ "status": "success", "message": "This is a simple JSON response", "items": [ 1, 2, 3, 4, 5 ], "url": "https://<TARGET_VALUE>", // <-- sub_filter成功捕获并替换 "url2": "https://<TARGET_VALUE>/wp-content/plugins/elementor/assets/images/app/site-editor/header.svg", // <-- sub_filter成功捕获并替换 }
注:上述是用于测试的完整JSON。
编辑补充:问题并非出在JSON本身。我编辑插件的.php文件返回一个非常简单的JSON对象:
{ "success": true, "data": { "responses": [ "https://<TARGET_VALUE>/wp-admin" ] } }
sub_filter仍未替换target_value。
内容的提问来源于stack exchange,提问作者MSurrow
相关产品推荐
相关产品推荐

