现在的位置: 首页nginx>正文
nginx 全局变量说明
2012年06月26日 nginx 暂无评论 ⁄ 被围观 4,032 view+

经常需要配置Nginx ,其中有许多以 $ 开头的变量,经常需要查阅nginx 所支持的变量。

可能是对 Ngixn资源不熟悉,干脆就直接读源码,分析出支持的变量。

Nginx支持的http变量实现在 ngx_http_variables.c 的 ngx_http_core_variables存储实现:

ngx_http_core_variables
 static ngx_http_variable_t  ngx_http_core_variables[] = {

     { ngx_string("http_host"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.host), 0, 0 },

     { ngx_string("http_user_agent"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },

     { ngx_string("http_referer"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },

 #if (NGX_HTTP_GZIP)
     { ngx_string("http_via"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
 #endif

 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
     { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
 #endif

     { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
       offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },

     { ngx_string("content_length"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },

     { ngx_string("content_type"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },

     { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },

     { ngx_string("binary_remote_addr"), NULL,
       ngx_http_variable_binary_remote_addr, 0, 0, 0 },

     { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },

     { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 },

     { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },

     { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },

     { ngx_string("server_protocol"), NULL, ngx_http_variable_request,
       offsetof(ngx_http_request_t, http_protocol), 0, 0 },

     { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 },

     { ngx_string("request_uri"), NULL, ngx_http_variable_request,
       offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },

     { ngx_string("uri"), NULL, ngx_http_variable_request,
       offsetof(ngx_http_request_t, uri),
       NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("document_uri"), NULL, ngx_http_variable_request,
       offsetof(ngx_http_request_t, uri),
       NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("request"), NULL, ngx_http_variable_request_line, 0, 0, 0 },

     { ngx_string("document_root"), NULL,
       ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("realpath_root"), NULL,
       ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("query_string"), NULL, ngx_http_variable_request,
       offsetof(ngx_http_request_t, args),
       NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("args"),
       ngx_http_variable_request_set,
       ngx_http_variable_request,
       offsetof(ngx_http_request_t, args),
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("is_args"), NULL, ngx_http_variable_is_args,
       0, NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("request_filename"), NULL,
       ngx_http_variable_request_filename, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 },

     { ngx_string("request_method"), NULL,
       ngx_http_variable_request_method, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },

     { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,
       0, 0, 0 },

     { ngx_string("request_completion"), NULL,
       ngx_http_variable_request_completion,
       0, 0, 0 },

     { ngx_string("request_body"), NULL,
       ngx_http_variable_request_body,
       0, 0, 0 },

     { ngx_string("request_body_file"), NULL,
       ngx_http_variable_request_body_file,
       0, 0, 0 },

     { ngx_string("sent_http_content_type"), NULL,
       ngx_http_variable_sent_content_type, 0, 0, 0 },

     { ngx_string("sent_http_content_length"), NULL,
       ngx_http_variable_sent_content_length, 0, 0, 0 },

     { ngx_string("sent_http_location"), NULL,
       ngx_http_variable_sent_location, 0, 0, 0 },

     { ngx_string("sent_http_last_modified"), NULL,
       ngx_http_variable_sent_last_modified, 0, 0, 0 },

     { ngx_string("sent_http_connection"), NULL,
       ngx_http_variable_sent_connection, 0, 0, 0 },

     { ngx_string("sent_http_keep_alive"), NULL,
       ngx_http_variable_sent_keep_alive, 0, 0, 0 },

     { ngx_string("sent_http_transfer_encoding"), NULL,
       ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },

     { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers,
       offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },

     { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
       ngx_http_variable_request_get_size,
       offsetof(ngx_http_request_t, limit_rate),
       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },

     { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
       0, 0, 0 },

     { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
       0, 0, 0 },

     { ngx_string("pid"), NULL, ngx_http_variable_pid,
       0, 0, 0 },

     { ngx_null_string, NULL, NULL, 0, 0, 0 }
 };

把这些变量提取下,总结如下:

名称 版本 说明(变量列表来源于文件 ngx_http_variables)
$args 1.0.8 请求中的参数
$binary_remote_addr 1.0.8 远程地址的二进制表示
$body_bytes_sent 1.0.8 已发送的消息体字节数
$content_length 1.0.8 HTTP请求信息里的"Content-Length"
$content_type 1.0.8 请求信息里的"Content-Type"
$document_root 1.0.8 针对当前请求的根路径设置值
$document_uri 1.0.8 与$uri相同
$host 1.0.8 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$hostname 1.0.8
$http_cookie 1.0.8 cookie 信息
$http_post 1.0.8
$http_referer 1.0.8 引用地址
$http_user_agent 1.0.8 客户端代理信息
$http_via 1.0.8 最后一个访问服务器的Ip地址
$http_x_forwarded_for 1.0.8 相当于网络访问路径
$is_args 1.0.8
$limit_rate 1.0.8 对连接速率的限制
$nginx_version 1.0.8
$pid 1.0.8
$query_string 1.0.8 与$args相同
$realpath_root 1.0.8
$remote_addr 1.0.8 客户端地址
$remote_port 1.0.8 客户端端口号
$remote_user 1.0.8 客户端用户名,认证用
$request 1.0.8 用户请求
$request_body 1.0.8
$request_body_file 1.0.8 发往后端的本地文件名称
$request_completion 1.0.8
$request_filename 1.0.8 当前请求的文件路径名
$request_method 1.0.8 请求的方法,比如"GET"、"POST"等
$request_uri 1.0.8 请求的URI,带参数
$scheme 1.0.8 所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect
$sent_http_cache_control 1.0.8
$sent_http_connection 1.0.8
$sent_http_content_length 1.0.8
$sent_http_content_type 1.0.8
$sent_http_keep_alive 1.0.8
$sent_http_last_modified 1.0.8
$sent_http_location 1.0.8
$sent_http_transfer_encoding 1.0.8
$server_addr 1.0.8 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)
$server_name 1.0.8 请求到达的服务器名
$server_port 1.0.8 请求到达的服务器端口号
$server_protocol 1.0.8 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"
$uri 1.0.8 请求的URI,可能和最初的值有不同,比如经过重定向之类的

来自:http://www.cnblogs.com/AloneSword/archive/2011/12/10/2283483.html

给我留言

留言无头像?


×
腾讯微博