在处理没有host
头部的请求时,我希望Nginx能达到444
。
我已经阅读http://nginx.org/en/docs/http/request_processing.html和Nginx的默认主机时,没有定义 ,它说:
"If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port."
我已经configuration好了
server { listen 80 default_server; # Since version 0.8.48, this is the default setting for the server name, # so the server_name "" can be omitted. # server_name ""; return 444; }
然后我请求
telnet localhost 80<enter> GET / HTTP/1.1<enter> <enter>
并收到400
,而不是我预期的444
:
HTTP/1.1 400 Bad Request Server: nginx/1.2.5 Date: Wed, 28 Nov 2012 21:01:59 GMT Content-Type: text/html Content-Length: 172 Connection: close [body]
我也是一样
telnet localhost 80<enter> GET / HTTP/1.1<enter> Host:<enter>
当没有host
头被提供时,我怎么能得到Nginx为444? 如果服务器认为请求是错误的请求,这是不可能的?
HTTP 1.1请求必须包含主机头。 如果不这样做,则服务器必须回答错误400.另请参阅RFC 2316,第14.23节
您仍然可以捕获400错误,并返回444。 以下工作适用于没有Host
头的请求
server { listen 80; server_name ""; return 444; error_page 400 = @400; location @400 { return 444; } }
PS如果你不发送GET /xxx HTTP/1.x
, POST /xxx HTTP/1.x
, HEAD /xxx HTTP/1.x
,或者奇怪的GET /
,你仍然会得到400。