nginx-http-footer-filter 是一个第三方 Nginx 模块,用于在响应的 HTML 页面底部添加自定义内容(如版权信息、统计代码等)。以下是该模块的详细配置方法:
首先需要编译安装包含此模块的 Nginx:
# 下载模块
git clone https://github.com/alibaba/nginx-http-footer-filter.git
# 编译Nginx时添加模块
./configure --add-module=/path/to/nginx-http-footer-filter
make
make install
在 Nginx 配置文件中添加以下内容:
http {
footer_types "text/html";
footer "$host_comment";
footer_hostnames "example.com=Example Site;*.example.org=Test Site";
}
footer_types
: 指定哪些 MIME 类型的响应需要添加页脚
footer_types "text/html" "text/plain";
footer
: 设置要添加的页脚内容
footer "<!-- Powered by Nginx -->";
footer_hostnames
: 根据不同域名设置不同的页脚
footer_hostnames "example.com=Example Site;test.com=Test Site";
使用变量动态生成页脚:
footer "<!-- Served by $server_name at $time_local -->";
排除特定URL:
location /admin/ {
footer off;
}
只对特定状态码添加页脚:
footer_statuses 200 301 302;
http {
footer_types "text/html";
footer "<!-- Page generated at $time_local -->\n<!-- Powered by Nginx -->";
footer_hostnames "example.com=Example Site;test.com=Test Site";
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
footer "<!-- Custom footer for $host -->";
}
location /api/ {
footer off; # 不添加页脚
}
}
}
配置完成后,可以:
重载Nginx配置:
nginx -s reload
使用curl测试:
curl -i http://example.com
检查响应HTML底部是否包含配置的页脚内容
如需更复杂的内容注入,可以考虑结合Nginx的sub_filter模块使用。