8

Nginx修改响应内容模块(sub_filter模块)

 3 years ago
source link: https://www.leixuesong.com/3844
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx修改响应内容模块(sub_filter模块)

Linux Nginx服务器 2020年6月28日

sub_filter模块是一个Nginx内置的过滤器模块,主要用来修改网站响应内容中的字符串。sub_filter模块指令可在nginx.conf的http, server, location三个位置配置使用。当我们使用代理的时候,静态资源可能不在代理服务器上,那么我们就需要在响应的时候修改这些地址。我们也可以在这个时候过滤和替换一些服务器返回的数据。下面雷雪松给大家讲解下Nginx替换响应内容模块(sub_filter模块)。

1、安装sub_filter模块。如果已经安装Nginx,新增sub_filter模块。可以看一下这个文章《Nginx编译安装模块(非重装)》

[root@nginx ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@nginx ~]# tar -zxvf nginx-1.18.0.tar.gz
[root@nginx ~]# cd nginx-1.18.0
[root@nginx ~]# ./configure --with-http_sub_module && make && make install

2、sub_filter 指令
sub_filter string replacement。将string替换成replacement,不区分大小写。
sub_filter_last_modified on | off。默认:off,防止缓存。是否在Response header中写入Last-Modified,控制缓存。
sub_filter_once on | off。默认: on,只执行一次。sub_filter指令是执行一次,还是重复执行。
sub_filter_types mime-type …。默认: text/html。指定类型的MINE TYPE,如果所有类型,则使用:*。

3、sub_filter指令具体例子:

1
2
3
4
5
6
#将所有文件中的/js/替换为www.leixuesong.com/js/
location /leixuesong {
sub_filter_once off;
sub_filter_types *;
sub_filter /js/ www.leixuesong.com/js/;
}

来源:Nginx修改响应内容模块(sub_filter模块)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK