Nginx 反向代理内容替换(整理总结一套自己需要的东西)

1.修改 nginx 的./configure 模块,

添加反向代理和动态内容替换

#su

(先进入 root 状态)

# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module --add-module=/data/software/ngx_http_google_filter_module

#make

注意:1.这里不能按 make install 2.必须进入 root 状态,否则编译过程中会出现--prefix=**的时候 permisson deny。

2.进入 nginx.conf 文件进行修改内容。

      location / {

                 proxy_pass http://127.0.0.1:8080;

                 proxy_redirect off; subs_filter   ‘原内容‘   ‘替换成的内容’;

                 Index index.html index.htm;
                 sub_filter_once on;
}
3.一些常用语法:

sub_filter 指令: sub_filter string(原字符串) replacement(用于替换的字符串); 用于设置需要使用说明字符串替换说明字符串.string 是要被替换的字符串,replacement 是 新的字符串,它里面可以带变量。


sub_filter_last_modified 指令: sub_filter_last_modified on | off;

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


sub_filter_once 指令:sub_filter_once on | off;

用于设置字符串替换次数,默认只替换一次。如果是 on,默认只替换第一次匹配到的到字 符,如果是 off,那么所有匹配到的字符都会被替换;


sub_filter_types 指令:sub_filter_types *

用于指定需要被替换的 MIME 类型,默认为“text/html”,如果制定为*,那么所有的;


说明:以上指令可在 nginx.conf 的 http, server, location 三个位置配置使用;

猜你喜欢

转载自blog.csdn.net/qq_33182045/article/details/56673367