Nginx限制域
请找到以下设置,该设置位于我的网站域名下的/etc/nginx/sites-enabled 中. (mysite.lk)
Please find the below setting which is placed in /etc/nginx/sites-enabled under my site domain name. (mysite.lk)
server {
listen 80;
server_name mysite.lk www.mysite.lk;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:8080";
}
}
该应用程序在端口8080上运行,在这里我将所有80流量重定向到8080. 我的网站仅使用 mysite.lk 和 www.mysite.lk 域名.
The application is running on port 8080 and here I'm redirecting all the 80 traffic to 8080. My website only uses mysite.lk and www.mysite.lk domain names.
因此,我想限制/阻止所有其他要进入此服务器IP的域(除了mysite.lk和www.mysite.lk).为此,我需要做些什么改变?
Hence, I want to restrict/block all other domains (except mysite.lk and www.mysite.lk) which are coming to this server IP. What is the change that I need to do to achieve this?
我尝试了很多事情,例如 为什么Nginx响应任何域名?,但是在Nginx启动时出现错误.
I tried numerous things such as answers given in the Why is nginx responding to any domain name?, but was getting errors at the nginx startup.
请帮帮我! 谢谢.
找到答案.在给定配置之前,应在配置顶部需要一个包含所有内容的服务器块,如下所示.代码块应该是这样的.
Found the Answer. A catch-all server block should needed in the top of the config before the given config like below. The code block should be like this.
server {
return 403;
}
server {
listen 80;
server_name mysite.lk www.mysite.lk;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:8080";
}
}
在Nginx中定义的第一台服务器被视为default_server
,因此只需将其中一个添加为默认服务器并返回412(失败的前提条件)或任何其他状态,最适合您的要求,将有助于后续服务器遵守server_name
The first server defined in Nginx is treated as the default_server
so by just adding one as the default and returning 412 (Precondition Failed) or any another status that best fits your requirements, will help for the subsequent servers to obey the server_name
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 412;
}
server {
listen 80;
server_name mysite.lk www.mysite.lk;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:8080";
}
}