Nginx在使用过程中,有不少的内置全局变量可以用做条件判断和编程控制,本文总结一些已知的指令,以供参考。
$arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。
$args 这个变量等于请求行中的参数。
$binary_remote_addr 二进制码形式的客户端地址。
$body_bytes_sent 传送页面的字节数
$content_length 请求头中的Content-length字段。
$content_type 请求头中的Content-Type字段。
$cookie_COOKIE cookie COOKIE的值。
$document_root 当前请求在root指令中指定的值。
$document_uri 与$uri相同。
$host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。
$is_args 如果$args设置,值为"?",否则为""。
$limit_rate 这个变量可以限制连接速率。
$nginx_version 当前运行的nginx版本号。
$query_string 与$args相同。
$remote_addr 客户端的IP地址。
$remote_port 客户端的端口。
$remote_user 已经经过Auth Basic Module验证的用户名,由ngx_http_auth_basic_module认证。
$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。
$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。
$request_body_file 客户端请求主体信息的临时文件名。
$request_completion 未知。
$request_method 这个变量是客户端请求的动作,通常为GET或POST。
包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。
$request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
$scheme 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;
$server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
$server_name 服务器名称。
$server_port 请求到达服务器的端口号。
$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。
另外:
HTTP_X_FORWARDED_FOR是透过代理服务器取得客户端的真实IP地址,有些用此方法读取到的仍然是代理服务器的IP。还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 HTTP_X_FORWARDED_FOR 取到的值将是空的。
The core module supports built-in variables, whose names correspond with the names of variables in Apache.First of all, there are the variables, which represent the lines of the title of the client request, for example, $http_user_agent, $http_cookie, and so forth.
Furthermore, there are other variables:
$arg_PARAMETER
This variable contains the value of the GET request variable PARAMETER if present in the query string
$args
This variable is equal to arguments in the line of request;
$binary_remote_addr
The address of the client in binary form;
$body_bytes_sent
$content_length
This variable is equal to line Content-Length in the header of request;
$content_type
This variable is equal to line Content-Type in the header of request;
$cookie_COOKIE
The value of the cookie COOKIE;
$document_root
This variable is equal to the value of directive root for the current request;
$document_uri
The same as $uri.
$host
This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.
This variable may have a different value from $http_host when the Host input header is absent or has an empty value.
$http_HEADER
The value of the HTTP header HEADER when converted to lowercase and with ‘dashes’ converted to ‘underscores’, e.g. $http_user_agent, $http_referer…;
$is_args
Evaluates to “?” if $args is set, “” otherwise.
$limit_rate
This variable allows limiting the connection rate.
$query_string
The same as $args.
$remote_addr
The address of the client.
$remote_port
The port of the client;
$remote_user
This variable is equal to the name of user, authenticated by the Auth Basic Module;
$request_filename
This variable is equal to path to the file for the current request, formed from directives root or alias and URI request;
$request_body
This variable(0.7.58+) contains the body of the request. The significance of this variable appears in locations with directives proxy_pass or fastcgi_pass.
$request_body_file
Client request body temporary filename;
$request_completion
(undocumented)
$request_method
This variable is equal to the method of request, usually GET or POST.
Before and including 0.8.20, this variable always evaluates to the method name of the main request, not the current request if the current request is a subrequest.
$request_uri
This variable is equal to the complete initial URI together with the arguments;
$scheme
The HTTP scheme (i.e. http, https). Evaluated only on demand, for example:
rewrite ^(.+)$ $scheme://example.com$1 redirect;
$server_addr
Equal to the server address. As a rule, for obtaining the value of this variable is done one system call. In order to avoid system call, it is necessary to indicate addresses in directives listen and to use parameter bind.
$server_name
The name of the server.
$server_port
This variable is equal to the port of the server, to which the request arrived;
$server_protocol
This variable is equal to the protocol of request, usually this HTTP/1.0 or HTTP/1.1.
$uri
This variable is equal to current URI in the request, it can differ from initial, for example by internal redirects, or with the use of index it is file with internal redirects.
参考:
http://wiki.nginx.org/NginxHttpCoreModule#Variables
转自:http://www.freeoa.net/osuport/servap/nginx-inside-global-var_2709.html