nginx 里的常用变量

1. 从请求行中解析到的变量

以访问http://invo.com/nginx-var/request-line?a=1&b=2得到的结果为例,invo.com为测试的虚拟主机

变量 含义 示例

变量含义示例
$request 整个请求行 GET /nginx-var/request-line?a=1&b=2 HTTP/1.1
$request_method 请求方法(如GET、POST) GET
$request_uri 完整的请求URI /nginx-var/request-line?a=1&b=2
$uri URI,除去查询字符串 /nginx-var/request-line
$document_uri 同$uri /nginx-var/request-line
$args 查询字符串 a=1&b=2
$query_string 同$args a=1&b=2
$server_protocol 请求协议(如HTTP/1.0 HTTP/1.1) HTTP/1.1
$arg_name 请求行中name参数的值 $arg_a = 1 , $arg_b = 2



说明: 这些变量在配置文件中通常配合try_files指令和rewrite指令使用。

2. 从请求头中解析到的变量

用Firefox的HttpRequester插件,添加Cookie为CA=abc;CB=123,Referer为http://invo.com的请求头,
以访问地址http://invo.com/nginx-var/header-var得到的结果为例。

变量 含义 示例

变量含义示例
$host 该变量按如下优先级获得:请求行中解析到的host、请求头“Host”中的host、配置文件中匹配到的server_name invo.com
$remote_addr 客户端ip地址 127.0.0.1
$remote_port 客户端端口 4204
$http_user_agent 用户代理(“User-Agent”请求头的值) Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0
$http_cookie “Cookie”请求头的值 CA=abc;CB=123
$cookie_name Cookie中名为name的值 $cookie_CA=abc, $cookie_CB=123
$http_referer “Http-Referer”请求头的值 http://invo.com

3. 其它内置变量

变量 含义 

变量含义
$body_bytes_sent 发给客户端的数据大小,以字节计,不包括http报头
$bytes_sent 发给客户端的数据大小,以字节计
$status http响应状态码
$request_time 请求处理时间
$upstream_response_time 从与upstream建立连接到收到最后一个字节所经历的时间(nginx做反向代理服务器时可用)
$upstream_connect_time 与upstream建立连接所消耗的时间(nginx做反向代理服务器时可用)



说明:以上变量通常用于日志配置中,用于统计流量和监视服务器性能。

4、本文中用到的配置内容

相关nginx配置内容如下,其中用到的echo指令需要用到第三方echo模块,推荐使用OpenResty,各种常用第三方模块都有。

listen 80;
server_name invo.com invo.com;
root "D:/phpStudy/WWW/invo";
access_log logs/access.log invo_com;
location / {
index index.html index.htm index.php;
#autoindex on;
}
# variables parsed from request line
location /nginx-var/request-line {
default_type text/html;
echo "Request Line: $request<br/>";
echo "Request Method: $request_method<br/>";
echo "Full Uri: $request_uri<br/>";
echo "uri: $uri<br/>";
echo "args: $args<br/>";
echo "Server Protocol: $server_protocol<br/>";
echo "Document Uri: $document_uri<br/>";
echo "Query_String: $query_string<br/>";
echo "arg_a=$arg_a, arg_b=$arg_b";
}
# variables parsed from http header;
location /nginx-var/header-var {
default_type text/html;
echo "Host: $host<br/>";
echo "Remote Addr: $remote_addr<br/>";
echo "Remote Port: $remote_port<br/>";
echo "Content-Type: $content_type<br/>";
echo "User-Agent: $http_user_agent<br/>";
echo "Http-Cookie: $http_cookie<br/>";
echo "cookie_CA=$cookie_CA, cookie_CB=$cookie_CB<br/>";
echo "Http-Referer: $http_referer<br/>";
}

原文链接:https://blog.csdn.net/chunyuan314/article/details/55056539

原文地址:https://www.cnblogs.com/fengfengyang/p/15552853.html