nginx内置属性

https://blog.csdn.net/zhsh87/article/details/8191264

nginx 各参数翻译,作用

$arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值。

$args #这个变量等于请求行中(GET请求)的参数,例如foo=123&bar=blahblah;

$binary_remote_addr #二进制的客户地址。

$body_bytes_sent #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。

$content_length #请求头中的Content-length字段。

$content_type #请求头中的Content-Type字段。

$cookie_COOKIE #cookie COOKIE变量的值

$document_root #当前请求在root指令中指定的值。

$document_uri #与$uri相同。

$host #请求主机头字段,否则为服务器名称。

$hostname #Set to the machine’s hostname as returned by gethostname

$http_HEADER

$is_args #如果有$args参数,这个变量等于”?”,否则等于”",空值。

$http_user_agent #客户端agent信息

$http_cookie #客户端cookie信息

$limit_rate #这个变量可以限制连接速率。

$query_string #与$args相同。

$request_body_file #客户端请求主体信息的临时文件名。

$request_method #客户端请求的动作,通常为GET或POST。

$remote_addr #客户端的IP地址。

$remote_port #客户端的端口。

$remote_user #已经经过Auth Basic Module验证的用户名。

$request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。

$request_method #GET或POST

$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。

$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。

$scheme #HTTP方法(如http,https)。

$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。

$server_name #服务器名称。

$server_port #请求到达服务器的端口号。

$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。该值有可能和$request_uri 不一致。$request_uri是浏览器发过来的值。该值是rewrite后的值。例如做了internal redirects后。

 

 

今天在给某网站写rewrite重定向规则时,碰到了这个关于重定向的参数处理问题。默认的情况下,Nginx在进行rewrite后都会自动添加上旧地址中的参数部分,而这对于重定向到的新地址来说可能是多余。虽然这也不会对重定向的结果造成多少影响,但当你注意到新地址中包含有多余的“?xxx=xxx”时,心里总还是会觉得不爽。那么该如何来处理这部分的内容呢?看了下面两个简单的例子你就会明白了。

例如:
http://example.com/test.php?para=xxx 重定向到 http://example.com/new
若按照默认的写法:rewrite ^/test.php(.*) /new permanent;
重定向后的结果是:http://example.com/new?para=xxx
如果改写成:rewrite ^/test.php(.*) /new? permanent;
那结果就是:http://example.com/new

所以,关键点就在于“?”这个尾缀。假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数来实现。

例如:
http://example.com/test.php?para=xxx&p=xx 重写向到 http://example.com/new?p=xx
可以写成:rewrite  ^/test.php   /new?p=$arg_p?  permanent;

只求结果的朋友可以直接忽略前面的内容,看这里:

rewrite  ^/test.php  /new  permanent;       //重写向带参数的地址

rewrite  ^/test.php  /new?  permanent;      //重定向后不带参数

rewrite  ^/test.php   /new?id=$arg_id?  permanent;    //重定向后带指定的参数

 

permanent是永久重定向参数,根据需要去掉也可以,不过最好是带有。

参考301重定向与302重定向的区别

=====================================

 

nginx 内置变量使用说明及具体意义

https://blog.csdn.net/wanglei_storage/article/details/66004933

$args

参数: $args

解释: HTTP请求中的完整参数。

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "a=10"

$binary_remote_addr

参数: $binary_remote_addr

解释: 二进制格式的客户端地址。

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "\xC0\xA8\x01\xC8"

$body_bytes_sent

参数: $body_bytes_sent

解释: 表示在客户端发送的http响应中,包体部分的字节数

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "264"

$content_length

参数: $content_length

解释: 表示客户端请求头部中的Content-Length

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "264"

$content_type

参数: $content_type

解释: 表示客户端请求头部中的Content-Type

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "text/html"

$document_root

参数: $document_root

解释: 返回nginx服务器中资源跟目录,这取决于在server{}中定义的root路径

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "/usr/local/nginx/html"

$uri

参数: $uri

解释: 表示当前请求的URI,不带任何参数

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "/192.168.1.200"

$document_uri

参数: $document_uri

解释: $uri含义相同

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "/192.168.1.200"

$request_uri

参数: $request_uri

解释: 表示客户端发来的原始URL,带完整的参数,$request_uri永远不会变,始终是用户的原始URL

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "/192.168.1.200?a=10"

$host

参数: $host

解释: 表示客户端请求头部中的Host字段。如果Host字段不存在,则以实际处理的server name名称代替。如果Host字段中带有端口,IP:PORT,那么$host会去掉端口

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "test.wanglei.com"

$hostname

参数: $hostname

解释: 表示Nginx所在的机器的名称,gethostbyname调用返回的值相同

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "centos7-201"

$is_args

参数: $is_args

解释: 表示请求中的URL是否带参数,如果带参数,$is_args值为"?"。如果不带参数,则是空字符串

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "?"

访问: curl http://test.wanglei.com/192.168.1.200 -I

返回: ""

$limit_rate

参数: $limit_rate

解释: 表示当前限制速率是多少, 0表示无限速

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "51200"

配置: limit_rate 50k;

$nginx_version

参数: $nginx_version

解释: 表示当前nginx的版本号

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "1.8.1"

$query_string

参数: $query_string

解释: 请求URI中的参数,$args相同,然而$query_string是只读的不会变得

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "a=10"

$remote_addr

参数: $remote_addr

解释: 表示客户端的地址

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "192.168.1.200"

$remote_port

参数: $remote_port

解释: 表示客户端连接使用的端口,这个是随机的,每个人都不一样

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "50451"

$remote_user

参数: $remote_user

解释: 表示使用Auth Basic Module时定义的用户名

访问: curl -u wanglei:wanglei123 http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "wanglei"

$request_filename

参数: $request_filename

解释: 表示用户请求中的URI经过rootalias转换后的文件路径,默认是index.html

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "/usr/local/nginx/html/192.168.1.200"

$request_completion

参数: $request_completion

解释: 当请求已经全部完成时,其值为"OK"。若没有完成,就要返回客户端,则其值为空字符串;或者在断点续传等情况下使用HTTP range访问的并不是文件的最后一块,那么其值也是空字符串

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "OK"

$request_method

参数: $request_method

解释: 表示HTTP请求的方法名,GET/POST/PUT

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "HEAD"

访问: curl http://test.wanglei.com/192.168.1.200 -d "x=1" -I

返回: "POST"

$scheme

参数: $scheme

解释: 表示HTTP scheme,如在请求https中表示https,http中表示http

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "http"

$server_addr

参数: $server_addr

解释: 表示服务器地址

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "192.168.1.200"

$server_name

参数: $server_name

解释: 表示服务器名称

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "test.wanglei.com"

$server_port

参数: $server_port

解释: 表示服务器端口

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "80"

$server_protocol

参数: $server_protocol

解释: 表示服务器向客户端发送响应的协议,HTTP/1.1HTTP/1.0

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "HTTP/1.1"

$time_local

参数: $time_local

解释: 表示服务器当前的本地时间

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "25/Mar/2017:22:10:51 +0800"

$request

参数: $request

解释: 返回客户端请求的头部信息

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "HEAD /192.168.1.200?a=10 HTTP/1.1"

$status

参数: $status

解释: 表示客户端请求服务器的HTTP返回码

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "302"

$http_user_agent

参数: $http_user_agent

解释: 浏览页面的访问者在用什么操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好的代码

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "curl/7.29.0"

$request_time

参数: $request_time

解释: 返回从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应数据时间。

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "0.000"

$response_time

参数: $response_time

解释: 返回从Nginx向后端(upstream)建立连接开始到接受完数据然后关闭连接为止的时间。

访问: curl http://test.wanglei.com/192.168.1.200?a=10 -I

返回: "0.000"

================================================

 

Ngx_http_core_module模块支持内置变量,他们的名字和apache的内置变量是一致的,人性化吧,了解这些变量,将使我们配置起来非常方便,

例如:负载均衡中的header头转发 让后端服务器可以获取到客户端请求的真实ip  

首先我们来先看一个最基本的请求header  这是我去请求www.baidu.com 请求header如下:

 

  1. Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

  2. Accept-Encoding gzip, deflate, br

  3. Accept-Language zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3

  4. Connection keep-alive

  5. Host www.baidu.com

  6. Referer https://www.baidu.com/s?wd=aaa&rsv_spt=1&rsv_iqid=0x80378a480003084f&issp=1&f=8&rsv_bp=0&rsv_idx=2&

  7. ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&inputT=1804&rsv_sug4=1805

  8. User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0





为什么要贴这个呢?因为我接下来介绍的nginx内置变量中就有和它们相关的  边看边核对 理解起来更容易不是?
常用内置变量列表如下

$args   
此变量与请求行中的参数相等 注意我请求中的 referer  此变量表示我请求的所有参数

$content_length
等于请求行的“Content_Length”的值。

$content_type
等同与请求头部的”Content_Type”的值

$document_root
等同于当前请求的root指令指定的值

$document_uri
讲解这个变量之前需要讲解下nginx规范化
所谓规范化,就是先将URI中形如“%XX”的编码字符进行解码,再解析URI中的相对路径“.”和“..”部分, 另外还可能会压缩相邻的两个或多个斜线成为一个斜线,
这也就是如www.xxx.com///index.html 这样的访问也能访问的原因
规范前的url为 www.xxx.com///index.html 保存在$request_uri中
而规范后的url为 www.xxx.com/index.html 保存在$uri中  nginx会压缩相邻的两个或多个斜线成为一个斜线

$host
与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样 上述例子中没有体现

$limit_rate
允许限制的连接速率 一般不常用

$request_method
等同于request的method,通常是“GET”或“POST”

$remote_addr
客户端ip 这个再做负载均衡的时候 如果要获取到客户端的请求ip需要这个

$remote_port
客户端port

$remote_user
等同于用户名,由ngx_http_auth_basic_module认证

$request_filename
当前请求的文件的路径名,由root或alias和URI request组合而成


$request_body_file
请求body内的文件

$request_uri
含有参数的完整的初始URI 如上例子就是

 
  1. https://www.baidu.com/s?wd=aaa&rsv_spt=1&rsv_iqid=0x80378a480003084f&issp=1&f=8&rsv_bp=0&rsv_idx=2&

  2. ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&inputT=1804&rsv_sug4=1805


$query_string
与$args一样 请求中的参数

$sheeme
http模式(http,https)

$server_protocol
等同于request的协议,使用“HTTP/或“HTTP/


$server_addr request
到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。

$server_name
请求到达的服务器名

$server_port
请求到达的服务器的端口号

$uri
等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index

 

======================================

名称        版本        说明(变量列表来源于文件 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相同; 比如 /test1/test2/test.php

$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://www.cnblogs.com/deng02/archive/2009/02/11/1387911.html

$http_x_forwarded_for        1.0.8        相当于网络访问路径。http://www.cnblogs.com/craig/archive/2008/11/18/1335809.html

$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_filename:D:\nginx/html/test1/test2/test.php

$request_method        1.0.8        请求的方法,比如"GET"、"POST"等;

$request_uri        1.0.8        请求的URI,带参数; 比如http://localhost:88/test1/test2/test.php

$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,可能和最初的值有不同,比如经过重定向之类的。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值