大网数据广告活动
服务器运维问答

Nginx: Unknown log format error

· 52CE 我爱测

I have a proxy set up that is taking UDP traffic to load balance internal DNS. It is routing the traffic properly to my backend DNS servers, but I cannot seem to get the log formatting to work correctly. The error I'm receiving i…

问题原文

原帖链接 · DevOpsSauce · CC BY-SA 4.0

I have a proxy set up that is taking UDP traffic to load balance internal DNS. It is routing the traffic properly to my backend DNS servers, but I cannot seem to get the log formatting to work correctly.

The error I'm receiving is:

nginx[32103]: nginx: [emerg] unknown log format "dns" in /etc/nginx/nginx.conf:23

I have the log format in the http block, and define it in the stream-->server block. I tried to set the log_format within the stream-->server block, but none of the variables would work.

    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;
}

I tried to place log_format in the stream block, but I start getting 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;

  }
}

This will not be a web server at all. It will only serve as a proxy for DNS traffic. How do I properly get the logging to function correctly? This is a default nginx.conf file except for the stream block.

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

A log_format for use by stream servers must be declared in the stream block, rather than the http block. These directives are independent of each other. See the documentation for the stream log_format.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-11-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。