在CentOS 6.9 x86_64的nginx 1.12.2上开启ngx_http_geo_module模块实录
2018-01-05 13:08
761 查看
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地址,但也可以从其他变量获得。如
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:使用以地址段的形式定义地址,这个参数必须放在首位。为了加速装载地址库,地址应按升序定义。
适用实例
上面的例子几乎都是官网说明例子。下面举例说明便于理解该指令的用法。
完整的nginx.conf如下:
https://github.com/taoyunxing/github_test/blob/master/nginx.conf
1. 使用默认变量也就是$remote_addr
1
默北
2. 使用指定变量
curl 127.0.0.1:8081/hello2?boy=8.8.8.8
2
8.8.8.8
3. 匹配原则
32
127.0.0.1
curl 127.0.0.1:8081/hello3?boy=127.0.0.12
24
127.0.0.12
4. range用法
beijing|cmcc
geo $arg_ip $address 表示使用客户端请求的ip参数值当做ip,如http://127.0.0.1:8081/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.自定义常量
参考文献
[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].http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html
这个模块提供了一个非常好用的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使用能最精确匹配客户端地址的值。
适用实例
上面的例子几乎都是官网说明例子。下面举例说明便于理解该指令的用法。
完整的nginx.conf如下:
https://github.com/taoyunxing/github_test/blob/master/nginx.conf
1. 使用默认变量也就是$remote_addr
http { geo $ttlsa_com { default 0; 127.0.0.1 1; } server { listen 8080; server_name localhost; location /hello { default_type text/plain; echo $ttlsa_com; echo $arg_boy; } } }# curl 127.0.0.1:8081/hello?boy=默北
1
默北
2. 使用指定变量
http { geo $arg_boy $tao_com { default 0; 127.0.0.1 1; 8.8.8.8 2; } server { listen 8081; server_name localhost; location /hello2 { default_type text/plain; echo $tao_com; echo $arg_boy; } } }
curl 127.0.0.1:8081/hello2?boy=8.8.8.8
2
8.8.8.8
3. 匹配原则
http { geo $arg_boy $tao2_com { default 0; 127.0.0.1/24 24; 127.0.0.1/32 32; 8.8.8.8 2; } server { listen 8081; server_name localhost; location /hello3 { default_type text/plain; echo $tao2_com; echo $arg_boy; } } }curl 127.0.0.1:8081/hello3?boy=127.0.0.1
32
127.0.0.1
curl 127.0.0.1:8081/hello3?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; 127.0.0.0-223.255.254.255 beijing|cmcc; } server { listen 80; server_name localhost; location /iptool { default_type text/plain; echo $address; } } }curl "http://127.0.0.1:8081/iptool?ip=144.144.144.144"
beijing|cmcc
geo $arg_ip $address 表示使用客户端请求的ip参数值当做ip,如http://127.0.0.1:8081/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].http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html
相关文章推荐
- 在CentOS 6.9 x86_64的nginx 1.12.2上开启ngx_http_empty_gif_module模块实录
- 在CentOS 6.9 x86_64上开启nginx 1.12.2的stub_status模块(ngx_http_stub_status_module)监控
- 在CentOS 6.9 x86_64的nginx 1.12.2上开启标准模块ngx_http_map_module实录
- 在CentOS 6.9 x86_64的nginx 1.12.2上开启标准模块ngx_http_auth_request_module实录
- 在CentOS 6.9 x86_64的nginx 1.12.2上开启ngx_req_status模块实录
- 在CentOS 6.9 x86_64的nginx 1.12.2上安装第三方模块set-misc-nginx-module实录
- 在CentOS 6.9 x86_64上开启nginx 1.12.2的proxy_cache缓存配置
- 在CentOS 6.9 x86_64的nginx 1.12.2上开启echo-nginx-module模块实录
- 在CentOS 6.8 x86_64的nginx 1.10.3上开启http2功能
- 在CentOS 6.9 x86_64上安装nginx 1.12.2
- nginx外部模块编译安装之ngx_http_accesskey_module防盗链
- Windows 下 Nginx 添加 ngx_http_sub_module 模块
- Nginx 负载均衡模块 ngx_http_upstream_module 详述
- 022_nginx常用模块之ngx_http_upstream_check_module
- Nginx 负载均衡模块 ngx_http_upstream_module 详述
- nginx教程第七篇:ngx_http_core_module模块提供的变量
- Nginx 负载均衡模块 ngx_http_upstream_module 详述
- Nginx模块 ngx_http_limit_req_module 限制请求速率
- Nginx最新模块—ngx_http_mirror_module分析
- nginx安全下载模块ngx_http_secure_link_module