How to adjust the log level of nginx in Docker

How to adjust the log level of nginx in Docker

Intro

Recently, we found that one of our applications generates a lot of logs, and most of these logs are nginx's access_log. By default, we collect the standard output into es to analyze the application logs, but many of them are access_log, which may cover up the real error logs. So sometimes we may not want to output these access_logs. The example is as follows:

Nginx Dockerfile

I went to Github to look up Nginx's Dockerfile, docker-nginx/Dockerfile at master · nginxinc/docker-nginx (github.com)

You can see that nginx links access_log and error_log to standard output by default, which is why we can see access_log in docker logs or kubectl logs

We can execute cat /etc/nginx/nginx.conf in the container to view the default nginx configuration

default nginx conf

From the above, we can see that the error_log level is notice. If necessary, it can also be configured to warn/error. For specific log configuration, please refer to the official documentation. The configurable log levels are: debug, info, notice, warn, error, crit, alert, emerg

access_log can be disabled directly using off or using another path, so that it will not be output directly to standard output and there will not be so many logs.

New conf

It is more convenient to know how to modify the configuration. We only need to replace the default configuration with our new configuration. The new configuration is as follows:

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

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

    access_log off;

    sendfile on;
    #tcp_nopush on;

    keepalive_timeout 65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;
}

Then in the Dockerfile, overwrite the default configuration with the new one:

# Copy custom nginx config
COPY /conf/nginx.conf /etc/nginx/nginx.conf

More

If you want to record access_log but don't want to do it directly, you can configure access_log to another file name, and it will be written to the corresponding configured file, but it will not be output directly to standard output, so that many of the collected logs will not be access_log.

If you don't want to log, you can directly use the off configuration to disable access_log. If you want to selectively log, such as not logging 2xx/3xx, you can also log other situations. The nginx documentation also introduces it. You can choose according to your needs.

map $status $loggable {
    ~^[23] 0;
    default 1;
}
access_log /path/to/access.log combined if=$loggable;

References

  • https://notestack.io/public/configure-nginx-logging-in-a-docker-container/874f1253-cf1a-4c62-9d2d-467ab23c258d
  • https://docs.nginx.com/nginx/admin-guide/monitoring/logging/
  • https://github.com/nginxinc/docker-nginx/blob/master/stable/alpine/Dockerfile

This is the end of this article on how to adjust the log level of nginx in Docker. For more information about the log level of nginx in Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the idea of ​​rolling nginx logs in docker

<<:  SQL insert into statement writing method explanation

>>:  Font Treasure House 50 exquisite free English font resources Part 2

Recommend

SQL GROUP BY detailed explanation and simple example

The GROUP BY statement is used in conjunction wit...

A quick guide to MySQL indexes

The establishment of MySQL index is very importan...

Implementing a simple timer in JavaScript

This article example shares the specific code of ...

Differences between proxy_pass in two modules in nginx

1. The proxy_pass directive of the 1.ngx_stream_p...

Gojs implements ant line animation effect

Table of contents 1. Gojs Implementation 1. Drawi...

How to deploy nodejs service using Dockerfile

Initialize Dockerfile Assuming our project is nam...

JavaScript imitates Jingdong magnifying glass special effects

This article shares the specific code of JavaScri...

How to write a picture as a background and a link (background picture plus link)

The picture is used as the background and the lin...

Basic learning tutorial of table tag in HTML

Table label composition The table in HTML is comp...

Implementing a simple student information management system based on VUE

Table of contents 1. Main functions 2. Implementa...

Analysis of the Linux input subsystem framework principle

Input subsystem framework The linux input subsyst...

Use nginx + secondary domain name + https support

Step 1: Add a secondary domain name to the Alibab...

React implementation example using Amap (react-amap)

The PC version of React was refactored to use Ama...

The process of deploying a project to another host using Jenkins

environment Hostname ip address Serve Jenkins 192...

Detailed explanation of template tag usage (including summary of usage in Vue)

Table of contents 1. Template tag in HTML5 2. Pro...