OpenResty之使用 Nginx 内置绑定变量

Nginx作为一个成熟、久经考验的负载均衡软件,与其提供丰富、完整的内置变量是分不开的,它极大增加了对Nginx网络行为的控制细度。这些变量大部分都是在请求进入时解析的,并把他们缓存到请求cycle中,方便下一次获取使用。首先来看看Nginx对外都开放了那些API。

  • $arg_name:请求中的 name 参数
  • $args:请求中的参数
  • $binary_remote_addr:远程地址的二进制表示
  • $body_bytes_sent:已发送的消息体字节数
  • $content_length:HTTP 请求信息里的 "Content-Length"
  • $content_type:请求信息里的 "Content-Type"
  • $document_root:针对当前请求的根路径设置值
  • $document_uri:与 $uri 相同;比如 /test2/test.php
  • $host:请求信息中的 "Host",如果请求中没有 Host 行,则等于设置的服务器名
  • $hostname:机器名使用 gethostname 系统调用的值
  • $http_cookie:cookie 信息
  • $http_referer:引用地址
  • $http_user_agent:客户端代理信息
  • $http_via:最后一个访问服务器的 IP 地址
  • $http_x_forwarded_for:相当于网络访问路径
  • $is_args:如果请求行带有参数,返回 "?",否则返回空字符串
  • $limit_rate:对连接速率的限制
  • $nginx_version:当前运行的 nginx 版本号
  • $pid:worker 进程的 PID
  • $query_string:与 $args 相同
  • $realpath_root:按 root 指令或 alias 指令算出的当前请求的绝对路径。其中的符号链接都会解析成真实文件路径
  • $remote_addr:客户端 IP 地址
  • $remote_port:客户端端口号
  • $remote_user:客户端用户名,认证用
  • $request:用户请求
  • $request_body:这个变量(0.7.58+)包含请求的主要信息。在使用 proxy_pass 或 fastcgi_pass 指令的 location 中比较有意义
  • $request_body_file:客户端请求主体信息的临时文件名
  • $request_completion:如果请求成功,设为 "OK";如果请求未完成或者不是一系列请求中最后一部分则设为空
  • $request_filename:当前请求的文件路径名,比如 /opt/nginx/www/test.php
  • $request_method:请求的方法,比如 "GET"、"POST" 等
  • $request_uri:请求的 URI,带参数
  • $scheme:所用的协议,比如 http 或者是 https
  • $server_addr:服务器地址,如果没有用 listen 指明服务器地址,使用这个变量将发起一系列系统调用以取得地址(造成资源浪费)
  • $server_name:请求达到的服务器名
  • $server_port:请求达到的服务器端口号
  • $server_protocol:请求的协议版本,"HTTP/1.0" 或 "HTTP/1.1"
  • $uri:请求的 URI,可能和最初的值有不同,比如经过重定向之类的

利用这些内置变量,来做一个简单的数学求和运算例子:

server {
    listen    80;
    server_name  localhost;

    location /sum {
        #处理业务
       content_by_lua_block {
            local a = tonumber(ngx.var.arg_a) or 0
            local b = tonumber(ngx.var.arg_b) or 0
            ngx.say("sum: ", a + b )
        }
    }
}

测试:

➜  ~  curl 'http://127.0.0.1/sum?a=11&b=12'
sum: 23

示例:简易防火墙

worker_processes 1;
error_log logs/error.log;
events {
	worker_connections 1024;
}

http {
	server {
		listen 80;
		server_name localhost;

		location /sum {
			# 使用 access 阶段完成准入阶段处理
			access_by_lua_block {
				local black_ips = {["127.0.0.1"]=true}
				
				local ip = ngx.var.remote_addr
				if true == black_ips[ip] then
					ngx.exit(ngx.HTTP_FORBIDDEN)
				end
			}
			
			# 处理业务
			content_by_lua_block {
				local a = tonumber(ngx.var.arg_a) or 0
				local b = tonumber(ngx.var.arg_b) or 0
				ngx.say("sum:", a + b)
			}
		}
	}
}

运行测试:

➜  ~  curl '192.168.1.104/sum?a=11&b=12'
sum:23
➜  ~
➜  ~
➜  ~  curl '127.0.0.1/sum?a=11&b=12'
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>openresty/1.9.3.1</center>
</body>
</html>

大多数 Ngxin 内置都是不允许写入的,例如刚刚的终端IP地址,在请求中是不允许对其进行更新的。对于可写的变量中的limit_rate,值得一提,它能完成传输速率限制,并且它的影响是单个请求级别。

location /download {
    access_by_lua_block {
        ngx.var.limit_rate = 1000
    };
}

下载测试:

➜  ~  wget '127.0.0.1/download/1.cab'
--2015-09-13 13:59:51--  http://127.0.0.1/download/1.cab
Connecting to 127.0.0.1... connected.
HTTP request sent, awaiting response... 200 OK
Length: 135802 (133K) [application/octet-stream]
Saving to: '1.cab'

1.cab                6%[===>             ]   8.00K  1.01KB/s   eta 1m 53s
posted @ 2018-07-04 09:37  季末的天堂  阅读(1100)  评论(0)    收藏  举报