参考:http://nginx.org/en/docs/http/ngx_http_geo_module.html

    http_geo_module,不需要在nginx编译时指定,是默认的。如果不需要,则要在configure时用without来指定
    nginx -V 可以查看configure的配置

    1. # nginx -V
    2. nginx version: nginx/1.20.1
    3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
    4. built with OpenSSL 1.1.1i 8 Dec 2020
    5. TLS SNI support enabled
    6. configure arguments: --prefix=/usr/local/nginx --sbin-path=sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx/nginx.pid --lock-path=/run/nginx/nginx.lock --http-client-body-temp-path=/data/nginxcache/client_temp --http-proxy-temp-path=/data/nginxcache/proxy_temp --http-fastcgi-temp-path=/data/nginxcache/fastcgi_temp --http-uwsgi-temp-path=/data/nginxcache/uwsgi_temp --http-scgi-temp-path=/data/nginxcache/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 --with-openssl=/usr/local/openssl

    geo可以把ip映射到不同的值,比如1、2、EN、CN等,最开始估计是用来对ip进行地理位置分类的,但可以用来表示不同区域也可以。

    默认从$remote_addr取值,根据定义的映射表得到映射的值,值存在指定的变量中
    然后你可以根据变量的值来做不同的处理。

    举例:

    1. geo $iptype {
    2. default 0;
    3. include conf/geo.conf;
    4. 127.0.0.1 2;
    5. 192.168.1.0/24 TRUST;
    6. 10.1.0.0/16 UNTRUST;
    7. ::1 2;
    8. 2001:0db8::/32 1;
    9. }

    这一段放在http{}里,然后就可以在location里引用

    1. location /aaa {
    2. proxy_pass https://server1;
    3. if ( $iptype != "TRUST" ){
    4. rewrite .* https://error.xxx.cn redirect;
    5. }
    6. }
    7. #server1是前面定义的upstream