nginx map指令用于根据现有的变量的值来赋值新变量。如根据
x-forwarded-for
和remote_addr
的值,创建新变量clientip
。
用法解析
Syntax: map string $variable { … }
Default: —
Context: http
string
可以是一个或多个变量组成的字符串$variable
是新变量名{...}
中的内容为source
与resulting
的映射source
可以为字符串或正则(~
区分大小写,~*
不区分大小写)resulting
赋予$variable
新变量的值
{...}
中可以使用以下关键字,当source
与关键字重复时需要使用\
转义default value
设置默认值hostnames
指明source
的值是主机名,主机名可包含前缀或后缀include file
包含变量文件volatile
指明变量不可缓存
实例演示
获取client真实ip
nginx http区块配置如下:
map $http_x_forwarded_for $clientip {
"" $remote_addr;
default $http_x_forwarded_for;
}
log_format main '$clientip - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$request_t