【发布时间】:2019-11-15 07:07:13
【问题描述】:
背景: 我们使用外部服务重定向到我们的 Wordpress 网站上的确认页面。该服务通过我们想要删除的 URL 字符串发送某些参数,因为它们包含私人信息。不幸的是,该服务在那个方向上是不可配置的(即它总是会发送完整的参数集)。
目标: 我目前正在研究一个 nginx 位置块,该块应该将某些 URL 参数从请求中剥离到某个页面,并使用“干净”的 URL 重定向到同一页面。到目前为止,我已经设法摆脱了 URL 中不需要的参数,但是,我正在努力处理 location 块的重写部分。看来我还没有弄清楚如何在 Wordpress 上下文中正确重写 URL。
错误描述:
当使用参数?full_name=john 访问所需的 URL 时,nginx 会正确剥离参数并尝试重定向到页面。但是,nginx 会抛出 404 错误。
环境:
- Ubuntu 16.04 上的 Plesk Onyx 17.8
- Wordpress 5.2.2
- nginx:Plesk 的
sw-nginx - PHP:7.3.6(通过 nginx 进行 FPM)
代码: 我用沉思伟对Remove parameters within nginx rewrite的评论开始了。 这是我目前所拥有的:
location ^~ /confirmation/ {
if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
set $original_path $1;
set $args1 $2;
set $unwanted $3;
set $args2 $4;
set $args "";
rewrite ^(.+)$ /index.php/$original_path?$args1$args2 permanent;
}
}
我认为罪魁祸首是 rewrite ^(.+)$ /index.php/$original_path?$args1$args2 permanent;,因为我最不确定如何重写这个对 Wordpress 友好的版本。
我非常感谢任何正确方向的帮助或指示。提前非常感谢!
2019-07-05 更新:
感谢@RichardSmith,我修改了代码:
location ^~ /confirmation/ {
if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
set $original_path $1;
set $args1 $2;
set $unwanted $3;
set $args2 $4;
set $args "";
rewrite ^(.+)$ $original_path?$args1$args2 permanent;
}
}
这会导致 404 错误(取自 proxy_error_log):
2019/07/05 11:51:13 [error] 22623#0: *39709 "/var/www/vhosts/domain.de/sub.domain.de/confirmation/index.html" is not found (2: No such file or directory), client: 109.41.XXX.XXX, server: sub.domain.de, request: "GET /confirmation/ HTTP/2.0", host: "sub.domain.de"
这看起来像预期的行为,因为该位置没有index.html。但是,现在我必须告诉 nginx 不要将index.html 附加到重写的请求中。
您知道如何完成此任务吗? 提前致谢!
解决方案:
我的问题的解决方案是我缺少一条语句来告诉 nginx 在找不到文件时如何工作。所以nginx一直在那个地方找index.html,没找到。在 URL 重写之后添加一个额外的 if() 块来处理没有文件的位置访问就可以了。
代码:
location ^~ /confirmation/ {
if ($request_uri ~ "([^\?]*)\?(.*)full_name=([^&]*)&?(.*)") {
set $original_path $1;
set $args1 $2;
set $unwanted $3;
set $args2 $4;
set $args "";
rewrite ^(.+)$ $original_path?$args1$args2 permanent;
}
if (!-e $request_filename) {
rewrite / /index.php last;
}
}
【问题讨论】:
-
您将
index.php放在URI 前面,然后将原始请求附加为path_info。如果原始请求已经被正确处理,尽管带有私人信息,也许您应该从重写的 URI 中删除/index.php/部分。 -
感谢您的指点,@RichardSmith!我现在尝试了更多,我在代理日志中发现了以下错误:
2019/07/05 11:51:13 [error] 22623#0: *39709 "/var/www/vhosts/domain.de/sub.domain.de/termin-bestaetigt/index.html" is not found (2: No such file or directory), client: 109.41.XXX.XXX, server: sub.domain.de, request: "GET /termin-bestaetigt/ HTTP/2.0", host: "sub.domain.de"所以 nginx 正在尝试将 /index.html 附加到请求中,但无法正常工作,因此 404 到目前为止是正确的。现在我需要知道如何阻止这种行为......
标签: wordpress nginx url-rewriting nginx-location nginx-reverse-proxy