openresty 内置变量,



openresty 内置变量

          

官网:http://nginx.org/en/docs/varindex.html

        

             

                                 

常用内置变量

          

openresty 获取nginx的内置变量

nginx 内置变量:$arg_name
openresty lua_ngx获取方式:ngx.var.arg_name

             

请求、响应变量

$request:记录请求的URL、http
$request_uri:请求的url,带参数
$request_time:请求到达nginx,一直到返回响应花费的时间
$request_method:请求方法,如:get、post等
$request_length:请求的长度,包括请求头、请求体
$request_filename:请求的文件路径,基于root alias指令、url生成

$uri:当前请求的uri
$document_uri:uri别名

$arg_name:url中的名为name的参数
$args:获取所有参数

$cookie_name:cookie名为name的值
$sent_http_name:任意响应头

$binary_remote_addr:客户端地址(二进制形式)
$body_bytes_sent:发送给客户端的字节数,不包含响应头
$bytes_sent:发送给客户端的总字节数(响应头、响应体)

$scheme:协议,如:http、https等
$hostname:主机名(nginx所在服务器的主机名)
$status:http请求状态

$http_referer:请求是从哪个页面链接过来的
$http_user_agent:客户端浏览器信息

         

ip、port等变量

# 客户端信息
$remote_addr:客户端ip
$remote_port:客户端端口
$remote_user:客户端用户名

$realip_remote_addr:保留原来的客户端地址

# 服务端信息
$server_name:服务器名称(通常是域名)
$server_addr:服务器地址
$server_port:服务器端口号
$server_protocol:请求协议、版本号,如:http1.0、http1.1

# 反向代理信息
$upstream_addr:请求反向代理后,传到后端的ip
$upstream_port:请求反向代理后,传到后端的端口
$upstream_status:反向代理请求在nginx的http状态
$upstream_response_time:反向代理请求在nginx消耗的时间

              

其他变量

$pid:woker进程号
$nginx_version:nginx版本号
$connection_requests:连接的请求量

$geoip_city:城市名称
$time_local:通用日志格式下的本地时间
$msec:日志写入时间,单位为秒,精度为毫秒

$pipe:如果请求是http流水线发送的,值为"p",否则为"."

         

               

                                 

使用示例

          

nginx.conf

events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    server {
        location /lua {         
            default_type "applictaion/json";
            content_by_lua "ngx.say('hello gtlx')";
        }
 
        location /hello {         
            content_by_lua_block {
                ngx.say(ngx.var.uri);     # 输出uri
                ngx.say(ngx.var.args);    # 输出路径参数
                ngx.say(ngx.var.scheme);  # 输出scheme
            }
        }
    }
 
    client_body_temp_path /var/run/openresty/nginx-client-body;
    proxy_temp_path       /var/run/openresty/nginx-proxy;
    fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;
    uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;
    scgi_temp_path        /var/run/openresty/nginx-scgi;
 
    sendfile        on;
    keepalive_timeout  65;
 
}

       

创建容器

docker run -it -d -p 8082:80 \
-v /Users/huli/lua/openresty/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
--name openresty3 lihu12344/openresty

        

使用测试

huli@hudeMacBook-Pro openresty % curl localhost:8082/lua
hello gtlx

huli@hudeMacBook-Pro openresty % curl localhost:8082/hello
/hello
nil
http

          

                  

相关内容