首页 > Web开发 > 详细

nginx 替换网站响应内容(ngx_http_sub_module)

时间:2017-07-12 22:44:25      阅读:553      评论:0      收藏:0      [点我收藏+]

nginx在编译安装的时候需要编译安装这个模块

--with-http_sub_module
make && make install

语法:     sub_filter old_string new_string;
默认值:     —
配置段:     http, server, location

这三个段都可以配置sub_filter 

设置需要使用说明字符串替换说明字符串.old_string是要被替换的字符串,new_string是新的字符串,它里面可以带变量。


语法:       sub_filter_last_modified on | off;
默认值:     sub_filter_last_modified off;
配置段:      http, server, location

用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用,默认值是off;


语法: sub_filter_once on | off;
默认值: sub_filter_once on;
配置段: http, server, location
字符串替换一次还是多次替换,默认为on只替换一次,如果off,那么所有的old_string都会被替换


语法: sub_filter_types mime-type ...;
默认值: sub_filter_types text/html;
配置段: http, server, location
指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有类型的文件


例子:

在nignx上加上个server,在localtion上加

    server {
        listen       80;
        server_name  www.hxy.com;

        root /data/www;
        location / {
            sub_filter  world ‘hxy‘;
                }
            }
cat /data/www/index.html
hello world

curl 
hello hxy

在localhost上加入生效了,现在在server上加入:

    server {
        listen       80;
        server_name  www.hxy.com;
        sub_filter  world ‘hxy123‘;
        root /data/www;
            }
cat /data/www/index.html
hello world

curl www.hxy.com
hello hxy123

server上也生效了

本文出自 “Forand” 博客,请务必保留此出处http://853056088.blog.51cto.com/12966870/1946765

nginx 替换网站响应内容(ngx_http_sub_module)

原文:http://853056088.blog.51cto.com/12966870/1946765

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!