查看所有nginx变量

c2e8gylq  于 2022-12-22  发布在  Nginx
关注(0)|答案(2)|浏览(243)

nginx HttpEchoModule允许我访问单个nginx变量:

location /whathost {
    echo "This host is $http_host";
}

我想看到每个可用的变量及其值,类似于bash中set(w/o args)的输出。
这可能吗?

r9f1avp5

r9f1avp51#

我不认为有一个内置的方式来显示所有的变量。但如果你看看源代码,他们似乎是定义在:src/http/ngx_http_variables.c
查找ngx_string,下面是一些awk来打印它们:awk -F\" '/[^\w]ngx_string[^\w]/ {printf "%s:\t$%s\n", $2, $2}' src/http/ngx_http_variables.c
然后你就可以编写自己的set函数了:

location = /set {
    default_type text/plain;
    return 200 "
# insert awk output here <-
";
}

你可以在这里找到来源:http://hg.nginx.org/nginx
或者你可以看到我在这里找到的变量:http://pastie.org/9992530

brccelvz

brccelvz2#

下面是我用来显示nginx documentation here中的变量列表的代码片段。我不得不删除一些变量,因为它们在我的设置中不可用,因为它们来自不同的模块,我没有默认启用它们。我使用了官方docker hub page中的默认nginx映像。

location /variables {       
        set $variables '<br/><br/> remote_addr: $remote_addr';
        set $variables '$variables <br/> ancient_browser: $ancient_browser';
        set $variables '$variables <br/> arg_: $arg_';
        set $variables '$variables <br/> args: $args';
        set $variables '$variables <br/> body_bytes_sent: $body_bytes_sent';
        set $variables '$variables <br/> connection_time: $connection_time';
        set $variables '$variables <br/> connections_active: $connections_active';
        set $variables '$variables <br/> connections_reading: $connections_reading';
        set $variables '$variables <br/> connections_waiting: $connections_waiting';
        set $variables '$variables <br/> connections_writing: $connections_writing';
        set $variables '$variables <br/> content_length: $content_length';
        set $variables '$variables <br/> content_type: $content_type';
        set $variables '$variables <br/> cookie_: $cookie_';
        set $variables '$variables <br/> date_gmt: $date_gmt';
        set $variables '$variables <br/> date_local: $date_local';
        set $variables '$variables <br/> document_root: $document_root';
        set $variables '$variables <br/> document_uri: $document_uri';
        set $variables '$variables <br/> fastcgi_path_info: $fastcgi_path_info';
        set $variables '$variables <br/> fastcgi_script_name: $fastcgi_script_name';
        set $variables '$variables <br/> gzip_ratio: $gzip_ratio';
        set $variables '$variables <br/> host: $host';
        set $variables '$variables <br/> http2: $http2';
        set $variables '$variables <br/> http_: $http_';
        set $variables '$variables <br/> https: $https';
        set $variables '$variables <br/> invalid_referer: $invalid_referer';
        set $variables '$variables <br/> is_args: $is_args';
        set $variables '$variables <br/> limit_rate: $limit_rate';
        set $variables '$variables <br/> limit_req_status: $limit_req_status';
        set $variables '$variables <br/> modern_browser: $modern_browser';
        set $variables '$variables <br/> msie: $msie';
        set $variables '$variables <br/> proxy_add_x_forwarded_for: $proxy_add_x_forwarded_for';
        set $variables '$variables <br/> proxy_host: $proxy_host';
        set $variables '$variables <br/> proxy_port: $proxy_port';
        set $variables '$variables <br/> query_string: $query_string';
        set $variables '$variables <br/> realpath_root: $realpath_root';
        set $variables '$variables <br/> remote_user: $remote_user';
        set $variables '$variables <br/> request: $request';
        set $variables '$variables <br/> request_body: $request_body';
        set $variables '$variables <br/> request_body_file: $request_body_file';
        set $variables '$variables <br/> request_completion: $request_completion';
        set $variables '$variables <br/> request_filename: $request_filename';
        set $variables '$variables <br/> request_id: $request_id';
        set $variables '$variables <br/> request_method: $request_method';
        set $variables '$variables <br/> request_uri: $request_uri';
        set $variables '$variables <br/> scheme: $scheme';
        set $variables '$variables <br/> secure_link: $secure_link';
        set $variables '$variables <br/> secure_link_expires: $secure_link_expires';
        set $variables '$variables <br/> sent_http_: $sent_http_';
        set $variables '$variables <br/> sent_trailer_: $sent_trailer_';
        set $variables '$variables <br/> server_name: $server_name';
        set $variables '$variables <br/> server_protocol: $server_protocol';
        set $variables '$variables <br/> slice_range: $slice_range';
        set $variables '$variables <br/> ssl_client_escaped_cert: $ssl_client_escaped_cert';
        set $variables '$variables <br/> ssl_client_i_dn_legacy: $ssl_client_i_dn_legacy';
        set $variables '$variables <br/> ssl_client_s_dn_legacy: $ssl_client_s_dn_legacy';
        set $variables '$variables <br/> ssl_early_data: $ssl_early_data';
        set $variables '$variables <br/> tcpinfo_rtt: $tcpinfo_rtt';
        set $variables '$variables <br/> tcpinfo_rttvar: $tcpinfo_rttvar';
        set $variables '$variables <br/> tcpinfo_snd_cwnd: $tcpinfo_snd_cwnd';
        set $variables '$variables <br/> tcpinfo_rcv_space: $tcpinfo_rcv_space';
        set $variables '$variables <br/> uid_got: $uid_got';
        set $variables '$variables <br/> uid_reset: $uid_reset';
        set $variables '$variables <br/> uid_set: $uid_set';
        set $variables '$variables <br/> upstream_cache_status: $upstream_cache_status';
        set $variables '$variables <br/> upstream_cookie_: $upstream_cookie_';
        set $variables '$variables <br/> upstream_header_time: $upstream_header_time';
        set $variables '$variables <br/> upstream_http_: $upstream_http_';
        set $variables '$variables <br/> upstream_response_length: $upstream_response_length';
        set $variables '$variables <br/> upstream_response_time: $upstream_response_time';
        set $variables '$variables <br/> upstream_status: $upstream_status';
        set $variables '$variables <br/> upstream_trailer_: $upstream_trailer_';
        set $variables '$variables <br/> uri: $uri';            

        return 200 'Variables: $variables';
        add_header Content-Type text/html;
    }

这肯定不是一个完整的变量列表,因为不同的模块和不同版本的nginx可能会提供不同的变量。不幸的是,我不知道任何编程的方式,我们可以用来迭代所有可能的变量列表。

相关问题