`

nginx 全局变量

 
阅读更多

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

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

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

复制代码
  1 static ngx_http_variable_t  ngx_http_core_variables[] = {
2
3 { ngx_string("http_host"), NULL, ngx_http_variable_header,
4 offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
5
6 { ngx_string("http_user_agent"), NULL, ngx_http_variable_header,
7 offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },
8
9 { ngx_string("http_referer"), NULL, ngx_http_variable_header,
10 offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },
11
12 #if (NGX_HTTP_GZIP)
13 { ngx_string("http_via"), NULL, ngx_http_variable_header,
14 offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
15 #endif
16
17 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
18 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
19 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
20 #endif
21
22 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
23 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },
24
25 { ngx_string("content_length"), NULL, ngx_http_variable_header,
26 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },
27
28 { ngx_string("content_type"), NULL, ngx_http_variable_header,
29 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
30
31 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },
32
33 { ngx_string("binary_remote_addr"), NULL,
34 ngx_http_variable_binary_remote_addr, 0, 0, 0 },
35
36 { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },
37
38 { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 },
39
40 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },
41
42 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },
43
44 { ngx_string("server_protocol"), NULL, ngx_http_variable_request,
45 offsetof(ngx_http_request_t, http_protocol), 0, 0 },
46
47 { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 },
48
49 { ngx_string("request_uri"), NULL, ngx_http_variable_request,
50 offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },
51
52 { ngx_string("uri"), NULL, ngx_http_variable_request,
53 offsetof(ngx_http_request_t, uri),
54 NGX_HTTP_VAR_NOCACHEABLE, 0 },
55
56 { ngx_string("document_uri"), NULL, ngx_http_variable_request,
57 offsetof(ngx_http_request_t, uri),
58 NGX_HTTP_VAR_NOCACHEABLE, 0 },
59
60 { ngx_string("request"), NULL, ngx_http_variable_request_line, 0, 0, 0 },
61
62 { ngx_string("document_root"), NULL,
63 ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
64
65 { ngx_string("realpath_root"), NULL,
66 ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
67
68 { ngx_string("query_string"), NULL, ngx_http_variable_request,
69 offsetof(ngx_http_request_t, args),
70 NGX_HTTP_VAR_NOCACHEABLE, 0 },
71
72 { ngx_string("args"),
73 ngx_http_variable_request_set,
74 ngx_http_variable_request,
75 offsetof(ngx_http_request_t, args),
76 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
77
78 { ngx_string("is_args"), NULL, ngx_http_variable_is_args,
79 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
80
81 { ngx_string("request_filename"), NULL,
82 ngx_http_variable_request_filename, 0,
83 NGX_HTTP_VAR_NOCACHEABLE, 0 },
84
85 { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 },
86
87 { ngx_string("request_method"), NULL,
88 ngx_http_variable_request_method, 0,
89 NGX_HTTP_VAR_NOCACHEABLE, 0 },
90
91 { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },
92
93 { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,
94 0, 0, 0 },
95
96 { ngx_string("request_completion"), NULL,
97 ngx_http_variable_request_completion,
98 0, 0, 0 },
99
100 { ngx_string("request_body"), NULL,
101 ngx_http_variable_request_body,
102 0, 0, 0 },
103
104 { ngx_string("request_body_file"), NULL,
105 ngx_http_variable_request_body_file,
106 0, 0, 0 },
107
108 { ngx_string("sent_http_content_type"), NULL,
109 ngx_http_variable_sent_content_type, 0, 0, 0 },
110
111 { ngx_string("sent_http_content_length"), NULL,
112 ngx_http_variable_sent_content_length, 0, 0, 0 },
113
114 { ngx_string("sent_http_location"), NULL,
115 ngx_http_variable_sent_location, 0, 0, 0 },
116
117 { ngx_string("sent_http_last_modified"), NULL,
118 ngx_http_variable_sent_last_modified, 0, 0, 0 },
119
120 { ngx_string("sent_http_connection"), NULL,
121 ngx_http_variable_sent_connection, 0, 0, 0 },
122
123 { ngx_string("sent_http_keep_alive"), NULL,
124 ngx_http_variable_sent_keep_alive, 0, 0, 0 },
125
126 { ngx_string("sent_http_transfer_encoding"), NULL,
127 ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },
128
129 { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers,
130 offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },
131
132 { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
133 ngx_http_variable_request_get_size,
134 offsetof(ngx_http_request_t, limit_rate),
135 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
136
137 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
138 0, 0, 0 },
139
140 { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
141 0, 0, 0 },
142
143 { ngx_string("pid"), NULL, ngx_http_variable_pid,
144 0, 0, 0 },
145
146 { ngx_null_string, NULL, NULL, 0, 0, 0 }
147 };
复制代码

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

 

已Excel形式提供,方便查询:

http://files.cnblogs.com/AloneSword/nginx%E5%86%85%E7%BD%AE%E5%8F%98%E9%87%8F.rar

分享到:
评论

相关推荐

    nginx全局变量整理小结

    Nginx 全局变量整理小结 Nginx 全局变量是 Nginx 服务器中的一组预定义变量,用于存储当前请求的信息和服务器的状态。这些变量可以在 Nginx 配置文件中使用,帮助管理员更好地管理和优化服务器。这篇文章将对 Nginx...

    Nginx中全局变量整理小结

    在Nginx中,全局变量扮演着至关重要的角色,它们为服务器配置提供了丰富的信息来源,使得我们可以根据不同的请求条件进行动态响应。以下是一些主要的全局变量及其详细说明: 1. **$http_USER_AGENT**: 这个变量包含...

    Nginx中rewrite实现二级域名、三级域名、泛域名、路径的重写[文].pdf

    Nginx 中 rewrite 实现二级域名、三级域名、泛域名...Nginx 中的 rewrite 模块可以实现二级域名、三级域名、泛域名、路径的重写,并且可以使用 rewrite flags、正则表达式匹配、全局变量等来实现复杂的 URL 重写逻辑。

    nginx自定义变量与内置预定义变量的使用

    每个变量都是全局可见的,但它们并不是真正的全局变量。这意味着在一个配置块中定义的变量可以在其他配置块中被访问,但它们的值只在声明它们的块及其子块中有效。例如: ```nginx location a/ { return 200 $a; }...

    Nginx访问控制与参数调优的方法

    Nginx全局变量 Nginx中有很多的全局变量,可以通过$变量名来使用。下面列举一些常用的全局变量: 变量 说明 $args 请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2 $content_length HTTP...

    Nginx模块源码 nginx-notice-2

    1. **初始化模块**:这部分代码在Nginx启动时执行,用于设置模块的配置信息和全局变量。 2. **配置处理函数**:这些函数负责解析和处理在Nginx配置文件中为模块指定的指令。 3. **事件处理函数**:这些函数处理来自...

    agentzh 的 Nginx 教程(版本 2016.07.21)整理成pdf 添加书签

    - **变量作用域**:探讨了变量的作用域,例如全局变量、请求级别的变量等。 - **变量生命周期**:解释了变量何时被创建和销毁。 - **变量动态生成**:学习如何根据请求动态生成变量值。 - **内置变量与自定义...

    nginx笔记.zip

    安装完成后,Nginx的主配置文件位于`/etc/nginx/nginx.conf`,在这个文件中可以设置全局变量、服务器块和location块。 **反向代理** Nginx的一个重要功能是作为反向代理,它可以将客户端请求转发到后端服务器集群...

    nginx搭建配置详细说明

    3.1.3. nginx的全局配置 3.2. events配置 3.3. http的配置 3.4. nginx重要指令之location 4. nginx中的rewrite 4.1. 什么是rewrite 4.2. rewrite的命令的作用域和优先级 4.3. if指令 4.3.1. if指令的语法 ...

    Nginx Rewrite使用场景及配置方法解析

    ### 常用的 Nginx 全局变量 - `$host`:服务器主机名。 - `$server_port`:服务器端口。 - `$request_uri`:原始请求 URI。 - `$document_root`:文档根目录。 - `$request_filename`:请求的文件路径。 ### 案例 ...

    nginx-1.9.0+配置.zip

    3. 配置环境变量:为了方便在命令行中直接运行 Nginx 命令,需要将 Nginx 的可执行文件路径添加到系统环境变量 `Path` 中。 二、配置文件写法 Nginx 的配置主要位于 `nginx.conf` 文件中,通常位于 Nginx 根目录的...

    Nginx 面试题让你全面掌握核心技术.rar

    Rewrite全局变量是什么? Nginx如何实现后端服务的健康检查?Nginx如何开启压缩? unstream..module的作用是什么? Aa.AahMHRrnmintnniimmtr 什么是C10K问题? Nginx是否支持将请求压缩到上游?如何在Nginx中获得当前的...

    Nginx指南 Nginx 服务器

    Nginx能够根据URL进行请求分发,支持 Rewrite 规则的设置,可以根据正则表达式或者文件目录进行匹配,同时也支持全局变量作为条件判断的依据。此外,Nginx还支持 Redirect 功能,可以方便地进行重定向操作。 Nginx...

    nginx安装包.zip

    2. **配置环境变量**:将Nginx的sbin目录添加到系统的PATH环境变量中,以便于执行Nginx命令。 3. **编辑配置文件**:根据您的需求修改`nginx.conf`,确保所有路径和设置都正确无误。 4. **启动Nginx**:运行Nginx...

Global site tag (gtag.js) - Google Analytics