Nginx http_sub_module 替换网站响应内容

网友投稿 835 2022-09-27

Nginx http_sub_module 替换网站响应内容

Nginx  http_sub_module 替换网站响应内容

sub_module模块

The ​​ngx_is a filter that modifies a response by replacing one specified string by another.

This module is not built by default, it should be enabled with the ​​--with-parameter.

ngx_模块是一个过滤器,它修改网站响应内容中的字符串,比如你想把响应内容中的‘test’全部替换成‘mytest’,这个模块已经内置在Nginx中,但是默认未安装,需要安装需要加上配置参数:– with-~]# /usr/local/nginx/sbin/nginx -Vnginx version: nginx/1.16.1built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) configure arguments: --prefix=/usr/local/nginx --with---add-module=../nginx-src]# cd /usr/src/nginx-1.16.1[root@nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --with---add-module=../nginx---with-nginx-1.16.1]# make && make install[root@nginx-1.16.1]# make upgrade

语法: sub_filter string replacement;默认值: —配置段: server, location设置需要使用说明字符串替换说明字符串.string 是要被替换的字符串, replacement 是新的字符串,它里面可以带变量。语法: sub_filter_last_modified on | off;默认值: sub_filter_last_modified off;配置段: server, location这个指令在 nginx 1.5.1 中添加,我这个版本没有,可以忽略掉.Allows preserving the “ Last-Modified” header field from the original response during replacement to facilitate response caching.By default, the header field is removed as contents of the response are modified during processing.语法: sub_filter_once on | off;默认值: sub_filter_once on;配置段: server, location字符串替换一次还是多次替换,默认替换一次,例如你要替换响应内容中的tets为mytest,如果有多个test 出现,那么只会替换第一个,如果 off,那么所有的test都会被替换语法: sub_filter_types mime-type …;默认值: sub_filter_types text/html;配置段: server, location指定需要被替换的 MIME 类型,默认为“ text/html”,如果制定为*,那么所有的

Nginx 替换字符串实例

server { listen 80; server_name test.com; charset utf-8; root html; location /{ sub_filter test 'mytest'; sub_filter_once on; } }[root@~]# echo "welcom to TEST" > /usr/local/nginx/html/sub1.html[root@~]# cat /usr/local/nginx/html/sub1.htmlwelcom to TEST[root@~]# curl test.com/sub1.htmlwelcom to mytes我们可以看到它替换是不区分大小写的,而且test只被替换了一次。我把 sub_filter_once on 改成 off 试试。[root@~]# echo "welcom to TEST website" >> /usr/local/nginx/html/sub1.html[root@~]# cat /usr/local/nginx/html/sub1.htmlwelcom to TESTwelcom to TEST website[root@~]# curl test.com/sub1.htmlwelcom to mytestwelcom to mytest website我们可以看到test都被替换掉了!

总结

这个 nginx 替换响应内容的模块安装使用尤为简单,应用的地方相对较少,在 nginx 中也是一个可选模块。假如站点出现什么敏感字,想修改很耗时间,不妨试试这个模块.或者想临时在站点中加上一个通用 js 或者 css 之类的文件,也可以使用这个模块.至于要在哪里,大家看看自己的需求。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Servlet文件的上传与下载详解
下一篇:Keepalived 配置详解与主备模式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~