Nginx的geo模块使用

Nginx geo
做IT的不管用没用过,应该都听过nginx吧,今天就给大家分享nginx的geo模块。
大家遇到了黑客攻击都是怎么解决的呢?我们可以在公有云安全组中限制黑客ip,在服务器防火墙中添加规则,其实我们也可以使用nginx做防护,今天就分享给大家,其实配置还是很简单的,但是希望大家可以做到学以致用,更重要的是举一反三。
修改nginx配置文件
在http端添加如下内容:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
geo $remote_addr $ip_whitelist {
default 0;
include ip.conf; #ip黑名单
}
server {
listen 80;
server_name localhost;
location / {
if ($ip_whitelist = 1) {
return 403;
}
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Ip.conf内容如下(一行一个ip):
192.168.51.148 1;
此时,ip为192.168.51.148将不能正常访问服务,而是返回403,具体的返回页面大家可以定制。
其实配置还是很简单的,不知道大家是否学会;其实我们公司目前使用的并不是geo的这个功能,而是用geo模块,根据不同ip做了分发,比如说公司项目刚上线,我们只希望很少的一部分用户可以使用新系统,而其他的用户则使用旧系统,也是使用geo来实现的。具体的配置过程就不发给大家了,大家可以自己尝试一下!