Nginx ngx_http_addition_module 404问题

本文介绍了一种在使用Nginx的ngx_http_addition_module模块时遇到的404错误解决方案。通过调整配置文件中add_after_body指令后的文件路径,成功解决了调试过程中的404错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

过滤模块可以在响应体的前面或后面附加上数据

404问题

在重新编译了nginx后,增加了ngx_http_addition_module 模块,用来在response数据后面增加其他自定义信息。但是在调试时一直报404 error,经调试分析

配置如下:
location / {
              root   html;
              index  html;
              index  test1.html;
              add_after_body  test2.html;
          }

在配置时,add_after_body 后面的文件路径需要加上/即可。

add_after_body  test2.html;
修改为
add_after_body  /test2.html;
### 配置 Nginx 使用 `ngx_stream_geoip2_module` 模块 #### 安装依赖项 为了成功编译和运行 `ngx_stream_geoip2_module`,需要先安装必要的开发工具和库文件。这通常包括 MaxMind 的数据库支持库。 ```bash yum install libmaxminddb-devel -y ``` #### 下载并准备模块源码 获取最新的 `ngx_http_geoip2_module` 和 `ngx_stream_geoip2_module` 源代码: ```bash cd /usr/local/src/ git clone https://github.com/leev/ngx_http_geoip2_module.git ``` 注意:虽然这里提到的是 `ngx_http_geoip2_module`,但是 GitHub 上该项目也包含了流模式的支持即 `ngx_stream_geoip2_module`. #### 编译安装 NGINX 并添加自定义模块 在编译 NGINX 之前,确保已经下载了官方发布的稳定版本,并将其解压缩到合适的位置。接着,在配置阶段通过命令行参数指定要加入的新功能模块路径。 ```bash tar -zxvf nginx-1.20.1.tar.gz cd nginx-1.20.1/ ./configure \ --with-stream \ --add-dynamic-module=../ngx_http_geoip2_module \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_module=dynamic \ --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_degradation_module \ --with-http_slice_module \ --with-http_stub_status_module \ --with-mail=static \ --with-mail_ssl_module \ --with-pcre \ --with-pcre-jit \ --with-stream=dynamic \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-debug make && make install ``` 上述过程会将 `ngx_stream_geoip2_module` 动态加载至 NGINX 中[^3]。 #### 获取 IP 地理位置数据集 (MaxMind DB 文件) 从官方网站或其他可信资源处获得最新版的 GeoLite2 数据包,并按照说明放置于适当位置供 NGINX 访问。 ```bash mkdir -p /etc/nginx/geoip cd /tmp wget https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=YOUR_LICENSE_KEY&suffix=tar.gz tar xf geoip_download\?edition_id\=GeoLite2-City* -C /etc/nginx/geoip --strip-components=1 ``` 请注意替换 URL 参数中的 `YOUR_LICENSE_KEY` 为你自己的许可证密钥[^2]。 #### 修改 NGINX 配置以启用 Stream GeoIP 支持 编辑 `/etc/nginx/nginx.conf` 或者相应的虚拟主机配置文件来引入新的指令用于处理 TCP/UDP 请求的同时解析客户端的真实地理位置信息。 ```nginx stream { upstream backend { server 192.168.1.1:80; } map $remote_addr $country_code { default "unknown"; include /etc/nginx/geoip/country.map; # 自定义映射表可选 } log_format stream_combined '$remote_addr [$time_local] ' '"$protocol $status" $bytes_sent "$upstream_addr"' ' country=$country_code'; access_log /var/log/nginx/stream-access.log stream_combined; server { listen 12345; proxy_pass backend; geoip2 /etc/nginx/geoip/GeoLite2-City.mmdb { auto_load on; $city city.names.en; $country_code country.iso_code; } set $allowed_countries "CN US CA"; # 允许访问的国家列表 if ($country_code !~* ^($allowed_countries)$ ) { return 503; } } } ``` 此段配置实现了基于地理区域限制服务可用性的基本逻辑,仅当请求来自特定几个允许的国家时才转发流量给后端服务器;否则返回 HTTP 503 错误响应表示暂时不可用[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值