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

Mysql delete data and data table method example

It is very easy to delete data and tables in MySQ...

Detailed explanation of Vue's sync modifier

Table of contents 1. Instructions 2. Modifiers 3....

How to clean up the disk space occupied by Docker

Docker takes up a lot of space. Whenever we run c...

Detailed graphic explanation of sqlmap injection

Table of contents 1. We found that this website m...

MySQL deduplication methods

MySQL deduplication methods 【Beginner】There are v...

Seven Principles of a Skilled Designer (1): Font Design

Well, you may be a design guru, or maybe that'...

How to configure nginx to limit the access frequency of the same IP

1. Add the following code to http{} in nginx.conf...

MySQL string splitting example (string extraction without separator)

String extraction without delimiters Question Req...

Ubuntu 16.04 image complete installation tutorial under VMware

This article shares with you the installation tut...

Summary of standard usage of html, css and js comments

Adding necessary comments is a good habit that a ...

Vue Element-ui form validation rule implementation

Table of contents 1. Introduction 2. Entry mode o...

Centos6.5 glibc upgrade process introduction

Table of contents Scenario Requirements glibc ver...

mysql5.6.zip format compressed version installation graphic tutorial

Preface: MySQL is a relational database managemen...

Grid systems in web design

Formation of the grid system In 1692, the newly c...