2

ngx_http_request_t我需要通过nginx 模块中的结构来获取他/她在 ngnix 中请求的客户端 ip 和 uri 。

如您所知,nginx模块编写如下代码

ngx_http_sql_handler(ngx_http_request_t *r){//Code Here}

我怎样才能得到ip和uri r,我该怎么做?

4

2 回答 2

2

您可以从以下位置获取 IP:

&r->connection->addr_text

其中addr_textngx_str_t

于 2016-11-23T10:37:42.637 回答
0

Angx_http_request_tngx_http_request_s. 请参阅http://lxr.nginx.org/source/src/http/ngx_http.h#0016

它看起来如下(来自http://lxr.nginx.org/source/src/http/ngx_http_request.h#0358):

0358 struct ngx_http_request_s {
0359     uint32_t                          signature;         /* "HTTP" */
0360 
0361     ngx_connection_t                 *connection;
0362 
0363     void                            **ctx;
0364     void                            **main_conf;
0365     void                            **srv_conf;
0366     void                            **loc_conf;
0367 
0368     ngx_http_event_handler_pt         read_event_handler;
0369     ngx_http_event_handler_pt         write_event_handler;
...
0394     ngx_str_t                         request_line;
0395     ngx_str_t                         uri;
0396     ngx_str_t                         args;
0397     ngx_str_t                         exten;
0398     ngx_str_t                         unparsed_uri;
...
};

ngx_connection_t是 a ngx_connection_s,您可以在http://lxr.nginx.org/source/src/core/ngx_connection.h#0117找到它的定义。

ngx_connection_t一个ngx_socket_t. 一旦你有了套接字,你就可以从sin_addr.s_addr.

URI 仅作为 nginx 字符串提供。

于 2014-01-05T07:37:02.880 回答