MrGildarts
MrGildarts

Reputation: 833

Get current time in format ISO-8601 Nginx

Is there any way to get the current time in ISO-8601 format in nginx I want to add the time in the header. Thank you in advance

Upvotes: 1

Views: 6437

Answers (1)

Hemant
Hemant

Reputation: 1166

In nginx you can simply use the variable $time_iso8601 to access current time.

In case your want the date time to format this date(which is in iso8601 format) to any desired format, your can your regex along with nginx http module via map method.

refer this to know more about the nginx http module Note: your can only map within http module.

Here i am sharing a sample nginx.conf code for reference and better understanding

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

    events {
        worker_connections 768;
        # multi_accept on;
    }

    http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings

    map $time_iso8601 $year {
        default             'year';
        '~^(?<yyyy>\d{4})-'     $yyyy;
    }
    map $time_iso8601 $month {
        default             'month';
        '~^\d{4}-(?<mm>\d{2})-'     $mm;
    }
    map $time_iso8601 $day {
        default             'day';
        '~^\d{4}-\d{2}-(?<dd>\d{2})'    $dd;
    }
    map $time_iso8601 $hour {
            default             'hour';
            '~^\d{4}-\d{2}-\d{2}T(?<hh>\d{2})'    $hh;
        }
    map $time_iso8601 $min {
            default             'minute';
            '~^\d{4}-\d{2}-\d{2}T\d{2}:(?<mn>\d{2})'    $mn;
        }

    map $time_iso8601 $formatted_date {
        default                         'date-not-found';
        '~^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})'    $year-$month-$day;
    }


    access_log /var/log/nginx/access.$formatted_date,$hour:$min.log;
    error_log /var/log/nginx/error.log;


    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;}

adding an image of the above code in case it is not formatted properly nginx config snapshot

Here i have added hour and min parameters as well based on my requirement, if your don't need it your can simply drop the map chunks of $hour and $min highlighed below.

map $time_iso8601 $hour {
        default             'hour';
        '~^\d{4}-\d{2}-\d{2}T(?<hh>\d{2})'    $hh;
}
    map $time_iso8601 $min {
        default             'minute';
        '~^\d{4}-\d{2}-\d{2}T\d{2}:(?<mn>\d{2})'    $mn;
    }

and from file path

access_log /var/log/nginx/access.$formatted_date.log;

and it will work with only date as log file name and you can also customize it based on your requirement.

In case you want to use it in a sub-config file like xyz.com, you need to remove http module i.e. http{} from nginx.conf and update it the same way in your xyz.com config file else it will get nested as http(http{}} which might cause issues.

Upvotes: 3

Related Questions