nginx 禁止IP访问
2017-06-13 00:00
253 查看
我们在使用的时候会遇到很多的恶意IP攻击,这个时候就要用到Nginx 禁止IP访问了。下面我们就先看看Nginx的默认虚拟主机在用户通过IP访问,或者通过未设置的域名访问(比如有人把他自己的域名指向了你的ip)的时 候生效最关键的一点是,在server的设置里面添加这一行:
listen 80 default;
后面的default参数表示这个是默认虚拟主机。
Nginx 禁止IP访问这个设置非常有用。
比如别人通过ip或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500.目前国内很多机房都要求网站主关闭空主机头,防止未备案的域名指向过来造成麻烦。就可以这样设置:
也可以把这些流量收集起来,导入到自己的网站,只要做以下跳转设置就可以:
按照如上设置后,确实不能通过IP访问服务器了,但是在应该用中出现当server_name后跟多个域名时,其中一个域名怎么都无法访问,设置如下:
没更改之前,通过server_name 中的www.example.com example.com均可访问服务器,加入Nginx 禁止IP访问的设置后,通过example.com无法访问服务器了,www.example.com可以访问,用 Nginx -t 检测配置文件会提示warning:
最后通过在listen 80 default;后再加server_name _;解决,形式如下:
#禁止IP访问
这样,通过example.com就能访问服务器了。
listen 80 default;
后面的default参数表示这个是默认虚拟主机。
Nginx 禁止IP访问这个设置非常有用。
比如别人通过ip或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500.目前国内很多机房都要求网站主关闭空主机头,防止未备案的域名指向过来造成麻烦。就可以这样设置:
server { listen 80 default; return 500; }
也可以把这些流量收集起来,导入到自己的网站,只要做以下跳转设置就可以:
server { listen 80 default; rewrite ^(.*) http://www.example.com permanent; }
按照如上设置后,确实不能通过IP访问服务器了,但是在应该用中出现当server_name后跟多个域名时,其中一个域名怎么都无法访问,设置如下:
server { listen 80; server_name www.example.com example.com }
没更改之前,通过server_name 中的www.example.com example.com均可访问服务器,加入Nginx 禁止IP访问的设置后,通过example.com无法访问服务器了,www.example.com可以访问,用 Nginx -t 检测配置文件会提示warning:
[warn]: conflicting server name “example.com” on 0.0.0.0:80, ignored the configuration file /usr/local/Nginx/conf/ Nginx.conf syntax is ok configuration file /usr/local/Nginx/conf/Nginx. conf test is successful
最后通过在listen 80 default;后再加server_name _;解决,形式如下:
#禁止IP访问
server { listen 80 default; server_name _; server_name www.example.com example.com return 500; }
这样,通过example.com就能访问服务器了。
相关文章推荐
- nginx禁止某个IP访问站点的设置方法
- [转载]nginx简单防盗链及设置默认虚拟主机(禁止通过IP访问)
- 禁止IP访问网站的多种方法分享(linux,php,nginx,apache)
- Nginx 禁止IP访问 只允许域名访问
- Nginx 安装配置 禁止使用IP访问 rewrite重写 别名设置 日志轮询
- web服务禁止 ip 直接访问: apache/httpd/nginx
- Nginx网站使用CDN之后禁止用户真实IP访问的方法
- nginx上设置禁止通过ip访问服务器,只能通过域名访问
- 设置nginx禁止IP直接访问,只能通过指定的域名访问
- nginx 禁止通过ip访问站点
- nginx配置实例:只允许内部IP访问 禁止外部访问
- Nginx 禁止IP访问 (实践过)
- Nginx 禁止IP访问 只允许域名访问
- nginx 禁止ip直接访问web服务器
- nginx 禁止某个IP访问
- nginx上设置禁止通过ip访问服务器,只能通过域名访问
- Nginx禁止通过IP,未绑定域名访问服务器
- nginx 禁止ip访问
- Nginx禁止ip访问或IP网段访问方法
- nginx下禁止某些ip访问web服务器的配置