最近有个地方的项目需要搭建一个演示环境。该演示环境是在公司现有系统基础上来搭建的,所有测试数据也从现有系统中导入,但由于新客户(B客户)与现有客户(A客户)所属行业不同,为了演示环境有更逼真的演示效果,需要把测试数据及应用系统中页面上有关A客户的信息都改成B客户。
如果直接在应用程序及测试数据中来修改,工作量很大,而且容易遗漏。
另外有个办法就是实现一个HttpServletFilter来对输出内容进行过滤。这种方式比第一种方式要好一些,可以实现多数内容的替换,但对于一些静态文件,还是无法满足要求。
基于第二种办法的思路,我想出在应用服务器前用一个HTTP 服务器来作反射代理,客户端直接访问HTTP服务器,HTTP服务器向应用服务器请求数据,然后把需要替换的内容进行替换,最后把替换后的结果返回给客户端。
目前比较主的HTTP服务器(非JAVA的HTTP服务器)主要是LightTPD, Nginx和Apache。Apache是元老级的HTTP服务器,功能强大自不必说,但经过这么多年的扩充,身躯也是臃肿庞大。lighttpd是最轻量级的,我先试用lighttpd。
在lighttpd中配置反向代理时发现,lighttpd对域名支持上有些问题,于是放弃lighttpd改用Nginx。
Nginx在功能上与Apache相似度要高于lighttpd。配置反向代理还是比较顺利,但到了配置输出内容替换功能时,却找遇到一个问题解决不了。在Nginx中,使用HttpSubstitution模块来实现内容替换,该组件对英文的支持还算完美,但对于中文(我的系统是GBK字符集,UTF-8没试),却怎么也无法查找替换,在网上翻了很久,也没有找到在效的解决办法。只好把Nginx也枪毙了,剩下的只有Apache了,Apache应该感觉鸭梨很大。
在Apache中配置反射代理也很顺利,只有一个地方需要注意一下:在目标服务域名后面要加上“/”。接下来是配置输出内容替换功能,在网络上能找到的几个可以实现内容替换的组件:mod_substitute.so、mod_sed.so、mod_line_edit.so,可以是当我尝试使用这个几组件进行替换时,对英文内容工作良好,但对中文的处理,跟Nginx一样,总是无法查找指定的关键字词并替换。就这几个组件,来来回回地修改配置,测试,花了大半天时间,还是无法解决。
在感觉到无计可施时,突然看到Apache还有一个mod_ext_filter组件,通过该组件,可以调用 一个外部程序来实现filter功能,我可以试试用一个sed程序来代替mod_sed.so,看看能不能正常处理中文。我以前保留了一套windows下的Unix/Linux小工具,其中就包括了sed.exe。我就把sed.exe复制到apache/bin目录下,然后在http-filter.conf中增加如下配置:
## mod_ext_filter directive to define a filter which ## replaces text in the response ## ExtFilterDefine fixtext mode=output intype=text/html cmd="/xampp-win32-1.7.4/apache/bin/sed.exe s/北京/上海/g" ExtFilterDefine fixtext1 mode=output intype=text/html cmd="/xampp-win32-1.7.4/apache/bin/sed.exe s/劳动局/财政厅/g" <Location /> # core directive to cause the fixtext filter to # be run on output SetOutputFilter fixtext;fixtext1 </Location>
然后启动重启apache,再访问,演示系统的关键词完美地被替换成目标词。We did it!
另外网上有个使用了跟我一样的方法:
mod_ext_filter的中文手册:
http://www.phpchina.com/manual/apache/mod/mod_ext_filter.html
附上本次配置的三个关键配置文件及sed.exe程序:
httpd.conf
ServerRoot "/xampp-win32-1.7.4/apache" Listen 80 LoadModule actions_module modules/mod_actions.so LoadModule auth_basic_module modules/mod_auth_basic.so LoadModule auth_digest_module modules/mod_auth_digest.so LoadModule authn_default_module modules/mod_authn_default.so LoadModule authn_file_module modules/mod_authn_file.so LoadModule authz_default_module modules/mod_authz_default.so LoadModule authz_host_module modules/mod_authz_host.so LoadModule authz_user_module modules/mod_authz_user.so LoadModule autoindex_module modules/mod_autoindex.so LoadModule env_module modules/mod_env.so LoadModule ext_filter_module modules/mod_ext_filter.so LoadModule filter_module modules/mod_filter.so LoadModule headers_module modules/mod_headers.so LoadModule include_module modules/mod_include.so LoadModule info_module modules/mod_info.so LoadModule log_config_module modules/mod_log_config.so LoadModule negotiation_module modules/mod_negotiation.so LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule rewrite_module modules/mod_rewrite.so LoadModule setenvif_module modules/mod_setenvif.so LoadModule status_module modules/mod_status.so LoadModule substitute_module modules/mod_substitute.so <IfModule !mpm_netware_module> <IfModule !mpm_winnt_module> User daemon Group daemon </IfModule> </IfModule> ServerAdmin postmaster@localhost ServerName localhost:80 DocumentRoot "/xampp-win32-1.7.4/htdocs" <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Deny from all </Directory> <Directory "/xampp-win32-1.7.4/htdocs"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Order allow,deny Allow from all </Directory> <IfModule dir_module> DirectoryIndex index.shtml index.html index.htm \ default.shtml default.html default.htm \ home.shtml home.html home.htm </IfModule> <FilesMatch "^\.ht"> Order allow,deny Deny from all Satisfy All </FilesMatch> ErrorLog "logs/error.log" LogLevel warn <IfModule log_config_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common <IfModule logio_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio </IfModule> CustomLog "logs/access.log" combined </IfModule> <Directory "/xampp-win32-1.7.4/cgi-bin"> AllowOverride None Options None Order allow,deny Allow from all </Directory> DefaultType text/plain # Server-pool management (MPM specific) Include "conf/extra/httpd-mpm.conf" # Multi-language error messages Include "conf/extra/httpd-multilang-errordoc.conf" # Fancy directory listings Include "conf/extra/httpd-autoindex.conf" # Language settings Include "conf/extra/httpd-languages.conf" # Implements a proxy/gateway for Apache. Include "conf/extra/httpd-proxy.conf" # 设置过滤器 Include "conf/extra/httpd-filter.conf" # Various default settings Include "conf/extra/httpd-default.conf"
httpd-proxy.conf
<IfModule proxy_module> <IfModule proxy_http_module> # # Reverse Proxy # ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://www.demo.com/ ProxyPassReverse / http://www.demo.com/ </IfModule> </IfModule>
http-filter.conf (该文件是我自己加的)
## mod_ext_filter directive to define a filter which ## replaces text in the response ## ExtFilterDefine fixtext mode=output intype=text/html cmd="/xampp-win32-1.7.4/apache/bin/sed.exe s/厦门/上海/g" ExtFilterDefine fixtext1 mode=output intype=text/html cmd="/xampp-win32-1.7.4/apache/bin/sed.exe s/地税/财政/g" <Location /> # core directive to cause the fixtext filter to # be run on output SetOutputFilter fixtext;fixtext1 </Location>