nginx的with-http_sub_module模块使用之替换字符串

一、介绍

该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  : 正则匹配。

原文地址:https://www.cnblogs.com/heruiguo/p/9076239.html

时间: 2024-07-28 21:58:40

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

修复OpenSSL漏洞 升级OpenSSL版本 nginx静态编译ssl模块

背景 OpenSSL全称为Secure Socket Layer,是Netscape所研发,利用数据加密(Encryption)作技术保障在Internet上数据传输的安全.可确保数据在网络上的传输不会被窃听及截取. 当然,OpenSSL是一个强大的密码库,我们在使用SSL协议的时候不一定非得采用OpenSSL,不过目前基本上都是用的OpenSSL,因为它更安全,使用起来也更简单. 通常出现的OpenSSL的漏洞需要升级版本解决 查看openssl版本 openssl version 进入存放源

nginx三 之缓存模块

友情提示: 缓存模块是在动静分离的环境基础上搭建,动静分离可以参考http://www.cnblogs.com/dahuandan/p/6759212.html 介绍 提高网站响应速度是web应用不容忽视的目标,在之前动静分离的基础上,我们已经降低了后端服务器压力,提高了处理请求的性能,但是用户请求的静态资源是从硬盘读取,相比内存的性能还有很大的提高: Nginx自带的缓存模块可以把静态资源缓存到内存中,提高了用户请求静态资源的速度,并且nginx自带缓存模块配置简单,使用灵活,搭配第三方插件可

nginx 学习五 filter模块简介和实现一个简单的filter模块

1 nginx过滤模块简介 过滤(filter)模块是过滤响应头和内容的模块,可以对回复的头和内容进行处理.它的处理时间在获取回复内容之后, 向用户发送响应之前.它的处理过程分为两个阶段,过滤HTTP回复的头部和主体,在这两个阶段可以分别对头部和主体 进行修改. 2 过滤模块执行顺序 2.1 ngx_http_output_(head, body)_filter_pt 先看一下nginx常用的过滤模块,在ngx_moudles.c中有一下代码: ngx_module_t *ngx_modules

Nginx学习指南之模块的应用

一.自述 Nginx模块功能也是相当的丰富,但对于Apache Web来说,两者之间还是有区别的,大家都知道Nginx模块是直接被编译进了nginx.conf配置文件,而Apache则是被编译成为.SO文件,有些是需要在httpd.conf配置文件中指定是否加载,这样才能激活模块功能.Nginx模块有三个角色,Handlers(处理一个HTTP请求,并产生输出):Filters(处理由一个handler生成的输出):load-balancers(负载均衡器,当后端服务器符合一个以上后,依据算法选

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

nginx图片过滤处理模块http_image_filter_module安装配置笔记 http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时缩放图片,旋转图片,验证图片有效性以及获取图片宽高以及图片类型信息,由于是即时计算的结果,所以网站访问量大的话,不建议使用. 安装还是很简单的,默认HttpImageFilterModule模块是不会编译进ngi

nginx源码分析--模块分类

ngx-modules Nginx 主要的模块大致可以分为四类: handler – 协同完成客户端请求的处理.产生响应数据.比如模块, ngx_http_rewrite_module, ngx_http_log_module, ngx_http_static_module. filter – 对 handler 产生的响应数据做各种过滤处理.比如模块, ngx_http_not_modified_filter_module, ngx_http_header_filter_module. ups

nginx图片过滤处理模块http_image_filter_module

nginx图片过滤处理模块http_image_filter_module安装配置笔记 http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时缩放图片,旋转图片,验证图片有效性以及获取图片宽高以及图片类型信息,由于是即时计算的结果,所以网站访问量大的话,不建议使用. 安装还是很简单的,默认HttpImageFilterModule模块是不会编译进ngi

Nginx服务器的功能模块

Nginx和Apache的区别优点 1.Nginx与Apache比较的优点 对于这个问题,并不能说哪一个较好,本身他们都有优缺点,例如:Apache提供了几百个模块,模块就意味着功能,但作为一个Apache服务器最多只有2000多的并发量,Nginx提供的模块也就几十个,但他却可以提供20000的并放量.Apache的功能多,而Nginx的功能少,但有一句话可以很好的说明: Apache就像Office一样,他有几十万的选项,但你只需要那么六个功能能选 项,Nginx只做了这六个功能,但是他做的

Nginx负载均衡组件模块

Nginx负载均衡组件模块 实现Nginx负载均衡的组件主要有两个: n  ngx_http_proxy_module proxy代理模块,用于把请求后抛给服务器节点或upstream服务器池 n  ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查 1.upstream模块 upstream模块允许Nginx定义一组或多组节点服务器组,使用时可以通过proxy_pass代理方式把网站的请求发送到事先定义好的对应upstream组的名字上,