bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[原]apache模块的编写实例 (反向透明代理实现方式之一)

Posted by bianbian on 2006-10-30 07:45

本文Tags: , ,

除了用端口映射(当然,要在转发数据包前传递客户端IP地址给内网服务器)来实现反向透明代理以外,利用apache的mod_proxy也是一种办法。而后一种办法更方便的地方是一个80端口可以给很多域名使用(即虚拟主机)——当然前一种也是可以,只要根据不同域名把数据包转发到不同内网服务器而已,我只是懒得写而已;因为工作量显然是直接用apache来得少。。。。
我贴出用后一种办法实现以前lilybbs.net公网代理的代码;之所以要自己写apache的模块,是因为直接用mod_proxy是不能传递客户端的IP地址给真正的内网服务器的。我原本打算是插入自定义的一个HTTP头来实现IP传递,这样一般是没有问题;但是当客户端本身访问网络经过Squid代理时,Squid会删除掉非标准的HTTP头,真是FT。所以我采用的方法是通过”Accept-Language”头传递客户端IP地址,”Accept-Language”是用来标示客户端接受的页面语言顺序(中文?英文?之类),在非国际化的网站中并没有很大的用处。
apache的模块编写其实很简单,大部分都是架子工程,直接贴过来就可以。
代码如下(代码经过lilybbs.net公网访问测试OK,不过现在学校网络中心停止了lilybbs.net的公网出口)。

  1. /*
  2. apache 2.x 模块
  3. 所有http请求的header发送ip,以实现代理
  4. 如需使用 LilybbsipEnable 设为 on 即可(需要和mod_proxy一起使用)
  5.  
  6. 编译:
  7. /home/apache2/bin/apxs -i -a -c mod_lilybbsip.c
  8.  
  9. 参考:
  10. http://modules.apache.org/
  11.  
  12. 作者:
  13. Net@Lilybbs, 2006-3-26 17:38
  14. http://bianbian.org
  15. */
  16.  
  17. #include "httpd.h"
  18. #include "http_config.h"
  19. #include "http_core.h"
  20. #include "http_log.h"
  21. #include "http_protocol.h"
  22. #include "http_vhost.h"
  23. #include "apr_strings.h"
  24.  
  25. module AP_MODULE_DECLARE_DATA lilybbsip_module;
  26.  
  27.  
  28. typedef struct {
  29.     int enable;
  30. } lilybbsip_server_cfg;
  31.  
  32. static void *lilybbsip_create_server_cfg(apr_pool_t *p, server_rec *s) {
  33.     lilybbsip_server_cfg *cfg = (lilybbsip_server_cfg *)apr_pcalloc(p, sizeof(lilybbsip_server_cfg));
  34.     if (!cfg)
  35.         return NULL;
  36.  
  37.     cfg->enable = 0;
  38.  
  39.     return (void *)cfg;
  40. }
  41.  
  42.  
  43. static const char *lilybbsip_enable(cmd_parms *cmd, void *dummy, int flag) {
  44.     lilybbsip_server_cfg *cfg = (lilybbsip_server_cfg *)ap_get_module_config(cmd->server->module_config, &lilybbsip_module);
  45.     cfg->enable = flag;
  46.     return NULL;
  47. }
  48.  
  49. static const command_rec lilybbsip_cmds[] =
  50. {
  51.     AP_INIT_FLAG(
  52.                  "LilybbsipEnable",
  53.                  lilybbsip_enable,
  54.                  NULL,
  55.                  RSRC_CONF,
  56.                  "Enable mod_lilybbsip"
  57.                  ),
  58.     { NULL }
  59. };
  60.  
  61. static int ip_lilybbsing_handler(request_rec *r) {
  62.  
  63.     lilybbsip_server_cfg *cfg = (lilybbsip_server_cfg *)ap_get_module_config(r->server->module_config, &lilybbsip_module);
  64.     if (!cfg->enable)
  65.         return DECLINED;
  66.    
  67.     //通过Accept-Language传递IP
  68.     apr_table_set(r->headers_in, "Accept-Language", r->connection->remote_ip);
  69.  
  70.     return DECLINED;
  71. }
  72.  
  73.  
  74. static void register_hooks(apr_pool_t *p)
  75. {
  76.     ap_hook_post_read_request(ip_lilybbsing_handler, NULL, NULL, APR_HOOK_MIDDLE);
  77. }
  78.  
  79. module AP_MODULE_DECLARE_DATA lilybbsip_module =
  80. {
  81.     STANDARD20_MODULE_STUFF,
  82.     NULL,
  83.     NULL,
  84.     lilybbsip_create_server_cfg,
  85.     NULL,
  86.     lilybbsip_cmds,
  87.     register_hooks,
  88. };

使用的时候在vhost(虚拟主机)内设定:

LilybbsipEnable On
ProxyRequests Off
ProxyPass / http://bbs.nju.edu.cn

标签: , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

2 Responses to “[原]apache模块的编写实例 (反向透明代理实现方式之一)”

  1. danny Says:

    haproxy, 可以实现同样的功能,可以参考一下,very的不错

  2. dowson Says:

    朋友,您好,我喜欢AUTOIT,请问可以加我吗,谢谢。我的msn:dowson1982@hotmail.com

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

(required)