一、介绍

该ngx_http_sub_module模块是一个过滤器,通过将一个指定的字符串替换为另一个字符串来修改响应。该模块不是默认生成的,它应该使用--with-http_sub_module 配置参数启用 。

二、指令介绍

2.1、sub_filter指令

作用:用于替换字符串

用法:sub_filter   要替换的字符串   替换后的字符串,不区分字符串的大小写

范围:http   server   location

默认:-

2.2、sub_filter_last_modified指令

作用:允许在替换期间保留来自原始响应的“Last-Modified”标题字段以促进响应缓存。默认情况下,在处理期间修改响应的内容时,标题字段被删除。

用法:sub_filter_last_modified  on | off

范围:http   server   location

默认:sub_filter_last_modified  on

2.3、sub_filter_once

作用:指示是否查找每个字符串以替换一次或重复替换。

用法:sub_filter_once  on | off

范围:http   server   location

默认:http   server   location

2.4、sub_filter_types

作用:指定MIME类型的字符串替换,除了“ text/html” 之外,还可以在指定MIME类型的响应中启用字符串替换。特殊值“ *”匹配任何MIME类型

用法:sub_filter_types mime-type ...

范围:http   server   location

默认:sub_filter_types text / html;

三、操作演示

3.1、编译安装nginx

编译安装nginx,注意加上--with-http_sub_module模块,这里提供编译的参数:

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \

3.2、安装nginx

make && make  install

3.3修改配置文件nginx.conf

 location / {
sub_filter 'nginx.com' 'baidu.com'; --把nginx.com替换成了baidu.com
sub_filter 'nginx.org' 'buy.jiegeng.com';--把ngin.org替换成了buy.jiegeng.com
sub_filter_types css/html;--替换的类型
sub_filter_once off;--全部替换
root html;
index index.html index.htm;
}

3.4、重启

/usr/sbin/nginx    -s   reload

3.5、验证

3.5.1修改后页面

 

3.5.2原始页面

四、支持正则的第三方模块

使用ngx_http_sub_module模块好处是nginx内置该模块使用方便,不足之处在于该模块不支持正则替换,灵活性不够,

支持正则匹配替换的第三方模块ngx_http_substitutions_filter_module:

4.1、下载

下载地址:https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip

添加参数--add-module=/模块解压后文件路径

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \
--add-module=../ngx_http_substitutions_filter_module-master

4.2、subs_filter的使用介绍

* subs_filter
subs_filter 语法: subs_filter source_str destination_str [gior]
默认: none
适用: http,server,location
subs_filter 是用来替换文本的,可以使用正则
g(默认):替换匹配项。
i :区分大小写的匹配
o : 只匹配发现的第一个。
r : 正则匹配。

nginx的with-http_sub_module模块使用之替换字符串的更多相关文章

  1. nginx的http_sub_module模块使用之替换字符串

    Nginx可以实现很多功能,提供了许多插件,其中一个比较冷门的http_sub_module,是用来替换指定字符串的,它的原理是Nginx解析到文件后,执行这个插件进行拦截后返回. 昨天碰到一个场景, ...

  2. yum安装下的nginx,如何添加模块,和添加第三方模块

    需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...

  3. Nginx编译安装第三方模块http_substitutions_filter_module2222

    Nginx编译安装第三方模块http_substitutions_filter_module Rming -- 阅读 安装 Http 编译 module filter nginx 模块 >> ...

  4. Nginx编译安装第三方模块http_substitutions_filter_module

    Nginx编译安装第三方模块http_substitutions_filter_module 分类:服务器技术  作者:rming  时间:-- . >>ngx_http_substitu ...

  5. 不重装nginx实现动态添加模块

    如果项目在提供服务的过程中,因为需求使然,需要对nginx进行模块的动态添加,可以按照如下流程进行操作 一.查看nginx当前已经安装了那些模块 1) 进入nginx执行文件目录 cd   /usr/ ...

  6. 线上nginx 平滑添加新模块;如(--with-http_realip_module)

    nginx 添加模块1.查看当前nginx信息(配置文件路径,启动用户...) ps aux | grep nginx 2.查看当前nginx已启用的模块(记录模块信息,安装路径)./nginx -V ...

  7. Nginx一致性哈希模块的Lua实现

    Nginx一致性哈希模块的Lua重新实现 技术背景: 最近在工作中使用了nginx+redis 的架构,redis在后台做分布式存储,每个redis都存放不同的数据,这些数据都是某门户网站通过Hado ...

  8. nginx图片过滤处理模块http_image_filter_module安装配置笔记

    http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...

  9. Nginx加载ngx_pagespeed模块,加快网站打开的速度

    [页面加速]配置Nginx加载ngx_pagespeed模块,加快网站打开的速度   ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升 ...

随机推荐

  1. Linux 网络侦错:无法联机原因分析

    所谓的软件问题,绝大部分就是 IP 参数设定错误啊,路由不对啊,还有 DNS 的 IP 设定错误等等的, 这些问题都是属于软件设定啦!只要将设定改一改,利用一些侦测软件查一查,就知道问题出在哪里了!基 ...

  2. 第八章 让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    因为最近开发的项目涉及到移动设备上的 HTML5 开发,其中需要实现轮播效果.然后最快捷的方式,你知道的(Bootstrap),然后原生的 Bootstrap 的 carousel.js 插件并没有支 ...

  3. CreateDialog和DialogBox

    原文地址:https://blog.csdn.net/aikker/article/details/5631412 INT_PTR DialogBox(          HINSTANCE hIns ...

  4. java 解压缩 中文名称问题

    public List<String> unZip(String pathString, String zipPathString) { long startTime = System.c ...

  5. heidiSQL使用简介

    2017年12月5日09:49:42 星期二 HeidiSQL下载链接 1. 这个是免费的, 不用辛苦找破解码了, 也不用担心被破解软件装后门 2. 可以一次执行多条SQL语句分开选项卡显示 3. 可 ...

  6. Django请求周期图

  7. tcp和udp协议的聊天 和udp协议的时间同步机制-----编码

    tcp协议聊天 服务端:: 客户端 udp协议的聊天 ############ udp协议 ########### 服务器 import socket sk = socket.socket(type ...

  8. 【原创】大数据基础之Flume(2)Sink代码解析

    flume sink核心类结构 1 核心接口Sink org.apache.flume.Sink /** * <p>Requests the sink to attempt to cons ...

  9. Math中的floor,round和ceil方法总结

    floor向下取整,返回不大于的最大整数  Math.floor(1.4)=1.0ceil向上取整,返回不小于的最小整数  Math.ceil(1.4)=2.0round 四舍五入,将原来的数字加入0 ...

  10. SQLServer 2014 本地机房HA+灾备机房DR解决方案

    SQLServer 2014 主数据中心HA+灾备机房DR解决方案 SQLServer 2008 的时候使用 local WSFC+DR Mirror方式,对象是单数据库 两个单独的 WSFC 上使用 ...