nginx 变量相关的map模块与split_clients模块及geo模块和geoip模块及keepalive介绍

map 模块指令默认编译进nginx的
1
2
3
4
5
6
7
8
9
Syntax: map string $variable { ... }  # 表示string匹配到{}里的值或变量赋值给$variable
Default: —
Context: http
Syntax: map_hash_bucket_size size;
Default: map_hash_bucket_size 32|64|128;
Context: http
Syntax: map_hash_max_size size;
Default: map_hash_max_size 2048;
Context: http

  配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@python vhast]# cat mo.conf
map $http_host $name {
    hostnames;
    default 0;
    ~map\.tao\w+\.org.cn 1;
    *.taohui.org.cn 2;
    map.taohui.tech 3;
    map.taohui.* 4;
    }
map $http_user_agent $mobile {
    default 0;
    "~Opera Mini" 1;
    }
server {
    listen 10001;
    default_type text/plain;
    location /{
        return 200 "$http_host:$name:$mobile\n";
    }
}

  测试

1
2
3
4
5
6
[root@python vhast]# curl -H 'Host: map.tao123.org.cn' 127.0.0.1:10001
map.tao123.org.cn:1:0
[root@python vhast]# curl -H 'Host: map.taohui.pub' 127.0.0.1:10001
map.taohui.pub:4:0
[root@python vhast]# curl -H 'Host: map.taohui.tech' 127.0.0.1:10001
map.taohui.tech:3:0

 split_clients 指令介绍

 

 

1
2
3
Syntax: split_clients string $variable { ... }
Default: —
Context: http

  

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
split_clients "${http_testcli}" $variant {
    0.51% .one;
    20.0% .two;
    50.5% .three;
    #40% .four;
    * "";
}
server {
    server_name split_clients.com;
    default_type text/plain;
    location /{
        return 200 'ABtest$variant\n';
    }
}

  测试

1
2
3
4
5
6
7
8
[root@python vhast]# curl -H 'testcli: 456152gy323v' split_clients.com/
ABtest.two
[root@python vhast]# curl -H 'testcli: 4561238852ohugy323v' split_clients.com/
ABtest.three
[root@python vhast]# curl -H 'testcli: 45' split_clients.com/
ABtest.two
[root@python vhast]# curl -H 'testcli: 45ftdr' split_clients.com/
ABtest

  geo模块:根据IP创建新变量

 

指令

1
2
3
Syntax: geo [$address] $variable { ... }
Default: —
Context: http

 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
        server_name split_clients.com;
        default_type text/plain;
        location /{
                return 200 '$country\n';
        }
}
 
geo $country {
        default ZZ;
        #include conf/geo.conf;
        proxy 192.168.183.4;
        127.0.0.0/24 US;
        127.0.0.1/32 RU;
        10.1.0.0/16 RU;
        192.168.1.0/24 UK;
        }

  测试

1
2
3
4
5
6
7
[root@python vhast]# curl -H 'X-Forwarded-For: 10.1.0.0,127.0.0.2'
split_clients.com
US
[root@python vhast]# curl -H 'X-Forwarded-For: 10.1.0.0,127.0.0.1' split_clients.com
RU
[root@python vhast]# curl -H 'X-Forwarded-For: 10.1.0.0,127.0.0.1,1.2.3.4' split_clients.com
ZZ

  使用变量查找用户地理位置geoip模块;默认未启用此功能;--with-http_geoip_module 编译进去

安装模块依赖的库

1
yum -y install GeoIP GeoIP-devel GeoIP-data

    编译安装依赖库

1
2
3
4
5
wget https://github.com/maxmind/geoip-api-c/releases/download/v1.6.12/GeoIP-1.6.12.tar.gz
tar xf GeoIP-1.6.12.tar.gz
cd GeoIP-1.6.12/
./configure
make && make install

 

重新编译nginx

1
2
3
4
5
[root@python nginx-1.15.9]# ./configure --prefix=/data/web --sbin-path=/usr/bin --user=nginx --group=nginx --with-http_stub_status_module --with-http_auth_request_module --with-http_sub_module --add-module=/root/nginx-http-concat --with-http_addition_module --with-http_
secure_link_module --with-http_geoip_module
[root@python nginx-1.15.9]# make
[root@python nginx-1.15.9]# mv /usr/bin/nginx{,.07.13.16.31}
[root@python nginx-1.15.9]# mv objs/nginx /usr/bin/

  指令介绍

1
2
3
4
5
6
Syntax: geoip_country file;#指定国家的地址文件
Default: —
Context: http
Syntax: geoip_proxy address | CIDR;  #信任的IP地址
Default: —
Context: http

  变量介绍

 

geoip_city 提供的变量

 配置在http字段配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
    server_name split_clients.com;
    default_type text/plain;
    location /{
        return 200 'country:$geoip_country_code,$geoip_country_code3,$geoip_country_name
country from city:$geoip_city_continent_code,$geoip_city_country_code3,$geoip_city_country_name\n';
    }
}
 
geo $country {
    default ZZ;
    #include conf/geo.conf;
    proxy 192.168.183.4;
    127.0.0.0/24 US;
    127.0.0.1/32 RU;
    10.1.0.0/16 RU;
    192.168.1.0/24 UK;
    }
geoip_country  /usr/share/GeoIP/GeoIP-initial.dat;
geoip_city   /usr/share/GeoIP/GeoIPCity-initial.dat;
geoip_proxy 192.168.183.4/32;
geoip_proxy_recursive on;

  测试

1
2
3
4
5
6
7
8
9
[root@python vhast]# curl -H 'X-Forwarded-For: 104.248.224.193,121.8.98.197' split_clients.com
country:CN,CHN,China
country from city:AS,CHN,China
[root@python vhast]# curl -H 'X-Forwarded-For: 104.248.224.193' split_clients.com
country:US,USA,United States
country from city:NA,USA,United States
[root@python vhast]# curl -H 'X-Forwarded-For: 77.48.21.58' split_clients.com
country:CZ,CZE,Czech Republic
country from city:EU,CZE,Czech Republic

 keepalive模块

指令介绍

1
2
3
4
5
6
7
8
9
Syntax: keepalive_disable none | browser ...;#对某些浏览器不适应此功能
Default: keepalive_disable msie6;
Context: http, server, location
Syntax: keepalive_requests number;   #表示在一个http最多执行多少个请求默认100
Default: keepalive_requests 100;
Context: http, server, location
Syntax: keepalive_timeout timeout [header_timeout];  跟两个时间,第一个是当请求完第一次后多长时间没请求我就断开连接默认75秒;第二个表示向浏览器返回这个连接保持的时间
Default: keepalive_timeout 75s;
Context: http, server, location

  

 

  

 

 

 

 

 

 

posted @   烟雨楼台,行云流水  阅读(892)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示