nginx的ngx_http_sub_module模块,可以用于修改网站响应内容中的字符串,如过滤敏感词。第三方模块ngx_http_substitutions_filter_module,弥补了ngx_http_sub_module的不足,可以采用正则表达式替换。

part 1、安装ngx_http_sub_module

nginx默认是不安装ngx_http_sub_module模块的,直接应用sub_filter指令将报错

nginx: [emerg] unknown directive "sub_filter" in /nginx-test/conf/nginx.conf:

因此需要在编译过程中添加 --with-http_sub_module 参数

# 编译
./configure --prefix=/nginx-sub --with-http_sub_module
# 安装
make install

part 2、sub模块替换文本

官网文档说明,ngx_http_sub_module包括四个命令:

sub_filter string replacement;                 将字符串string修改成replacement,不区分大小写,传入文本是上一次处理后的文本

sub_filter_last_modified on | off;    default: off      是否阻止response header中写入Last-Modified,防止缓存,默认是off,即防止缓存

sub_filter_once on | off;        default: on      sub_filter指令是执行一次,还是重复执行,默认是只执行一次

sub_filter_types mime-type ...;      default: text/html    指定类型的MINE TYPE才有效

下面以替换路飞为例:

sub.html原始文本为:

1)修改一次

location /sub {
sub_filter 'luffy' '路飞';
}

发现不区分大小写的把Luffy替换为了路飞,而第三行的luffy不变

2)重复执行修改

location /sub {
sub_filter 'luffy' '路飞';
  sub_filter_once off;
}

这次把Luffy和luffy都替换成路飞了

备注:开放80端口命令

# 开启端口
# --zone #作用域
# --add-port=/tcp #添加端口,格式为:端口/通讯协议
# --permanent #永久生效,没有此参数重启后失效
firewall-cmd --zone=public --add-port=/tcp --permanent
# 重启防火墙
firewall-cmd --reload

part 3、subs_filter多个替换

ngx_http_substitutions_filter_module是一个第三方模块,它可以多次替换或者正则替换网站响应内容,需要通过--add-module参数编译添加

首先需要下载,地址是:https://github.com/yaoweibin/ngx_http_substitutions_filter_module

下载之后,我放在了与nginx源码平级的目录下

# 编译
./configure --prefix=/nginx-sub --with-http_sub_module --add-module=../ngx_http_substitutions_filter_module-master
# 安装
make install 
subs_filter source_str destination_str [gior]   default:g    默认是全局匹配,大小写敏感

使用subs_filter指令

location /sub {
subs_filter 'luffy' '路飞';
}

location /sub {
subs_filter 'luffy' '路飞' i;
}

使用正则表达式:

location /sub {
subs_filter luffy|鲁夫 路飞 ir;
}

nginx sub模块替换文本的更多相关文章

  1. (转)nginx 常用模块整理

    原文:http://blog.51cto.com/arm2012/1977090 1. 性能相关配置 worker_processes number | auto: worker进程的数量:通常应该为 ...

  2. nginx基础模块

    http://www.nginx.cn/doc/ 基础模块 HTTP Core模块* HTTP Upstream 模块 HTTP Access 模块 HTTP Auth Basic 模块 HTTP A ...

  3. nginx添加模块 (非覆盖安装)

    nginx添加模块(非覆盖安装) 原已经安装好的nginx,现在需要添加一个未被编译安装的模块: 查看原来编译时都带了哪些参数# /usr/local/nginx/sbin/nginx -V ngin ...

  4. [转帖]Nginx rewrite模块深入浅出详解

    Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...

  5. Nginx详解七:Nginx基础篇之Nginx官方模块

    Nginx官方模块 --with-http_stub_status_module:Nginx的客户端状态,用于监控连接的信息,配置语法如下:配置语法:stub_status;默认状态:-配置方法:se ...

  6. nginx第三方模块安装方法

    nginx第三方模块安装方法 ./configure --prefix=/你的安装目录 --add-module=/第三方模块目录 比如echo模块. 没安装前,使用测试. location /tes ...

  7. Nginx RTMP 模块 nginx-rtmp-module 指令详解

    译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解.指令Corertmp语法:rtmp { ... }上下文:根描述:保存所 ...

  8. nginx常用模块(三)

    Nginx常用模块(三) ngx_http_proxy_module模块配置(http或https协议代理) proxy_pass URL; 应用上下文:location, if in locatio ...

  9. Nginx 常用模块

    Nginx 常用模块 1. ngx_http_autoindex_module # ngx_http_autoindex_module模块处理以斜杠字符(' / ')结尾的请求,并生成一个目录列表. ...

随机推荐

  1. Java web每天学之Servlet工作原理详情解析

    上篇文章中我们介绍了Servlet的实现方式以及Servlet的生命周期,我们这篇文章就来介绍一下常用对象. 点击回顾:<Java Web每天学之Servlet的工作原理解析>:<J ...

  2. [Swift]LeetCode244.最短单词距离 II $ Shortest Word Distance II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  3. [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  4. [Swift]LeetCode818. 赛车 | Race Car

    Your car starts at position 0 and speed +1 on an infinite number line.  (Your car can go into negati ...

  5. [Swift]LeetCode942. 增减字符串匹配 | DI String Match

    Given a string S that only contains "I" (increase) or "D" (decrease), let N = S. ...

  6. eclipse项目有红叉的解决办法

    eclipse项目上有红叉,说明这个项目存在一些的问题,对于这种情况需要具体来看. 1 新导入项目的红叉 如果是新导入的项目,一般红叉就只在项目名称上面有红叉,项目下的分项上面没有,这一般是由于当初项 ...

  7. 论Java访问权限控制的重要性

    人在什么面前最容易失去抵抗力? 美色,算是一个,比如说西施的贡献薄就是忍辱负重.以身报国.助越灭吴:金钱,算是另外一个,我们古人常说“钱乃身外之物,生不带来死不带去”,但我们又都知道“有钱能使鬼推磨” ...

  8. 前端笔记之jQuery(下)事件&节点操作&净位置&拖拽&页面卷动值&遍历JSON

    一.监听事件大全 1.1 JavaScript事件 onblur 元素失去焦点 onchange 用户改变域的内容 onclick 鼠标点击某个对象 ondblclick 鼠标双击某个对象 onfoc ...

  9. Asp.Net Core 程序部署到Linux(centos)生产环境(二):docker部署

    运行环境 照例,先亮环境:软件的话我这里假设你已经批准好了.net core 运行环境,未配置可以看我的这篇[linux(centos)搭建.net core 运行环境] 腾讯云 centos:7.2 ...

  10. MySQL 个人学习笔记

    最早之前学习了Mysql,不知道是多长时间了 O(∩_∩)O,今天突然发现之前学习过程中的这个笔记,于是,就分享给大家,希望,能帮助到有需要的朋友,有不足的地方欢迎大家多多交流 b( ̄▽ ̄)d 1. ...