ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串,好比你想把响应内容中的‘ttlsa’所有替换成‘运维生存时间’,这个模块已经内置在nginx中,可是默认未安装,须要安装须要加上配置参数:--with-http_sub_modulejavascript
1css 2html 3java 4nginx 5运维 6curl |
# wget http://nginx.org/download/nginx-1.4.2.tar.gz测试 # tar -xzvf nginx-1.4.2.tar.gz网站 # cd nginx-1.4.2ui # --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module --with-http_sub_module # make # make install |
若是你已经安装了nginx,只须要额外追加这个模块,请看如何安装nginx第三方模块
有时候,咱们须要单独安装nginx,来处理大量的下载请求。单独在Centos5安装nginx遇到的rewrite和HTTP cache错误解决办法:
wget http://nginx.org/download/nginx-0.8.33.tar.gz
tar -zxvf nginx-0.8.33.tar.gz
cd nginx-0.8.33
./configure --prefix=/usr/local/nginx
安装Nginx时报错
./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel解决问题
yum -y install pcre-devel
错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
解决办法:
yum -y install openssl openssl-devel
总结:
yum -y install pcre-devel openssl openssl-devel
./configure --prefix=/usr/local/nginx
make
make install
一切搞定
语法: sub_filter string replacement;
默认值: —
配置段: http, server, location
设置须要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是新的字符串,它里面能够带变量。
语法: sub_filter_last_modified on | off;
默认值: sub_filter_last_modified off;
配置段: http, 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;
配置段: http, server, location
字符串替换一次仍是屡次替换,默认替换一次,例如你要替换响应内容中的ttlsa为运维生存时间,若是有多个ttlsa出现,那么只会替换第一个,若是off,那么全部的ttlsa都会 被替换
语法: sub_filter_types mime-type ...;
默认值: sub_filter_types text/html;
配置段: http, server, location
指定须要被替换的MIME类型,默认为“text/html”,若是制定为*,那么全部的
3.1 配置
1 2 3 4 5 6 7 8 9 10 11 12 |
server { listen 80; server_name www.ttlsa.com;
root /data/site/www.ttlsa.com;
location / { sub_filter ttlsa '运维生存时间'; sub_filter_types text/html; sub_filter_once on; } } |
内容以下
1 2 3 |
# cat /data/site/www.ttlsa.com/2013/10/20131001_sub1.html welcome to tTlsa! TTLSA TEAM! |
访问结果
1 2 3 |
# curl www.ttlsa.com/2013/10/20131001_sub1.html welcome to 运维生存时间! TTLSA TEAM! |
咱们能够看到它替换是不区分大小写的,并且ttlsa只被替换了一次。我把sub_filter_once on改为off试试。
1 2 3 4 |
location / { sub_filter ttlsa '运维生存时间'; sub_filter_once off; } |
接着测试
1 2 3 |
# curl www.ttlsa.com/2013/10/20131001_sub1.html welcome to 运维生存时间! 运维生存时间 TEAM! |
咱们能够看到ttlsa都被替换掉了.
例如你想在</head>后追加一段js,配置以下:
1 2 3 4 |
location / { sub_filter </head> '</head><script language="javascript" src="$script"></script>'; sub_filter_once on; } |
这边我就再也不作测试了,你们能够测试一下.
这个nginx替换响应内容的模块安装使用尤其简单,应用的地方相对较少,在nginx中也是一个可选模块。假如站点出现什么敏感字,想修改很耗时间,不妨试试这个模块.或者想临时在站点中加上一个通用js或者css之类的文件,也可使用这个模块.至于要在哪里,你们看看本身的需求.