nginx替换网站响应内容(ngx_http_sub_module)
2014-03-23 00:00
633 查看
ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串,比如你想把响应内容中的‘ttlsa’全部替换成‘运维生存时间’,这个模块已经内置在nginx中,但是默认未安装,需要安装需要加上配置参数:--with-http_sub_module
1. 安装nginx
如何安装nginx第三方模块
2. 指令(Directives) 语法: 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. nginx替换字符串实例
3.1 配置
3.2 测试 内容如下
4. 结束语 这个nginx替换响应内容的模块安装使用尤为简单,应用的地方相对较少,在nginx中也是一个可选模块。假如站点出现什么敏感字,想修改很耗时间,不妨试试这个模块.或者想临时在站点中加上一个通用js或者css之类的文件,也可以使用这个模块.至于要在哪里,大家看看自己的需求. 转载请注明来至运维生存时间:http://www.ttlsa.com/html/3289.html
1. 安装nginx
# wget http://nginx.org/download/nginx-1.4.2.tar.gz # tar -xzvf nginx-1.4.2.tar.gz # cd nginx-1.4.2 # --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module --with-http_sub_module # make # make install如果你已经安装了nginx,只需要额外追加这个模块,请看
如何安装nginx第三方模块
2. 指令(Directives) 语法: 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. nginx替换字符串实例
3.1 配置
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; } }
3.2 测试 内容如下
# cat /data/site/www.ttlsa.com/2013/10/20131001_sub1.html welcome to tTlsa! TTLSA TEAM!访问结果
# curl www.ttlsa.com/2013/10/20131001_sub1.html welcome to 运维生存时间! TTLSA TEAM!我们可以看到它替换是不区分大小写的,而且ttlsa只被替换了一次。我把sub_filter_once on改成off试试。
location / { sub_filter ttlsa '运维生存时间'; sub_filter_once off; }接着测试
# curl www.ttlsa.com/2013/10/20131001_sub1.html welcome to 运维生存时间! 运维生存时间 TEAM!我们可以看到ttlsa都被替换掉了. 例如你想在</head>后追加一段js,配置如下:
location / { sub_filter </head> '</head><script language="javascript" src="$script"></script>'; sub_filter_once on; }这边我就不再做测试了,大家可以测试一下.
4. 结束语 这个nginx替换响应内容的模块安装使用尤为简单,应用的地方相对较少,在nginx中也是一个可选模块。假如站点出现什么敏感字,想修改很耗时间,不妨试试这个模块.或者想临时在站点中加上一个通用js或者css之类的文件,也可以使用这个模块.至于要在哪里,大家看看自己的需求. 转载请注明来至运维生存时间:http://www.ttlsa.com/html/3289.html
相关文章推荐
- nginx替换网站响应内容(ngx_http_sub_module)
- nginx 替换网站响应内容(ngx_http_sub_module)
- nginx中http_sub_module模块(替换网站响应内容)
- nginx中http_addition_module模块(向响应内容中追加内容)
- nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
- nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
- Windows 下 Nginx 添加 ngx_http_sub_module 模块
- Nginx记录分析响应慢的请求及替换网站响应内容的配置
- nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
- nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
- nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
- nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)
- nginx向响应内容中追加内容(ngx_http_addition_module模块)
- 比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module
- nginx向响应内容中追加内容(ngx_http_addition_module模块)
- 详细解释:nginx中ngx_http_auth_basic_module模块(HTTP Auth Basic 模块)配置及各个参数含义 .
- Nginx模块 ngx_http_limit_conn_module 限制连接数
- nginx 动态缩略图模块安装(ngx_http_image_filter_module)
- 配置nginx身份验证(ngx_http_auth_basic_module)
- nginx静态压缩(ngx_http_gzip_static_module)