如何设置Nginx URI以修复重定向到指定位置的空URI [英] How to set Nginx URI to fix empty URI in redirect to named location

查看:483
本文介绍了如何设置Nginx URI以修复重定向到指定位置的空URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:当使用包含'%'符号的无效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开了票,这是响应:

[...]问题是您试图将在早期请求处理阶段(当请求URI尚未解析时)出现的错误重定向到指定位置.命名位置旨在保留现有的请求URI,并且将这样的不完整请求重定向到命名位置可能会在代码的其他部分引起严重的问题.这样,尝试进行这样的重定向会导致重定向到指定位置的空URI"错误.反过来,这会阻止您使用自定义错误页面,因此将返回内部页面. 如果要处理在早期请求处理阶段出现的错误,例如400 Bad Request,请考虑使用带有明确设置URI的错误页面.或者,您也可以避免尝试重定向此类错误,因为这通常是更安全的方法. 请注意,服务器为nginx并不是信息泄漏.无论如何,它以多种方式报告,包括服务器响应标头.如果出于某种原因想要隐藏nginx版本,则可以通过server_tokens指令进行隐藏.

[...] the problem is that you are trying to redirect errors which appear during early request processing stages - when the request URI is not yet parsed - to a named location. Named locations are designed to preserve existing request URI, and redirecting such an incomplete request to a named location is likely to cause serious problems in other parts of the code. As such, an attempt to do such a redirect results in the "empty URI in redirect to named location" error. This in turn prevents your custom error page from being used, so an internal one is returned. If you want to handle errors which appear during early request processing stages, such as 400 Bad Request, consider using error pages with explicitly set URI. Or you may instead avoid trying to redirect such errors, as this is generally much safer approach. Note well that the fact that server is nginx is not an information leak. It is anyway reported in a number of ways, including the Server response header. If you for ​some reason want to hide nginx version, you can do so via the ​server_tokens directive.

因此,我将错误自定义更改如下:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆