Setup Global log_format

47 views
Skip to first unread message

Maksym

unread,
Nov 18, 2022, 3:59:44 AM11/18/22
to openresty-en
Hi, I ran into a problem when migrating from nginx to openresty version 1.21.4.1 while setting the global log_format.

How can I define log_format in the nginx.conf file so that I can later refer to it in other site configurations?

Now, when I try to do this (I mean that I defined log_format json_analytics in nginx.conf and try to use it in default.conf), I get an nginx error: [emerg] unknown log format "json_analytics" in /etc/openresty/nginx/sites/default .conf:5

Although it works on nginx.

Help me please.

Junlong li

unread,
Nov 18, 2022, 10:54:58 PM11/18/22
to openresty-en
did you include the   /etc/openresty/nginx/sites/default .conf before the definition of the " json_analytics"

Maksym

unread,
Nov 21, 2022, 8:10:28 AM11/21/22
to openresty-en
Yes, below is an example of my configuration, as you can see, the  include   goes before the  definition of the " json_analytics"

user www-data;
worker_processes auto;
pid /run/openresty.pid;
worker_rlimit_nofile 64000;

events {
    worker_connections 130000;
    multi_accept on;
    use epoll;
}

http {
    geoip2 /tmp/GeoLite2-Country.mmdb {
        $geoip2_data_country_iso_code country iso_code;
        $geoip2_data_continent_code   continent code;
    }

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;

    include /etc/openresty/nginx/sites/*.conf;

    client_max_body_size 8M;
    server_names_hash_max_size 512;
    server_names_hash_bucket_size 64;
    proxy_headers_hash_max_size 512;
    proxy_headers_hash_bucket_size 128;

    include mime.types;
    default_type application/octet-stream;

    gzip off;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 4;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    resolver 8.8.8.8;

    ssl_session_timeout  5m;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_session_cache shared:SSL:50m;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    ssl_prefer_server_ciphers on;

    # log_by_lua main '[$time_local] "$host" "$remote_addr" "$status" "$request_time" "$upstream_response_time" "$bytes_sent" "$request" "$http_referer"';

    log_format json_analytics escape=json '{'
        '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
        '"connection": "$connection", ' # connection serial number
        '"connection_requests": "$connection_requests", ' # number of requests made in connection
        '"pid": "$pid", ' # process pid
        '"request_id": "$request_id", ' # the unique request id
        '"request_length": "$request_length", ' # request length (including headers and body)
        '"remote_addr": "$remote_addr", ' # client IP
        '"remote_user": "$remote_user", ' # client HTTP username
        '"remote_port": "$remote_port", ' # client port
        '"time_local": "$time_local", '
        '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
        '"request": "$request", ' # full path no arguments if the request
        '"request_uri": "$request_uri", ' # full path and arguments if the request
        '"args": "$args", ' # args
        '"status": "$status", ' # response status code
        '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
        '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
        '"http_referer": "$http_referer", ' # HTTP referer
        '"http_user_agent": "$http_user_agent", ' # user agent
        '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
        '"http_x_forwarded_proto": "$http_x_forwarded_proto", '
        '"http_x_current_version": "$http_x_current_version", '
        '"http_host": "$http_host", ' # the request Host: header
        '"server_name": "$server_name", ' # the name of the vhost serving the request
        '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
        '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
        '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
        '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
        '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
        '"upstream_response_length": "$upstream_response_length", ' # upstream response length
        '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
        '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
        '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
        '"scheme": "$scheme", ' # http or https
        '"request_method": "$request_method", ' # request method
        '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
        '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
        '"gzip_ratio": "$gzip_ratio", '
        '"http_cf_ray": "$http_cf_ray",'
        '"geoip_country_code": "$geoip2_data_country_iso_code", '
        '"nginx_time": "$time_iso8601", '          # local time in the ISO 8601 standard format
        '"proxy_host": "$proxy_host" '
    '}';


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format log_resp_body  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '

    access_log /var/log/openresty/json_access.log json_analytics;
    access_log /var/log/openresty/access.log main;
    error_log /var/log/openresty/error.log;


}


суббота, 19 ноября 2022 г. в 05:54:58 UTC+2, zhuizhu...@gmail.com:
Reply all
Reply to author
Forward
0 new messages