我们很多时候会用到nginx的反向代理,的确是个非常好用的功能,在实际用的时候会遇到需要替换某些字符串的功能,下面我就带领大家如何动态的重新编译nginx添加ngx_http_substitutions_filter_module模块,并且设置好替换。
1、查看nginx当前安装所带参数,执行nginx -V
-bash-4.1# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.14.0 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-pcre --with-http_ssl_module
1.1 这里一定要查看原先的安装参数,不然后面可能会丢失某些功能
2、下载subs_filter模块
进入nginx源码目录,执行:git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
3、添加新模块并编译(不要make install)
./configure --user=www --group=www --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-pcre --with-http_ssl_module --add-module=ngx_http_substitutions_filter_module make
3.1 这里要注意添加了 --add-module=ngx_http_substitutions_filter_module 这个参数,也就是我们刚刚下载下来的
4、替换老的nginx文件为新的nginx文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old cp objs/nginx /usr/local/nginx/sbin/
5、配置nginx location模块
location / { charset utf-8; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Accept-Encoding ""; proxy_set_header X-NginX-Proxy true; proxy_set_header Connection ""; proxy_http_version 1.1; proxy_pass http://xxx.xxx.xxx.xxx:9999/; subs_filter aaa bbb; #sub_filter_once off; }
5.1 如果发现替换不成功,这里一般是添加 proxy_set_header Accept-Encoding ""; 参数就可以了
结束语
本文有任何错误,或有任何疑问,欢迎留言说明。
网友最新评论