首页 > Linux操作系统 > Linux操作系统 > Nginx实现对响应体内容的替换
该模块能够搜索和替换Nginx响应体中的文本内容,这个模块在默认安装Nginx时是不会安装的,因此,要想使用该模块,那么需要在./configure时添加--with-http_sub_module option选项。
|
location / { sub_filter ' sub_filter_once on; } |
该模块提供了3条指令。
指令名称:sub_filter
功 能:该指令用于在Nginx的响应中替代一些文本,即将原有的“text”替换为现有的“substitution”,而不依赖于源数据。内容匹配对大小写不敏感。替代文本可以包含变量,每一个location中只能使用一种替换规则。
语 法: sub_filter text substitution
默 认 值: none
使用环境: http, server, location
指令名称:sub_filter_once
功 能:如果将该指令设置为off,那么将会允许搜索和替换所有匹配的行,默认情况下仅替换第一个被匹配的行。
语 法: sub_filter_once on|off
默 认 值: sub_filter_once on
使用环境: http, server, location
指令名称:sub_filter_types
功 能:该指令用于指定sub_filter指令应该检测的内容类型。默认只有text/html。
语 法: sub_filter_types mime-type [mime-type ...]
默 认 值: sub_filter_types text/html
使用环境: http, server, location
在Nginx的配置文件中添加以下配置内容:
|
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
sub_filter '' ' sub_filter_once on;
server { listen 80; server_name localhost;
location / { root html; index index.html index.htm; } } …… }
|
这是某年用的最多的一个例子,它将所有的页面在IE浏览器下访问下变为灰色。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27043155/viewspace-732985/ ,如需转载,请注明出处,否则将追究法律责任。