如何拒绝Nginx中的子域请求

如果我的configuration看起来像

server { listen 80; server_name example.com; } 

如何拒绝对subdomain.example.com的请求?

 server { listen 80; server_name subdomain.example.com; deny all; } 

或者,如果您想要将所有不属于另一个server块中明确定义的域的stream量都丢弃到您的configuration中:

 server { listen 80 default_server; server_name _; deny all; } 

谢恩马登的答案将工作,或者你也可以使用非标准的响应代码444,这将终止连接,而不发送任何头(源: http : //wiki.nginx.org/HttpRewriteModule )

要阻止特定的子域名:

 服务器{
     server_name subdomain.example.com;
        返回444;
 } 

或者阻止其他地方无法处理的所有子域名或域名:

服务器{
    服务器名称 _;
    返回444;
 }

后面的选项是有用的,当阻止复制您的内容的域名,从而可能伤害您的search引擎排名。 (这来自个人经验。)

如果你的子域有交通,更好的redirect所有的请求从子域到域这样的

  if ($http_host != "example.com") { rewrite ^ http://example.com$request_uri permanent; } 

在服务器部分

这是SEO友好。