基于Nginx的XML Web Serviec实现

115 views
Skip to first unread message

bartholo

unread,
May 20, 2009, 3:31:51 AM5/20/09
to cnginx
先把我的问题再澄清一下:
Web Service服务器根据从客户端接收而来XML请求做出相应的处理,然后发回相应的XML响应。

之前跟版主请教过这个问题。他的建议是用Handler来做。

由于对Nginx的整个运行机制,架构都不是很熟悉。现在在看Emiller's Guide To Nginx Module
Development,但是里头涉及到的一些概念让我很是模糊。

所以对一个Handler如何来写,需要提供什么ctx,提供什么callbacks都不是很清楚?

恳请大家一起给指点迷津。


非常感谢

deltay

unread,
May 20, 2009, 3:45:58 AM5/20/09
to cnginx
如果你只是把nginx作为 webservice的前端,那么把请求丢给后面的web service 的服务器就可以了。nginx只是做一个反向
代理。

如果你准备自己实现 WEB Service的API,那么就要开发个handler,自己解析soap消息,执行API,返回API结果。

如果这样的话,有一个需要注意地方: nginx是基于事件驱动的,你也最好用事件驱动方式来做,不要阻塞了后续的请求。

Weibin Yao

unread,
May 20, 2009, 3:57:58 AM5/20/09
to cng...@googlegroups.com
http://wiki.nginx.org/NginxHttpCircleGifModule

对照Emiller的模块来看,会容易懂一点。

--
Weibin Yao

wenxing zheng

unread,
May 20, 2009, 3:50:54 AM5/20/09
to cng...@googlegroups.com
事实上,我的nginx不做代理,就是一个web server.所以nginx上,会实现一些业务的逻辑。

对于开发handler,用事件驱动,能否给个例子? 以及实现的一些细节。

个人觉得Nginx很好,以后会作为一个长期关注的项目。

目前比较紧急,希望能多得到指点

Ken

unread,
May 20, 2009, 3:58:15 AM5/20/09
to cng...@googlegroups.com
如何写 nginx module 是找对了方向

但是不应该使用这么重量级的解决方案,fastcgi 应该是个不错的方案


2009/5/20 bartholo <wenxin...@gmail.com>

deltay

unread,
May 20, 2009, 4:41:40 AM5/20/09
to cnginx
恩,我也觉得自己实现WEB SERVICE不是个很好的思路。
是什么促使你不选择代理方式用JAVA或其他语言实现WEB SERVICE API,而是自己在nginx上实现呢?

On 5月20日, 下午3时58分, Ken <ken.s...@gmail.com> wrote:
> 如何写 nginx module 是找对了方向
>
> 但是不应该使用这么重量级的解决方案,fastcgi 应该是个不错的方案
>

> 2009/5/20 bartholo <wenxing.zh...@gmail.com>

wenxing zheng

unread,
May 20, 2009, 4:49:51 AM5/20/09
to cng...@googlegroups.com
感谢大家!

其实,我们的Web service接收到请求之后,会根据请求的内容来更新同一主机上的共享内存。而这共享内存是由C++来写的,这是我们没法改变的。

用Java来实现,目前我个人觉得是没法和C++定义的共享内存通讯的。

为什么说?用Handler来实现,是一个重量级的方案? 我们的客户端,会根据我们提供的服务器端的Ip address和端口,向我们的server发送请求,可能对FastCGI不了解,但是一直觉得它不适合我们的场景。

我现在对实现一个就算只是打印出请求内容的handler都不是很了解,所以非常需要大家的帮助。其实,只要这一步做到了,其它的都会迎刃而解的。

Delta Yeh

unread,
May 20, 2009, 5:12:58 AM5/20/09
to cng...@googlegroups.com
这样的话,你就需要开发个handler,处理消息体里的SOAP信息,解析出API和方法,然后更新共享内存的东西,返回SOAP消息作为API调用结果。
你可以参考stub_status模块,基本的代码框架都有了。
你的工作量应该是:
1.解析和生成SOAP
2.更新共享内存。

祝你好运。


2009/5/20 wenxing zheng <wenxin...@gmail.com>

wenxing zheng

unread,
May 20, 2009, 5:30:36 AM5/20/09
to cng...@googlegroups.com
对于变量的定义,我有些不是很理解,赐教:

static ngx_command_t  ngx_http_status_commands[] = {

    { ngx_string("stub_status"),
      NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
      ngx_http_set_status,
      0,
      0,
      NULL },

      ngx_null_command
};

static ngx_http_module_t  ngx_http_stub_status_module_ctx = {
    NULL,                                  /* preconfiguration */
    NULL,                                  /* postconfiguration */

    NULL,                                  /* create main configuration */
    NULL,                                  /* init main configuration */

    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */

    NULL,                                  /* create location configuration */
    NULL                                   /* merge location configuration */
};


ngx_module_t  ngx_http_stub_status_module = {
    NGX_MODULE_V1,
    &ngx_http_stub_status_module_ctx,      /* module context */
    ngx_http_status_commands,              /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};


NGX_HTTP_SRV/LOC_CONF这个是什么意思?这个ngx_http_status_commands, ***_status_module_ct 以及***_status_module都是什么意思? 换句话说,这些功能定义是怎么被Nginx调用的?

Delta Yeh

unread,
May 20, 2009, 6:01:01 AM5/20/09
to cng...@googlegroups.com

这个是指命令在什么范围有效。
Server 范围还是Location范围。
只有正确设置范围才能够在  get_module__xxx_conf里访问到你的配置。
2009/5/20 wenxing zheng <wenxin...@gmail.com>

wenxing zheng

unread,
May 20, 2009, 6:15:11 AM5/20/09
to cng...@googlegroups.com
我试了一下stub_status模块。

由于command是这么定义的

static ngx_command_t  ngx_http_status_commands[] = {

    { ngx_string("stub_status"),
      NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
      ngx_http_set_status,
      0,
      0,
      NULL },

      ngx_null_command
};

所以在conf里头,必须要有location的定义
location /nginx_status
{

}


wenxing zheng

unread,
May 20, 2009, 6:19:18 AM5/20/09
to cng...@googlegroups.com
如果我的service是不基于location的,那么我是不是只要NGX_HTTP_SRV_CONF就可以。
如果是SRV级别的,那么Nginx怎么调用到我的module.

NGX_HTTP_MAIN_CONF这个又是什么概念?

Delta Yeh

unread,
May 20, 2009, 9:41:26 AM5/20/09
to cng...@googlegroups.com
LOC 级别和SVR级别是指请求来时你的模块可以获取到的经过合并后的配置参数。


nginx能否调用你的模块,是靠你安装到它的handle列表里去的。
建议你仔细读一下那个开发文档。
这里简单告诉你:
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_core_loc_conf_t  *clcf;

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    clcf->handler = ngx_http_status_handler;

    return NGX_CONF_OK;
}

这个函数把 ngx_http_status_handler注册到了nginx的内容handler里(唯一的),表示请求来后由ngx_http_status_handler
来处理请求并产生输出。
你要写的内容handler类似,处理在HTTP消息体里的soap信息,返回结果。



2009/5/20 wenxing zheng <wenxin...@gmail.com>

Weibin Yao

unread,
May 20, 2009, 6:17:15 AM5/20/09
to cng...@googlegroups.com
NGX_HTTP_SRV表示该指令可以出现在server块内
NGX_LOC_CONF表示该指令可以出现在location块内
> 这个ngx_http_status_commands,
一般用来描述指令的位置、类型、输入处理函数等的结构体
> ***_status_module_ctx
context有上下文的意思,主要是模块配置的初始化及合并。
> 以及***_status_module都是什么意思?
模块的声明,指明模块类型等参数
> 换句话说,这些功能定义是怎么被Nginx调用的?
>
比较复杂,一般每个location都有相应的handler函数,比如这里的handler函数初
始化于ngx_http_set_status中。

Nginx先根据请求找到相应的location,再把请求给location的handler函数调用,
如果找不到,就用默认的 ngx_http_static_module处理。

###############################################################
如果你了解apache的模块的话,其实这些都是类似的。慢慢看EMiller的文档吧,
这些东西他基本上都涉及了,急不来的。

--
Weibin Yao

wenxing zheng

unread,
May 20, 2009, 10:25:46 AM5/20/09
to cng...@googlegroups.com
多谢各位大虾的帮助,现在较之前多理解了很多。

还有一个问题,就是调试Nginx的问题。我在安装Nginx的时候,指定了--with-debug,是否就意味着,Nginx用于Debug的log都能看到。

如果不行,是否Nginx有用于Debug的接口,用来研究Nginx的执行过程...

Weibin Yao

unread,
May 20, 2009, 10:20:05 PM5/20/09
to cng...@googlegroups.com
1、仅仅在编译的时候加--with-debug是不行的,你需要在配置文件中加入如下指令:
error_log logs/error.log debug_core debug_event debug_http;
debug_core:表示打印启动核心代码时的调试输出。
debug_event:表示event处理时的调试输出。
debug_http:表示http模块处理时的调试输出,一般我们用到最多。
...
所有指令见http://wiki.nginx.org/NginxHttpMainModule#error_log

2、调试输出函数用ngx_log_debugN,其中'N'表示有几个输出参数,比如:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0,
"num_head <%d>", num_head);
该函数的第一个参数就是与debug_http对应,表明属于哪块输出。
第二个参数输出日志,每次都要指定。
第三个参数是errno,调试函数可以打印系统错误号对应的错误描述,一般为0,不
指定错误。
第四个参数典型的不定参数,与printf类似。Nginx的参数与printf类似,但也有
一些不同,主要是‘%’后面的参数有些不同,如果想了解全部,建议看看
src/core/ngx_string.c中ngx_vsnprintf函数的实现。

3、错误输出用ngx_log_error函数实现。第一个参数与ngx_log_debugN不同,表示
输出的错误级别,比如是|一般性的 error还是warn等等。|

--
Weibin Yao

yaoweibin

unread,
May 20, 2009, 10:26:24 PM5/20/09
to cnginx

奇怪,我六点多写的回复,到9点多才显示出来,上一条delt...@gmail.com的回复是9点多的。

On 5月20日, 下午6时17分, Weibin Yao <nbubi...@gmail.com> wrote:

yaoweibin

unread,
May 20, 2009, 10:26:44 PM5/20/09
to cnginx

Delta Yeh

unread,
May 20, 2009, 10:41:12 PM5/20/09
to cng...@googlegroups.com
不好意思,新会员有审帖。
现在都放开了。
2009/5/21 yaoweibin <nbub...@gmail.com>
Reply all
Reply to author
Forward
0 new messages