我使用 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 的任何流量。我怎样才能做到这一点?
定义默认服务器
如果您没有显式定义默认服务器,nginx 将隐式使用 first-found server。所以,只需创建一个服务器块来阻止未知主机:
(不,没有必要添加 server_name - 因为它永远不会匹配)。
有趣的是,我有时将“-”作为主机标头,这似乎没有被@AD7Six 的回答(?)抓住。至少在我的情况下它不起作用。
所以我稍微修改了这个referrer spam blog post entry ,它就像一个魅力:
不知道这是否是最好的解决方案,但它会让机器人远离我的网站。