下载Nginx包

Nginx官网下载地址

安装GCC等环境

# 安装gcc,源码编译依赖 gcc 环境
yum -y install gcc-c++

# 安装pcre,pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel

# 安装zlib,zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip
yum install -y zlib zlib-devel

# 安装OpenSSL库
yum install -y openssl openssl-devel

下载Nginx-Geo模块

下载地址

解压到Linux服务器中

例如:/env/nginx_lib/ngx_http_geoip2_module-3.4

下载国家/城市数据库

国家数据库

城市数据库

解压到Linux服务器中

例如:/env/nginx_lib/GeiLite2-Country.mmdb

国家数据库文件名:GeiLite2-Country.mmdb

城市数据库文件名:GeiLite2-City.mmdb

编译安装

# 到下载的Nginx目录下执行命令
./configure --prefix=[Nginx安装目录] --with-http_ssl_module --add-module=[ngx_http_geoip2_module模块文件路径] && make -j 4 && make install

#到安装目录启动Nginx
./nginx

#例如
# ./configure --prefix=/env/nginx --with-http_ssl_module --add-module=/env/nginx_lib/ngx_http_geoip2_module-3.4 && make -j 4 && make install
ChatGPT解释
这行命令是用于配置、编译和安装 Nginx 的。让我们分解一下每个部分的含义:

1. `./configure --prefix=/env/nginx --with-http_ssl_module --add-module=/env/nginx_lib/ngx_http_geoip2_module-3.4`:
   - `./configure`: 这是一个脚本,用于检查系统环境并配置 Nginx 的构建选项。
   - `--prefix=/env/nginx`: 这个选项指定了 Nginx 将被安装到的目录。在这种情况下,Nginx 将被安装到 `/env/nginx` 目录下。
   - `--with-http_ssl_module`: 这个选项启用了 HTTP SSL 模块,使 Nginx 支持 HTTPS。
   - `--add-module=/env/nginx_lib/ngx_http_geoip2_module-3.4`: 这个选项用于添加一个额外的模块,在这里是 `ngx_http_geoip2_module`,该模块用于处理基于 GeoIP2 数据库的地理定位功能。模块的路径是 `/env/nginx_lib/ngx_http_geoip2_module-3.4`。

2. `&& make -j 4`:
   - `&&`: 这是一个逻辑操作符,用于在前一条命令成功后执行下一条命令。
   - `make -j 4`: 这个命令用于并行编译 Nginx 代码。`-j 4` 表示使用 4 个并行线程来加快编译速度。

3. `&& make install`:
   - `make install`: 这个命令用于将编译完成的 Nginx 安装到前面指定的目录 `/ent-test/nginx` 中。

总结来说,这行命令是在配置、编译和安装 Nginx,同时启用了 SSL 支持并添加了 GeoIP2 模块。

限制国家访问

nginx.conf

# 配置到http代码段中,/env/nginx_lib/GeoLite2-Country.mmdb为国家数据库路径
# 配置 GeoIP2 模块,指定使用的 GeoLite2-Country.mmdb 数据库
geoip2 /env/nginx_lib/GeoLite2-Country.mmdb {
    # 每5分钟自动重新加载数据库
    auto_reload 5m;
    # 通过访问者IP获取国家代码,默认值为US(美国)
    $geoip2_data_country_code default=US source=$remote_addr country iso_code;
    # 通过访问者IP获取城市名称,默认值为Toronto(多伦多)
    $geoip2_data_city_name default=Toronto source=$remote_addr city names en;
}

# 定义自定义的日志格式,记录访客的地理位置和请求信息
log_format custom_format_country '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$geoip2_data_country_code"  "$block_country"';

# 配置到server中
# 将日志记录到 logs/host.access.log 文件中,使用自定义日志格式 custom_format_country
access_log  logs/host.access.log  custom_format_country;
# 映射国家代码到阻止标志位
map $geoip2_data_country_code $block_country {
    # 默认值为0,不阻止
    default 0;
    # 如果国家代码为CN(中国),则设置阻止标志位为1
    "CN" 1;
}
# 如果阻止标志位为1,则返回403 Forbidden,阻止访问
if ($block_country = 1) {
    return 403;
}    

限制区域访问

nginx.conf

# 配置到http代码段中,/env/nginx_lib/GeoLite2-City.mmdb为国家数据库路径
# 配置 GeoIP2 模块,指定使用的 GeoLite2-City.mmdb 数据库
geoip2 /env/nginx_lib/GeoLite2-City.mmdb {
    # 每5分钟自动重新加载数据库
    auto_reload 5m;
    # 通过访问者IP获取国家代码,默认值为US(美国)
    $geoip2_data_country_code default=US source=$remote_addr country iso_code;
    # 通过访问者IP获取城市名称,默认值为Toronto(多伦多)
    $geoip2_data_city_name default=Toronto source=$remote_addr city names en;
}

# 定义自定义的日志格式,记录访客的地理位置和请求信息
log_format custom_format_city '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$geoip2_data_city_code"  "$block_city"';

# 配置到server中
# 将日志记录到 logs/host.access.log 文件中,使用自定义日志格式 custom_format_city
access_log  logs/host.access.log  custom_format_city;
# 映射城市代码到阻止标志位
map $geoip2_data_city_code $block_city {
    # 默认值为0,不阻止
    default 0;
    # 如果城市代码为珠海(珠海),则设置阻止标志位为1
    "Zhuhai" 1;
}
# 如果阻止标志位为1,则返回403 Forbidden,阻止访问
if ($block_country = 1) {
    return 403;
}    

测试

查找日志文件命令:find / -name host.access.log

定位到日志目录并访问:tail -f host.access.log

访问nginx.conf配置文件server代码段的端口并查看日志文件,如果在配置文件中配置了CN为1,则使用中国地区IP地址访问Nginx则返回403
最后修改:2024 年 08 月 12 日
如果觉得我的文章对你有用,请随意赞赏