问题描述
问题:当使用包含'%'符号的无效URL访问我们的网站时,Nginx会引发400错误请求错误.
The problem: when accessing our website with an invalid URL that contains a '%' sign, Nginx throws a 400 Bad Request error.
我们希望将请求重写为(WordPress)404页面,而不是Nginx页面.
Instead of the Nginx page, we would like to rewrite the request to a (WordPress) 404 page.
我尝试了以下操作:
location @400 {
rewrite ^ /404 break;
}
error_page 400 =307 @400;
但是,这会在Nginx上生成500 Internal Server Error.
However, this generates a 500 Internal Server Error on Nginx.
Nginx错误日志说:"...空URI重定向到命名位置"@ 400",同时读取客户端请求行...,请求:"GET< invalid-url>"".
Nginx error log says: '... empty URI in redirect to named location "@400" while reading client request line..., request: "GET <invalid-url>"'.
这并不意外,因为原始URL无效.
This is not unexpected, as the original URL is invalid.
所以我想我正在寻找的是为重写显式设置URI.这该怎么做?还是有更好的方法?我对Nginx不太熟悉.
So I guess what I am looking for is to set the URI explicitly for the rewrite. How to do this? Or is there a better approach? I am not that familiar with Nginx.
推荐答案
我有相同的目标(返回444而不是自定义错误消息),并且使用@named_location
遇到了相同的问题(对于某些奇怪的请求,尤其是当没有在标头中设置了POST/GET/HEAD方法后,Nginx返回了默认的500错误页面,并报告了错误日志:empty URI in redirect to named location "@named_location" while reading client request
).
I had the same objective (return 444 instead of customised error messages) and encountered the same problem using @named_location
(for certain weird requests, in particular when no POST/GET/HEAD method set in header, Nginx returned its default 500 error page and the error log reported: empty URI in redirect to named location "@named_location" while reading client request
).
有人用Nginx开了票,这是响应:
因此,我将错误自定义更改如下:
So I changed the error customisation as follows:
error_page 301 400 403 404 500 502 503 504 =444 /444.html;
location = /444.html {
return 444;
}
# Instead of:
# error_page 301 400 403 404 500 502 503 504 =444 @named_location;
# location @named_locations {
# return 444;
# }
# which gives the above-mentioned error and returns Nginx's default 500 error page.
这似乎已经解决了问题.
This appears to have solved the issue.
这篇关于如何设置Nginx URI以修复重定向到指定位置的空URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!