我怎样才能阻止错误的主机头集请求?

我使用nginx来服务我的网站。 我想阻止与HTTP“主机”标题不匹配我的网站的域的所有请求。

更具体地说,我的nginx.conf包含这两个服务器块:

server { # Redirect from the old domain to the new domain; also redirect # from www.newdomain.com to newdomain.com without the "www" server_name www.olddomain.com olddomain.com www.newdomain.com; listen 80; return 301 $scheme://newdomain.com$request_uri; } server { server_name newdomain.com localhost; listen 80; # Actual configuration goes here... } 

我想拦截(即“返回”444状态码)任何其主机不是www.olddomain.com,olddomain.com,www.newdomain.com或newdomain.com的stream量。 我怎样才能做到这一点?

定义一个默认的服务器

如果你没有明确的定义一个默认的服务器, nginx会隐式地使用第一个被发现的服务器 。 所以,只需创build一个服务器块来阻止未知的主机 :

 server { listen 80 default_server; return 444; } 

(不,不需要添加一个server_name – 因为它永远不会匹配)。