美文网首页
nginx的变量

nginx的变量

作者: OOM_Killer | 来源:发表于2019-05-22 23:37 被阅读0次

    Nginx 的系统变量是由每一个编译进Nginx的模块提供的。而用户也可以自己定义变量。

    变量的特性
    • 惰性求值:当使用到这个变量时才会求其值。所以不会有性能上的问题。而惰性求值也带来了第二个特性。变量值动态改变。
    • 变量值动态改变:一个变量的内容是动态变化的,其值为使用的那一时刻的值。
    存放变量

    对于频繁使用的变量,其值被存放在哈希表中

    • variables_hash_bucket_size size;
      对于变量名特别长的,需要增大(默认64 byte)
    • variables_hash_max_size size;
      对于变量特别多的,需要最大(默认1024 byte)
    变量种类

    如下有一个nginx配置

    log_format var_log '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status bytes_sent=$bytes_sent body_bytes_sent=$body_bytes_sent "$http_referer"'
                    '"$http_user_agent" "$sent_http_abc"';
    
    server {
            listen 80;
            server_name var.loki.wjx localhost;
            access_log logs/var.log var_log;
    
            location / {
                    set $limit_rate 10k;
                    return 200 '
    arg_a: $arg_a, arg_b: $arg_b, args: $args
    connection: $connection, connection_requests: $connection_requests
    cookie_a: $cookie_a
    uri: $uri, document_uri: $document_uri, request_uri: $request_uri
    request: $request , request_id: $request_id
    server: $server_addr, $server_name, http_host: $http_host,
    limit_rate: $limit_rate, hostname: $hostname,
    content_length: $content_length
    status: $status, body_bytes_sent: $body_bytes_sent, bytes_sent: $bytes_sent
    time: $request_time, $msec, $time_iso8601, $time_local\n';
            }
    }
    
    

    使用curl访问后可以看到

    $ curl -H 'Host:var.loki.wjx' -H'Content-Length: 10' -H'Cookie: a=c1' 'http://127.0.0.1?a=1&b=2'
    
    arg_a: 1, arg_b: 2, args: a=1&b=2
    connection: 8, connection_requests: 1
    cookie_a: c1
    uri: /, document_uri: /, request_uri: /?a=1&b=2
    request: GET /?a=1&b=2 HTTP/1.1 , request_id: a08f79326436532975b68563ff5129b7
    server: 127.0.0.1, var.loki.wjx, http_host: var.loki.wjx,
    limit_rate: 10240, hostname: loki,
    content_length: 10
    status: 200, body_bytes_sent: 0, bytes_sent: 0
    time: 0.000, 1558537313.937, 2019-05-22T11:01:53-04:00, 22/May/2019:11:01:53 -0400
    
    
    

    这里的 content_length body_bytes_sent bytes_sent 都是0,因为这个时候,这些变量的值都还是0。

    常见的nginx 变量

    变量 作用
    arg_参数名 URL 中某个具体参数的值
    query_string 与args变量完全相同
    args 全部URL参数
    is_args 如果请求的URL中有参数则返回?否则返回空
    content_length HTTP 请求中标识包体长度的Content-Length头部的值,头部没有这个则为空
    content_type 标识请求包体类型的Content-Type 头部的值
    uri 请求的URI(不包含,不包含?后的参数)
    document_uri 与uri完全相同
    request_uri 请求的URL(包含?后的参数)
    scheme 协议名 HTTP 或者 HTTPS
    request_method 请求的方法GET 或者 POST
    request_length 所有请求内容的大小,包括请求行,头部,包体等
    remote_user 有HTTP Basic Authentication 协议传入的用户名
    request 原始的url请求,含有方法和协议版本

    TCP相关的变量

    变量 作用
    binary_remote_addr 客户端地质的整型格式,对于IPv4是4字节
    remote_addr 客户端地址
    remote_port 客户端端口
    connection 递增的连接序号
    connection_requests 当前连接上执行过的请求数,对keepalive 有意义
    proxy_protocol_addr 若使用了proxy_protocol 协议则返回协议中的地址
    proxy_protocol_port 若使用了proxy_protocol 协议则返回协议中的端口
    server_addr 服务器端地址(本端地址)
    server_port 服务器端端口
    TCP_INFO tcp内核层参数($tcpinfo_rtt,$tcpinfo_rttvar,$tcpinfo_snd_cwnd,$tcpinfo_rcv_space)
    server_protocol 服务端协议,例如 HTTP

    Nginx 处理请求过程中产生的变量

    变量 作用
    request_time 请求处理到现在的耗时
    server_name 匹配上的请求server_name
    request_completion 若请求处理完则返回OK,否则为空
    request_id 以16禁止输出的请求标识id,随即生成

    Nginx系统变量

    变量 作用
    time_local 以本地时间的标准输出
    pid 所属worker进程的id
    hostname 与系统上输出hostname 一致

    相关文章

      网友评论

          本文标题:nginx的变量

          本文链接:https://www.haomeiwen.com/subject/htbpzqtx.html