美文网首页
Nginx 请求内容替换ngx_http_sub_module

Nginx 请求内容替换ngx_http_sub_module

作者: EdgeE | 来源:发表于2023-08-09 17:39 被阅读0次

ngx_http_sub_module模块是一个过滤器,可以替换请求Response中的内容。在nginx编译时加上--with-http_sub_module即可使用。

./configure --with-http_stub_status_module --with-http_sub_module && make && make install

常用指令:

  • sub_filter指令: sub_filter string(原字符串) replacement(用于替换的字符串);

用于设置需要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是 新的字符串,它里面可以带变量。

  • sub_filter_last_modified指令: sub_filter_last_modified on | off;

用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用,默认值是off;

  • sub_filter_once指令:sub_filter_once on | off;

用于设置字符串替换次数,默认只替换一次。如果是on,默认只替换第一次匹配到的到字 符,如果是off,那么所有匹配到的字符都会被替换;

  • sub_filter_types指令:sub_filter_types *

用于指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有的;

说明:以上指令可在nginx.conf的http, server, location三个位置配置使用;
注意:替换不区分大小写、支持中文

配置实例:

server {
        listen  81;
        server_name ipc1;
        access_log  logs/access.log  main;
        location / {
                proxy_pass http://10.0.8.112/;
                sub_filter 'http://10.0.8.112' 'http://10.0.8.181:81';
                sub_filter_types *;
                sub_filter_once off;
        }
}

该实例是将所有请求返回的body中 ‘http://10.0.8.112’替换为‘http://10.0.8.181:81’

相关文章

网友评论

      本文标题:Nginx 请求内容替换ngx_http_sub_module

      本文链接:https://www.haomeiwen.com/subject/zecapdtx.html