网站建设

在Apache配置反向代理及实现输出内容替换

最近有个地方的项目需要搭建一个演示环境。该演示环境是在公司现有系统基础上来搭建的,所有测试数据也从现有系统中导入,但由于新客户(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中增加如下配置:

配置文件代码  icon_star
  1. ## mod_ext_filter directive to define a filter which  
  2. ## replaces text in the response  
  3. ##  
  4. ExtFilterDefine fixtext mode=output intype=text/html cmd=“/xampp-win32-1.7.4/apache/bin/sed.exe s/北京/上海/g”  
  5. ExtFilterDefine fixtext1 mode=output intype=text/html cmd=“/xampp-win32-1.7.4/apache/bin/sed.exe s/劳动局/财政厅/g”  
  6.   
  7. <Location />  
  8. # core directive to cause the fixtext filter to  
  9. # be run on output  
  10.   SetOutputFilter fixtext;fixtext1  
  11. </Location>  

 然后启动重启apache,再访问,演示系统的关键词完美地被替换成目标词。We did it!

另外网上有个使用了跟我一样的方法:

http://www.phwinfo.com/forum/alt-apache-configuration/226022-apache-windows-extfilterdefine-output-sed.html

mod_ext_filter的中文手册:

http://www.phpchina.com/manual/apache/mod/mod_ext_filter.html

附上本次配置的三个关键配置文件及sed.exe程序:

httpd.conf

配置文件代码  icon_star
  1. ServerRoot “/xampp-win32-1.7.4/apache”  
  2.   
  3. Listen 80  
  4.   
  5. LoadModule actions_module modules/mod_actions.so  
  6. LoadModule auth_basic_module modules/mod_auth_basic.so  
  7. LoadModule auth_digest_module modules/mod_auth_digest.so  
  8. LoadModule authn_default_module modules/mod_authn_default.so  
  9. LoadModule authn_file_module modules/mod_authn_file.so  
  10. LoadModule authz_default_module modules/mod_authz_default.so  
  11. LoadModule authz_host_module modules/mod_authz_host.so  
  12. LoadModule authz_user_module modules/mod_authz_user.so  
  13. LoadModule autoindex_module modules/mod_autoindex.so  
  14. LoadModule env_module modules/mod_env.so  
  15. LoadModule ext_filter_module modules/mod_ext_filter.so  
  16. LoadModule filter_module modules/mod_filter.so  
  17. LoadModule headers_module modules/mod_headers.so  
  18. LoadModule include_module modules/mod_include.so  
  19. LoadModule info_module modules/mod_info.so  
  20. LoadModule log_config_module modules/mod_log_config.so  
  21. LoadModule negotiation_module modules/mod_negotiation.so  
  22. LoadModule proxy_module modules/mod_proxy.so  
  23. LoadModule proxy_http_module modules/mod_proxy_http.so  
  24. LoadModule rewrite_module modules/mod_rewrite.so  
  25. LoadModule setenvif_module modules/mod_setenvif.so  
  26. LoadModule status_module modules/mod_status.so  
  27. LoadModule substitute_module modules/mod_substitute.so  
  28.   
  29. <IfModule !mpm_netware_module>  
  30. <IfModule !mpm_winnt_module>  
  31. User daemon  
  32. Group daemon  
  33.   
  34. </IfModule>  
  35. </IfModule>  
  36.   
  37. ServerAdmin postmaster@localhost  
  38.   
  39. ServerName localhost:80  
  40.   
  41. DocumentRoot “/xampp-win32-1.7.4/htdocs”  
  42.   
  43. <Directory />  
  44.     Options FollowSymLinks  
  45.     AllowOverride None  
  46.     Order deny,allow  
  47.     Deny from all  
  48. </Directory>  
  49.   
  50. <Directory “/xampp-win32-1.7.4/htdocs”>  
  51.     Options Indexes FollowSymLinks Includes ExecCGI  
  52.   
  53.     AllowOverride All  
  54.   
  55.     Order allow,deny  
  56.     Allow from all  
  57.   
  58. </Directory>  
  59.   
  60. <IfModule dir_module>  
  61.     DirectoryIndex index.shtml index.html index.htm   
  62.                    default.shtml default.html default.htm   
  63.                    home.shtml home.html home.htm  
  64. </IfModule>  
  65.   
  66. <FilesMatch “^.ht”>  
  67.     Order allow,deny  
  68.     Deny from all  
  69.     Satisfy All  
  70. </FilesMatch>  
  71.   
  72. ErrorLog “logs/error.log”  
  73.   
  74. LogLevel warn  
  75.   
  76. <IfModule log_config_module>  
  77.   
  78.     LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” combined  
  79.     LogFormat “%h %l %u %t “%r” %>s %b” common  
  80.   
  81.     <IfModule logio_module>  
  82.       LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i” %I %O” combinedio  
  83.     </IfModule>  
  84.     CustomLog “logs/access.log” combined  
  85. </IfModule>  
  86.   
  87. <Directory “/xampp-win32-1.7.4/cgi-bin”>  
  88.     AllowOverride None  
  89.     Options None  
  90.     Order allow,deny  
  91.     Allow from all  
  92. </Directory>  
  93.   
  94. DefaultType text/plain  
  95.   
  96.   
  97. # Server-pool management (MPM specific)  
  98. Include “conf/extra/httpd-mpm.conf”  
  99.   
  100. # Multi-language error messages  
  101. Include “conf/extra/httpd-multilang-errordoc.conf”  
  102.   
  103. # Fancy directory listings  
  104. Include “conf/extra/httpd-autoindex.conf”  
  105.   
  106. # Language settings  
  107. Include “conf/extra/httpd-languages.conf”  
  108.   
  109. # Implements a proxy/gateway for Apache.  
  110. Include “conf/extra/httpd-proxy.conf”  
  111.   
  112. # 设置过滤器  
  113. Include “conf/extra/httpd-filter.conf”  
  114.   
  115. # Various default settings  
  116. Include “conf/extra/httpd-default.conf”  

httpd-proxy.conf

配置文件代码  icon_star
  1. <IfModule proxy_module>  
  2. <IfModule proxy_http_module>  
  3.   
  4. #  
  5. # Reverse Proxy  
  6. #  
  7. ProxyRequests Off  
  8. <Proxy *>  
  9.     Order deny,allow  
  10.     Allow from all  
  11. </Proxy>  
  12. ProxyPass / http://www.demo.com/  
  13. ProxyPassReverse / http://www.demo.com/  
  14.   
  15.   
  16. </IfModule>  
  17. </IfModule>  

http-filter.conf (该文件是我自己加的)

配置文件代码  icon_star
  1. ## mod_ext_filter directive to define a filter which  
  2. ## replaces text in the response  
  3. ##  
  4. ExtFilterDefine fixtext mode=output intype=text/html cmd=“/xampp-win32-1.7.4/apache/bin/sed.exe s/厦门/上海/g”  
  5. ExtFilterDefine fixtext1 mode=output intype=text/html cmd=“/xampp-win32-1.7.4/apache/bin/sed.exe s/地税/财政/g”  
  6.   
  7. <Location />  
  8. # core directive to cause the fixtext filter to  
  9. # be run on output  
  10.   SetOutputFilter fixtext;fixtext1  
  11. </Location>  

Related Articles

发表回复

Back to top button