我有一个代理设置,它使用 UDP 流量来负载平衡内部 DNS。它正在将流量正确路由到我的后端 DNS 服务器,但我似乎无法让日志格式正常工作。
我收到的错误是:
nginx[32103]: nginx: [emerg] unknown log format "dns" in /etc/nginx/nginx.conf:23
我在 http 块中有日志格式,并在stream-->server
块中定义它。我试图在stream-->server
块中设置 log_format,但没有一个变量可以工作。
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
stream {
upstream dns_servers {
least_conn;
server 192.168.3.222:53 fail_timeout=10s;
server 192.168.70.80:53 fail_timeout=10s;
server 192.168.70.81:53 fail_timeout=10s;
}
server {
listen 53 udp;
proxy_pass dns_servers;
proxy_responses 1;
proxy_timeout 1s;
access_log /var/log/nginx/access.log dns;
}
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
log_format dns '[$time_local] | $remote_addr | $remote_user | $server_name $host to: $upstream_addr | '
'"$request" | $status | upstream_response_time $upstream_response_time msec '
'$msec | request_time $request_time';
access_log /var/log/nginx/access.log dns;
error_log /var/log/nginx/error.log;
}
我试图放置log_format
在流块中,但我开始得到unknown <variable name> variable
.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
stream {
log_format dns '[$time_local] | $remote_addr | $remote_user | $server_name $host to: $upstream_addr | '
'"$request" | $status | upstream_response_time $upstream_response_time msec '
'$msec | request_time $request_time';
upstream dns_servers {
least_conn;
server 192.168.3.222:53 fail_timeout=10s;
server 192.168.70.80:53 fail_timeout=10s;
server 192.168.70.81:53 fail_timeout=10s;
}
server {
listen 53 udp;
proxy_pass dns_servers;
proxy_responses 1;
proxy_timeout 1s;
access_log /var/log/nginx/access.log dns;
}
}
这根本不是 Web 服务器。它只会充当 DNS 流量的代理。如何正确使日志记录正常运行?这是一个默认的 nginx.conf 文件,除了流块。
A
log_format
for use bystream
server
s 必须在stream
块中声明,而不是在http
块中。这些指令彼此独立。请参阅stream
log_format
.我想到了。
stream
我找到了正确的变量,并根据本教程将所有内容放在我的块中。这是我的新
stream
块:你可以看到它确实在记录: