在CentOS 6.9 x86_64的nginx 1.12.2上开启ngx_http_geo_module模块实录

前端之家收集整理的这篇文章主要介绍了在CentOS 6.9 x86_64的nginx 1.12.2上开启ngx_http_geo_module模块实录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ngx_http_geo_module模块,默认情况下,Nginx会加载,除非人为的 --without-http_geo_module。
这个模块提供了一个非常好用的geo指令,可以用它来创建变量,诞生其值依赖于客户端IP地址。

ngx_http_geo_module
模块官网地址
http://Nginx.org/en/docs/http/ngx_http_geo_module.html

geo指令
语法: geo [$address] $variable { ... }
默认值: —
配置段: http
定义从指定的变量获取客户端的IP地址。默认情况下,Nginx从$remote_addr变量取得客户端IP地址,但也可以从其他变量获得。如
geo $remote_addr $geo {
default 0;
127.0.0.1 1;
}
geo $arg_ttlsa_com $geo {
default 0;
127.0.0.1 1;
}
如果该变量的值不能代表一个合法的IP地址,那么Nginx将使用地址“255.255.255.255”。
Nginx通过CIDR或者地址段来描述地址,支持下面几个参数:
delete:删除指定的网络
default:如果客户端地址不能匹配任意一个定义的地址,Nginx将使用此值。 如果使用CIDR,可以用“0.0.0.0/0”代替default。
include:包含一个定义地址和值的文件,可以包含多个。
proxy:定义可信地址。 如果请求来自可信地址,Nginx将使用其“X-Forwarded-For”头来获得地址。 相对于普通地址,可信地址是顺序检测的。
proxy_recursive:开启递归查找地址。 如果关闭递归查找,在客户端地址与某个可信地址匹配时,Nginx将使用“X-Forwarded-For”中的最后一个地址来代替原始客户端地址。如果开启递归查找,在客户端地址与某个可信地址匹配时,Nginx将使用“X-Forwarded-For”中最后一个与所有可信地址都不匹配的地址来代替原始客户端地址。
ranges:使用以地址段的形式定义地址,这个参数必须放在首位。为了加速装载地址库,地址应按升序定义。

geo $country {
default ZZ;
include conf/geo.conf;
delete 127.0.0.0/16;
proxy 192.168.100.0/24;
proxy 2001:0db8::/32;

127.0.0.0/24 US;
127.0.0.1/32 RU;
10.1.0.0/16 RU;
192.168.1.0/24 UK;
}
vim conf/geo.conf
10.2.0.0/16 RU;
192.168.2.0/24 RU;

地址段例子:
geo $country {
ranges;
default ZZ;
127.0.0.0-127.0.0.0 US;
127.0.0.1-127.0.0.1 RU;
127.0.0.1-127.0.0.255 US;
10.1.0.0-10.1.255.255 RU;
192.168.1.0-192.168.1.255 UK;
}

遵循最精确匹配原则,即Nginx使用能最精确匹配客户端地址的值。

适用实例
上面的例子几乎都是官网说明例子。下面举例说明便于理解该指令的用法
1. 使用默认变量也就是$remote_addr
http {
geo $ttlsa_com {
default 0;
127.0.0.1 1;
}
server {
listen 8080;
server_name test.ttlsa.com;

location /hello {
default_type text/plain;
echo $ttlsa_com;
echo $arg_boy;
}
}
}
# curl 127.0.0.1:8080/hello?boy=默北
1
默北

2. 使用指定变量
http {
geo $arg_boy $ttlsa_com {
default 0;
127.0.0.1 1;
8.8.8.8 2;
}
server {
listen 8080;
server_name test.ttlsa.com;

location /hello {
default_type text/plain;
echo $ttlsa_com;
echo $arg_boy;
}
}
}
# curl 127.0.0.1:8080/hello?boy=8.8.8.8
2
8.8.8.8

3. 匹配原则
http {
geo $arg_boy $ttlsa_com {
default 0;
127.0.0.1/24 24;
127.0.0.1/32 32;
8.8.8.8 2;
}
server {
listen 8080;
server_name test.ttlsa.com;

location /hello {
default_type text/plain;
echo $ttlsa_com;
echo $arg_boy;
}
}
}
# curl 127.0.0.1:8080/hello?boy=127.0.0.1
32
127.0.0.1
# curl 127.0.0.1:8080/hello?boy=127.0.0.12
24
127.0.0.12

4. range用法
http {
include mime.types;
default_type application/octet-stream;
geo $arg_ip $address {
ranges;
default no; # note that ranges precedes all other directives
.0.0.0-126.255.255.255 abroad|abroad;
.0.0.0-223.255.254.255 beijing|cmcc;
}
server {
listen 172.16.108.112:80;
server_name localhost;
location /iptool {
default_type text/plain;
echo $address;
}
}
}
curl "http://172.16.108.112/iptool?ip=144.144.144.144"
beijing|cmcc


geo $arg_ip $address 表示使用客户端请求的ip参数值当做ip,如http://172.16.108.112/iptool?ip=144.144.144.144,意思是使用144.144.144.144当做ip过geo这个模块,查询属于127.0.0.0-223.255.254.255这个网段,因此就将这个网段对应的值beijing|cmcc赋值给$address。


geo指令主要是根据IP来对变量进行赋值的。因此geo块下只能定义IP或网络段,否则会报错“Nginx: [emerg] invalid network”。


5.自定义常量
geo $dollar {
default "$";
}

server {
listen 8081;
server_name localhost;


location /test2 {
echo "This is a dollar sign: $dollar";
}
}
curl -v 'http://localhost:8081/test2'

参考文献 [1].http://Nginx.org/en/docs/http/ngx_http_geo_module.html [2].http://ju.outofmemory.cn/entry/96242 [3].http://www.ttlsa.com/Nginx/using-Nginx-geo-method/ [4]. 原文链接:https://www.f2er.com/centos/374914.html

猜你在找的CentOS相关文章