mirror of
https://github.com/nginx/nginx.git
synced 2025-06-07 17:52:38 +08:00
Realip: the $realip_remote_addr variable.
This commit is contained in:
parent
a52bbefd84
commit
1ce1610763
@ -43,9 +43,14 @@ static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
||||
static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf);
|
||||
static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
|
||||
void *parent, void *child);
|
||||
static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf);
|
||||
static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
|
||||
|
||||
|
||||
static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
|
||||
ngx_http_variable_value_t *v, uintptr_t data);
|
||||
|
||||
|
||||
static ngx_command_t ngx_http_realip_commands[] = {
|
||||
|
||||
{ ngx_string("set_real_ip_from"),
|
||||
@ -75,7 +80,7 @@ static ngx_command_t ngx_http_realip_commands[] = {
|
||||
|
||||
|
||||
static ngx_http_module_t ngx_http_realip_module_ctx = {
|
||||
NULL, /* preconfiguration */
|
||||
ngx_http_realip_add_variables, /* preconfiguration */
|
||||
ngx_http_realip_init, /* postconfiguration */
|
||||
|
||||
NULL, /* create main configuration */
|
||||
@ -105,6 +110,15 @@ ngx_module_t ngx_http_realip_module = {
|
||||
};
|
||||
|
||||
|
||||
static ngx_http_variable_t ngx_http_realip_vars[] = {
|
||||
|
||||
{ ngx_string("realip_remote_addr"), NULL,
|
||||
ngx_http_realip_remote_addr_variable, 0, 0, 0 },
|
||||
|
||||
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_http_realip_handler(ngx_http_request_t *r)
|
||||
{
|
||||
@ -416,6 +430,25 @@ ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_http_realip_add_variables(ngx_conf_t *cf)
|
||||
{
|
||||
ngx_http_variable_t *var, *v;
|
||||
|
||||
for (v = ngx_http_realip_vars; v->name.len; v++) {
|
||||
var = ngx_http_add_variable(cf, &v->name, v->flags);
|
||||
if (var == NULL) {
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
var->get_handler = v->get_handler;
|
||||
var->data = v->data;
|
||||
}
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_http_realip_init(ngx_conf_t *cf)
|
||||
{
|
||||
@ -440,3 +473,40 @@ ngx_http_realip_init(ngx_conf_t *cf)
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
|
||||
ngx_http_variable_value_t *v, uintptr_t data)
|
||||
{
|
||||
ngx_str_t *addr_text;
|
||||
ngx_pool_cleanup_t *cln;
|
||||
ngx_http_realip_ctx_t *ctx;
|
||||
|
||||
ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
|
||||
|
||||
if (ctx == NULL && (r->internal || r->filter_finalize)) {
|
||||
|
||||
/*
|
||||
* if module context was reset, the original address
|
||||
* can still be found in the cleanup handler
|
||||
*/
|
||||
|
||||
for (cln = r->pool->cleanup; cln; cln = cln->next) {
|
||||
if (cln->handler == ngx_http_realip_cleanup) {
|
||||
ctx = cln->data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text;
|
||||
|
||||
v->len = addr_text->len;
|
||||
v->valid = 1;
|
||||
v->no_cacheable = 0;
|
||||
v->not_found = 0;
|
||||
v->data = addr_text->data;
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user