我已经在ip 123.123.123.123的nginx web服务器上托pipeexample.com,如果我从远程机器使用像这样的主机头curl
curl -v -H "Host: example.com" 123.123.123.123/
我明白了
curl -v -H "Host: example.com" 123.123.123.123/ * Trying 123.123.123.123... * Connected to 123.123.123.123 (123.123.123.123) port 80 (#0) > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/7.43.0 > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.9.9 < Date: Tue, 15 Dec 2015 01:34:22 GMT < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < Set-Cookie: __cfduid=d2a30e617da2fbe890a6855ba4d993bc61450143261; expires=Wed, 14-Dec-16 01:34:21 GMT; path=/; domain=.example.com; HttpOnly < Vary: Accept-Encoding < Set-Cookie: PHPSESSID=mmdnqp3j4fnf0ph57krl696rb2; path=/ < CF-RAY: 254e67dae06335a8-LHR
这是好的,因为example.com是在服务器上有ip 123.123.123.123
但如果我更改主机头到任何其他域如anyother_domain.com
curl -v -H "Host: anyother_domain.com" 123.123.123.123/
我明白了
curl -v -H "Host: anyother_domain.com" 123.123.123.123/ * Trying 123.123.123.123... * Connected to 123.123.123.123 (123.123.123.123) port 80 (#0) > GET / HTTP/1.1 > Host: anyother_domain.com > User-Agent: curl/7.43.0 > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.9.9 < Date: Tue, 15 Dec 2015 01:30:29 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 612 < Last-Modified: Sat, 12 Dec 2015 16:03:23 GMT < Connection: keep-alive < ETag: "566c454b-264" < Accept-Ranges: bytes
这里是我的nginx.conf文件
worker_processes 4; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; gzip on; charset utf-8; include /usr/local/nginx/conf/sites-enabled/*.conf; }
default.conf
server { listen 80; server_name 123.123.123.123; charset utf-8; }
example.com.conf
server { server_name www.example.com; return 301 http://example.com$request_uri; access_log /dev/null; } server { server_name example.com; }
这里是答案: http : //nginx.org/en/docs/http/request_processing.html
为什么发生这种情况? 我的意思是为什么服务器提供200 OK状态,即使该域名不在我的服务器托pipe?
[通常] nginx只testing请求的头部字段“主机”,以确定请求路由到哪个服务器。 如果它的值不匹配任何服务器名称,或者请求根本不包含这个头域,那么nginx会将请求路由到这个端口的默认服务器。 在上面的configuration中,默认服务器是第一个 – 这是nginx的标准默认行为。
我怎样才能解决这个问题 ? 我的意思是我怎么不给200 OK状态,并抛出一些其他的头响应代码?
如果不应该允许没有“主机”头字段的请求,则可以定义一个只删除请求的服务器:
服务器{听80; 服务器名称 ””; 返回444; }
在这里,服务器名称被设置为一个空string,它将匹配没有“主机”头字段的请求,并且返回closures连接的特殊的nginx的非标准代码444。
或者尝试所有“默认”请求
server { listen 80 default_server; server_name ""; return 444; }