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参数的值 arga=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的值 cookieCA=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"RequestMethod:request_method<br/>";
echo "Full Uri: requesturi<br/>";echo"uri:uri<br/>";
echo "args: args<br/>";echo"ServerProtocol:server_protocol<br/>";
echo "Document Uri: documenturi<br/>";echo"QueryString:query_string<br/>";
echo "arg_a=arga,argb=arg_b";
}
# variables parsed from http header;
location /nginx-var/header-var {
default_type text/html;
echo "Host: host<br/>";echo"RemoteAddr:remote_addr<br/>";
echo "Remote Port: remoteport<br/>";echo"ContentType:content_type<br/>";
echo "User-Agent: httpuseragent<br/>";echo"HttpCookie:http_cookie<br/>";
echo "cookie_CA=cookieCA,cookieCB=cookie_CB<br/>";
echo "Http-Referer: $http_referer<br/>";
}

 

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

posted @   风风羊  阅读(427)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示