NGINX有很多变量,怎么才能看到所有的变量以及它们的值呢?对于Debug,这是非常重要的。
一、解决方案 (一)
通过 headers 发送环境变量。
add_header X-debug-message "A static file was served" always;
add_header X-uri "$request_uri";
然后,在浏览器的响应头可以看到:
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 '=========================================';
二、解决方案 (二)
可以返回一个 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
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: =========================================