目次Nginx是一款轻量级高性能服务器软件,固然轻量,但功能非常强盛,可用于提供WEB服务、反向代理、负载均衡、缓存服务、甚至可以通过添加一些模块搭建rtmp流媒体服务。最近遇到一个客户需求,必要用到nginx网页内容更换模块,贴出来跟各人交换,如有不足之处请指出。 ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串。这个模块已经内置在nginx中,但是默认未安装,必要安装必要加上配置参数:--with-http_sub_module 如果已经安装nginx,只必要再添加这个模块就可以了。 安装更换nginx官网下载安装包:http://nginx.org/en/download.html # wget http://nginx.org/download/nginx-1.11.5.tar.gz # tar -zxvf nginx-1.11.5.tar.gz # cd nginx-1.11.5 # ./configure --with-http_stub_status_module --with-http_sub_module && make && make install 常用指令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三个位置配置使用; 反向代理动态更换网页内容实例参考[code]server { listen 80; server_name mikecrm.xianzhixiong.com; # 上传文件巨细限定 client_max_body_size 20M; # 设置为on表现启动高效传输文件的模式 sendfile on; #charset koi8-r; #access_log logs/host.access.log main; location / { #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass http://mikecrm.com; proxy_set_header Host mikecrm.com; proxy_set_header Referer http://mikecrm.com; 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_request_buffering off; proxy_set_header Accept-Encoding ""; sub_filter_types *; sub_filter_once off; #sub_filter_last_modified on; sub_filter 'cdnq2.mikecrm.com' 'mikecrm.xianzhixiong.com'; sub_filter 'cdnq3.mikecrm.com' 'mikecrm.xianzhixiong.com'; sub_filter 'https://cdnq2plt.mikecrm.com' 'http://mikecrm.xianzhixiong.com'; sub_filter 'https://cdnq3plt.mikecrm.com' 'http://mikecrm.xianzhixiong.com'; sub_filter 'http://dlcn.mikecrm.com' ''; sub_filter 'http://mikecrm.com' ''; sub_filter 'https://mikecrm.com' ''; sub_filter 'www.mikecrm.com' 'mikecrm.xianzhixiong.com'; #sub_filter '.mikecrm.com' 'mikecrm.xianzhixiong.com'; #sub_filter 'mikecrm.com' 'mikecrm.xianzhixiong.com'; } #location ~ \.php$ { # proxy_pass https://real.mikecrm.com; #} #location ~ /plt.js$ { # proxy_pass https://cdnq3plt.mikecrm.com; #} }[/code]参数表明注意只有在新版本nginx中才支持多sub_filter. proxy_set_header Accept-Encoding ""; sub_filter_types *; sub_filter_once off; sub_filter 'str1' 'str2'; 总结到此这篇关于Nginx反向代理和内容更换模块实现网页内容动态更换功能的文章就介绍到这了,更多相干Nginx反向代理更换网页字符串内容请搜索脚本之家从前的文章或继承欣赏下面的相干文章盼望各人以后多多支持脚本之家! 来源:https://www.jb51.net/server/328339ysr.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 18:09 , Processed in 0.029784 second(s), 19 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.