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. js 格式化数字,格式化金额:

    js 格式化数字,格式化金额: function number_format(number, decimals, dec_point, thousands_sep) { /* * 参数说明: * nu ...

  2. Websocket实现即时通讯

    前言 关于我和WebSocket的缘:我从大二在计算机网络课上听老师讲过之后,第一次使用就到了毕业之后的第一份工作.直到最近换了工作,到了一家是含有IM社交聊天功能的app的时候,我觉得我现在可以谈谈 ...

  3. 美图App的移动端DNS优化实践:HTTPS请求耗时减小近半

    本文引用了颜向群发表于高可用架构公众号上的文章<聊聊HTTPS环境DNS优化:美图App请求耗时节约近半案例>的部分内容,感谢原作者. 1.引言 移动互联网时代,APP 厂商之间的竞争非常 ...

  4. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  5. [Swift]LeetCode669. 修剪二叉搜索树 | Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  6. 关于Python pandas模块输出每行中间省略号问题

    关于Python数据分析中pandas模块在输出的时候,每行的中间会有省略号出现,和行与行中间的省略号....问题,其他的站点(百度)中的大部分都是瞎写,根本就是复制黏贴以前的版本,你要想知道其他问题 ...

  7. Python Matplotlib.pyplot plt 中文显示

    话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...

  8. 【java设计模式】(6)---迭代器模式(案例解析)

    设计模式之迭代器模式 一.java迭代器介绍 1.迭代器接口 在jdk中,与迭代器相关的接口有两个:Iterator 与 Iterable. Iterator:迭代器,Iterator及其子类通常是迭 ...

  9. Qt之自定义托盘

    说起Qt,真是个不错的ui库,不仅仅ui做的好,其他方面也不差,在平台扩展方面也是非常的强大.这篇文章我将会分析下qt的托盘,QSystemTrayIcon是qt的托盘类,托盘类的用途是什么我就不说了 ...

  10. 在龙芯小本上安装Debain8.10

    (图片是LEMOTE8089D笔记本,来自互联网) YX原来送了一个LEMOTE笔记本给我.CPU是首款真正的国产,龙芯2F,兼容mips的指令集. 笔记本原来的操作系统是Debian6,后来升级到了 ...