获取Nginx内置绑定变量

1 篇文章 0 订阅

 

获取Nginx内置绑定变量

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

参看下表:

名称说明
$arg_name请求中的name参数
$args请求中的参数
$binary_remote_addr远程地址的二进制表示
$body_bytes_sent已发送的消息体字节数
$content_lengthHTTP请求信息里的"Content-Length"
$content_type请求信息里的"Content-Type"
$document_root针对当前请求的根路径设置值
$document_uri与$uri相同; 比如 /test2/test.php
$host请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$hostname机器名使用 gethostname系统调用的值
$http_cookiecookie 信息
$http_referer引用地址
$http_user_agent客户端代理信息
$http_via最后一个访问服务器的Ip地址。
$http_x_forwarded_for相当于网络访问路径
$is_args如果请求行带有参数,返回“?”,否则返回空字符串
$limit_rate对连接速率的限制
$nginx_version当前运行的nginx版本号
$pidworker进程的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,带参数; 比如http://localhost:88/test1/
$scheme所用的协议,比如http或者是https
$server_addr服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)
$server_name请求到达的服务器名
$server_port请求到达的服务器端口号
$server_protocol请求的协议版本,“HTTP/1.0"或"HTTP/1.1”
$uri请求的URI,可能和最初的值有不同,比如经过重定向之类的

ngx.status = ngx.HTTP_CONTINUE (100) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_SWITCHING_PROTOCOLS (101) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_OK (200)
ngx.status = ngx.HTTP_CREATED (201)
ngx.status = ngx.HTTP_ACCEPTED (202) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_NO_CONTENT (204) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_PARTIAL_CONTENT (206) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_SPECIAL_RESPONSE (300)
ngx.status = ngx.HTTP_MOVED_PERMANENTLY (301)
ngx.status = ngx.HTTP_MOVED_TEMPORARILY (302)
ngx.status = ngx.HTTP_SEE_OTHER (303)
ngx.status = ngx.HTTP_NOT_MODIFIED (304)
ngx.status = ngx.HTTP_TEMPORARY_REDIRECT (307) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_BAD_REQUEST (400)
ngx.status = ngx.HTTP_UNAUTHORIZED (401)
ngx.status = ngx.HTTP_PAYMENT_REQUIRED (402) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_FORBIDDEN (403)
ngx.status = ngx.HTTP_NOT_FOUND (404)
ngx.status = ngx.HTTP_NOT_ALLOWED (405)
ngx.status = ngx.HTTP_NOT_ACCEPTABLE (406) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_REQUEST_TIMEOUT (408) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_CONFLICT (409) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_GONE (410)
ngx.status = ngx.HTTP_UPGRADE_REQUIRED (426) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_TOO_MANY_REQUESTS (429) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_CLOSE (444) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_ILLEGAL (451) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR (500)
ngx.status = ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)
ngx.status = ngx.HTTP_BAD_GATEWAY (502) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE (503)
ngx.status = ngx.HTTP_GATEWAY_TIMEOUT (504) (first added in the v0.3.1rc38 release)
ngx.status = ngx.HTTP_VERSION_NOT_SUPPORTED (505) (first added in the v0.9.20 release)
ngx.status = ngx.HTTP_INSUFFICIENT_STORAGE (507) (first added in the v0.9.20 release)
 

很多是吧,其实这还不是全部,Nginx一直在不停迭代更新是一个原因,还有一个原因是有些变量太冷门。使用它们,我们将会有很多玩法。

首先,我们在OpenResty中如何引用这些变量呢?参考ngx.var.VARIABLE小节。

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

    server {
        listen       8866;
        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:8866/sum?a=11&b=12'
sum:23

也许你笑了,这个API太简单了,貌似实际意义不大。我们做个最简易的防火墙,貌似有就那么点意思了不是?

代码如下:

    server {
        listen       8866;
        server_name  localhost;

        location /sum {
            # 使用access阶段完成准入阶段处理
            access_by_lua '
                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 )
            }
        }
    }

测试 shell :

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

403 Forbidden

403 Forbidden
openresty/1.9.3.1

通过测试结果看到,我们提取了终端的IP地址后进行限制,我们这个简单防火墙就诞生了。稍微扩充一下,就可以做到支持范围,如果再可以与系统iptables进行配合,那么达到软防火墙的目的就没有任何问题。

目前为止,我们所有的例子都是对Nginx内置变量的获取,我们是否可以对其进行设置呢?其实大多数内容都是不允许写入的,例如刚刚的终端IP地址,在应用中我们是不允许对其进行更新的。对于可写的变量中的limit_rate,是值得一提的,他能完成传输速率限制。进一步说对于静态文件传输、日志传输的情况,我们可以用它来完成限速的效果,它的影响是单个请求。

例如下面的例子:

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

我们来下载这个文件:

➜  ~  wget '127.0.0.1:8866/download/1.cab'
--2015-09-13 13:59:51--  http://127.0.0.1:8866/download/1.cab
Connecting to 127.0.0.1:8866... 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

73287367&t=1552978370081?mid=&wid=51824&sid=&tid=7161&rid=FINISHED&custom1=mp.csdn.net&t=1552978370081

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值