3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 #define NGX_HTTP_REALIP_XREALIP 0
13 #define NGX_HTTP_REALIP_XFWD 1
14 #define NGX_HTTP_REALIP_HEADER 2
20 } ngx_http_realip_from_t;
24 ngx_array_t *from; /* array of ngx_http_realip_from_t */
28 } ngx_http_realip_loc_conf_t;
32 ngx_connection_t *connection;
35 } ngx_http_realip_ctx_t;
38 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
39 static void ngx_http_realip_cleanup(void *data);
40 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd,
42 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
43 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf);
44 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
45 void *parent, void *child);
46 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
49 static ngx_command_t ngx_http_realip_commands[] = {
51 { ngx_string("set_real_ip_from"),
52 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
54 NGX_HTTP_LOC_CONF_OFFSET,
58 { ngx_string("real_ip_header"),
59 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
61 NGX_HTTP_LOC_CONF_OFFSET,
70 static ngx_http_module_t ngx_http_realip_module_ctx = {
71 NULL, /* preconfiguration */
72 ngx_http_realip_init, /* postconfiguration */
74 NULL, /* create main configuration */
75 NULL, /* init main configuration */
77 NULL, /* create server configuration */
78 NULL, /* merge server configuration */
80 ngx_http_realip_create_loc_conf, /* create location configuration */
81 ngx_http_realip_merge_loc_conf /* merge location configuration */
85 ngx_module_t ngx_http_realip_module = {
87 &ngx_http_realip_module_ctx, /* module context */
88 ngx_http_realip_commands, /* module directives */
89 NGX_HTTP_MODULE, /* module type */
90 NULL, /* init master */
91 NULL, /* init module */
92 NULL, /* init process */
93 NULL, /* init thread */
94 NULL, /* exit thread */
95 NULL, /* exit process */
96 NULL, /* exit master */
102 ngx_http_realip_handler(ngx_http_request_t *r)
108 ngx_list_part_t *part;
109 ngx_table_elt_t *header;
110 struct sockaddr_in *sin;
112 ngx_pool_cleanup_t *cln;
113 ngx_http_realip_ctx_t *ctx;
114 ngx_http_realip_from_t *from;
115 ngx_http_realip_loc_conf_t *rlcf;
117 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
123 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
125 return NGX_HTTP_INTERNAL_SERVER_ERROR;
128 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module);
130 if (rlcf->from == NULL) {
134 switch (rlcf->type) {
136 case NGX_HTTP_REALIP_XREALIP:
138 if (r->headers_in.x_real_ip == NULL) {
142 len = r->headers_in.x_real_ip->value.len;
143 ip = r->headers_in.x_real_ip->value.data;
147 case NGX_HTTP_REALIP_XFWD:
149 if (r->headers_in.x_forwarded_for == NULL) {
153 len = r->headers_in.x_forwarded_for->value.len;
154 ip = r->headers_in.x_forwarded_for->value.data;
156 for (p = ip + len - 1; p > ip; p--) {
157 if (*p == ' ' || *p == ',') {
167 default: /* NGX_HTTP_REALIP_HEADER */
169 part = &r->headers_in.headers.part;
173 len = rlcf->header.len;
174 p = rlcf->header.data;
176 for (i = 0; /* void */ ; i++) {
178 if (i >= part->nelts) {
179 if (part->next == NULL) {
188 if (hash == header[i].hash
189 && len == header[i].key.len
190 && ngx_strncmp(p, header[i].lowcase_key, len) == 0)
192 len = header[i].value.len;
193 ip = header[i].value.data;
206 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip);
210 if (r->connection->sockaddr->sa_family != AF_INET) {
214 sin = (struct sockaddr_in *) c->sockaddr;
216 from = rlcf->from->elts;
217 for (i = 0; i < rlcf->from->nelts; i++) {
219 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
220 "realip: %08XD %08XD %08XD",
221 sin->sin_addr.s_addr, from[i].mask, from[i].addr);
223 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) {
227 ngx_http_set_ctx(r, ctx, ngx_http_realip_module);
229 addr = inet_addr((char *) ip);
231 if (addr == INADDR_NONE) {
235 p = ngx_pnalloc(c->pool, len);
237 return NGX_HTTP_INTERNAL_SERVER_ERROR;
240 ngx_memcpy(p, ip, len);
242 cln->handler = ngx_http_realip_cleanup;
245 ctx->addr = sin->sin_addr.s_addr;
246 ctx->addr_text = c->addr_text;
248 sin->sin_addr.s_addr = addr;
250 c->addr_text.len = len;
251 c->addr_text.data = p;
262 ngx_http_realip_cleanup(void *data)
264 ngx_http_realip_ctx_t *ctx = data;
267 struct sockaddr_in *sin;
271 sin = (struct sockaddr_in *) c->sockaddr;
272 sin->sin_addr.s_addr = ctx->addr;
274 c->addr_text = ctx->addr_text;
279 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
281 ngx_http_realip_loc_conf_t *rlcf = conf;
285 ngx_inet_cidr_t in_cidr;
286 ngx_http_realip_from_t *from;
288 if (rlcf->from == NULL) {
289 rlcf->from = ngx_array_create(cf->pool, 2,
290 sizeof(ngx_http_realip_from_t));
291 if (rlcf->from == NULL) {
292 return NGX_CONF_ERROR;
296 from = ngx_array_push(rlcf->from);
298 return NGX_CONF_ERROR;
301 value = cf->args->elts;
303 rc = ngx_ptocidr(&value[1], &in_cidr);
305 if (rc == NGX_ERROR) {
306 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
308 return NGX_CONF_ERROR;
311 if (rc == NGX_DONE) {
312 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
313 "low address bits of %V are meaningless", &value[1]);
316 from->mask = in_cidr.mask;
317 from->addr = in_cidr.addr;
324 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
326 ngx_http_realip_loc_conf_t *rlcf = conf;
330 value = cf->args->elts;
332 if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) {
333 rlcf->type = NGX_HTTP_REALIP_XREALIP;
337 if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) {
338 rlcf->type = NGX_HTTP_REALIP_XFWD;
342 rlcf->type = NGX_HTTP_REALIP_HEADER;
343 rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len);
344 rlcf->header = value[1];
351 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
353 ngx_http_realip_loc_conf_t *conf;
355 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t));
357 return NGX_CONF_ERROR;
361 * set by ngx_pcalloc():
365 * conf->header = { 0, NULL };
368 conf->type = NGX_CONF_UNSET_UINT;
375 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
377 ngx_http_realip_loc_conf_t *prev = parent;
378 ngx_http_realip_loc_conf_t *conf = child;
380 if (conf->from == NULL) {
381 conf->from = prev->from;
384 ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP);
386 if (conf->header.len == 0) {
387 conf->hash = prev->hash;
388 conf->header = prev->header;
396 ngx_http_realip_init(ngx_conf_t *cf)
398 ngx_http_handler_pt *h;
399 ngx_http_core_main_conf_t *cmcf;
401 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
403 h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
408 *h = ngx_http_realip_handler;
410 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
415 *h = ngx_http_realip_handler;