美文网首页
NGINX 变量打印(输出)

NGINX 变量打印(输出)

作者: 偷油考拉 | 来源:发表于2021-12-08 14:44 被阅读0次

NGINX有很多变量,怎么才能看到所有的变量以及它们的值呢?对于Debug,这是非常重要的。

一、解决方案 (一)

通过 headers 发送环境变量。

add_header X-debug-message "A static file was served" always;
add_header X-uri "$request_uri";

然后,在浏览器的响应头可以看到:


image.png

1. 简单范例:

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
    add_header X-debug-message "A static file was served" always;
    ...
}

location ~ \.php$ {
    add_header X-debug-message "A php file was used" always;
    ...
}

如上,
访问 http://www.example.com/img/my-ducky.png 会触发 former header
访问 http://www.example.com/index.php 会触发 latter header

2. 打印所有常见变量范例:
Debug NGINX variables from the request headers | Azimut7. Drupal web development

以conf文件方式,添加到目录conf.d/。比如:conf.d/debug-headers.conf

内容如下:

add_header x0-NGINX-DEBUG '=========================================';
add_header x1-NGINX-http_user_agent $http_user_agent;
add_header xA-NGINX-http_cookie $http_cookie;
add_header xB-NGINX-request $request;
add_header xC-NGINX-request_body $request_body;
add_header xD-NGINX-request_method $request_method;
add_header xE-NGINX-request_time $request_time;
add_header xF-NGINX-request_uri $request_uri;
add_header xG-NGINX-scheme $scheme;
add_header xH-NGINX-request_server_name $server_name;
add_header xI-NGINX-request_server_port $server_port;
add_header xJ-NGINX-uri $uri;
add_header xK-NGINX-args $args;
add_header xL-NGINX-is_args $is_args;
add_header xM-NGINX-request_filename $request_filename;
add_header xN-NGINX-pid $pid;
add_header xO-NGINX-document_root $document_root;
add_header xP-NGINX-document_uri $document_uri;
add_header xQ-NGINX-host $host;
add_header xR-NGINX-hostname $hostname;
add_header xS-NGINX-proxy_protocol_addr $proxy_protocol_addr;
add_header xT-NGINX-proxy_protocol_port $proxy_protocol_port;
add_header xU-NGINX-query_string $query_string;
add_header xV-NGINX-realpath_root $realpath_root;
add_header xW-NGINX-remote_addr $remote_addr;
add_header xX-NGINX-remote_port $remote_port;
add_header xY-NGINX-remote_user $remote_user;
add_header xZ-NGINX-DEBUG '=========================================';
image.png

二、解决方案 (二)

可以返回一个 http response 字符串

添加conf文件到default.d/目录,比如default.d/root.conf

内容如下:

location /
{
#    return 200 $document_root;
    return 200 "xforwardedfor:$proxy_add_x_forwarded_for--remote_addr:$remote_addr--scheme:$scheme--host:$host";
}

访问站点 / 目录,会发送一个文件回来,打开如下:

xforwardedfor:10.41.99.232--remote_addr:10.41.99.232--scheme:http--host:10.51.99.19

三、解决方案 (三)

通过 log_format 指令配置访问日志,将变量置入其内。
Module ngx_http_log_module (nginx.org)

log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log /spool/logs/nginx-access.log compression buffer=32k;

四、解决方案 (四)

编译的时候配置 echo module,或者安装 OpenResty

然后,可以简单地在配置中添加如下语句:

echo "args: $args"

五、debug log

Debugging NGINX | NGINX Plus

nginx -V 2>&1 | grep -- '--with-debug'

把如下,添加到main context

error_log  /var/log/nginx/error.log debug;

在error.log会返回如下:

Server: nginx/1.20.1
Date: Wed, 08 Dec 2021 06:40:05 GMT
Last-Modified: Fri, 16 May 2014 14:33:46 GMT
Connection: keep-alive
ETag: "537621ca-143d0"
X-debug-message: A static file was served
X-uri: /img/header-background.png
x0-NGINX-DEBUG: =========================================
x1-NGINX-http_user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0
xB-NGINX-request: GET /img/header-background.png HTTP/1.1
xD-NGINX-request_method: GET
xE-NGINX-request_time: 0.000
xF-NGINX-request_uri: /img/header-background.png
xG-NGINX-scheme: http
xH-NGINX-request_server_name: _
xI-NGINX-request_server_port: 80
xJ-NGINX-uri: /img/header-background.png
xM-NGINX-request_filename: /usr/share/nginx/html/img/header-background.png
xN-NGINX-pid: 19767
xO-NGINX-document_root: /usr/share/nginx/html
xP-NGINX-document_uri: /img/header-background.png
xQ-NGINX-host: 10.51.99.19
xR-NGINX-hostname: vm-99-19-centos
xV-NGINX-realpath_root: /usr/share/nginx/html
xW-NGINX-remote_addr: 10.41.99.232
xX-NGINX-remote_port: 60912
xZ-NGINX-DEBUG: =========================================

相关文章

  • NGINX 变量打印(输出)

    NGINX有很多变量,怎么才能看到所有的变量以及它们的值呢?对于Debug,这是非常重要的。 一、解决方案 (一)...

  • python入门基础语法总结

    定义变量,使用变量 input 用户自己输入值print 打印值 可以输出多个变量%d|%s 用于输出变量时...

  • 这应该是最详细的Python入门基础语法总结!

    定义变量,使用变量 input 用户自己输入值 print 打印值 可以输出多个变量 %d|%s 用于输出变量时占...

  • python学习01——基础、数据类型、运算符

    [TOC] 1. print()函数 换行打印: 输出: 2. Python关键字 输出: 3. 定义变量&变量的...

  • PHP基本使用

    打印输出 echo 只能输出简单数据类型 print_r() 打印复杂数据类型 var_dump() 输出变量的详...

  • Shell全面

    echo 打印 变量 readOnly 将变量变成只读变量 unset 删除变量 单引号里的任何东西都会原样输出 ...

  • xcode 调试常用命令 lldb

    bt: 自动输出最后一次堆栈的信息。 list: 显示源代码段 po: 打印变量的值 p: 打印数据变量的值。

  • Python系列教程(四):输入、输出

    一、输出 在程序中输出就是打印信息,python中通过print函数打印变量、常量、表达式、函数的结果,将结果显示...

  • GoLang基础语法

    变量定义 从hello world入手 控制台输出: 变量默认值 控制台输出: 注:s为"",故打印出来没有效果 ...

  • Python:2. Python基础

    输入输出 输出 普通输出 多个输出,用,隔开 打印整数,计算结果 输入 无提示输入 有提示输入 数据类型和变量 整...

网友评论

      本文标题:NGINX 变量打印(输出)

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