Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to adjust the log level of nginx in Docker

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to adjust the log level of nginx in Docker". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to adjust the log level of nginx in Docker.

Catalogue

Intro

Nginx Dockerfile

New conf

More

References

Intro

Recently, we found that one of our applications has generated a lot of logs, and most of these logs are access_log of nginx. By default, we collect the standard output to es to analyze the application logs, but many of them are access_log may cover up the real error logs, so sometimes we may not want to output these access_log. Examples are as follows:

Nginx Dockerfile

Go to Github and pick 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 when we docker logs or kubectl logs

We can view the default nginx configuration by executing cat / etc/nginx/nginx.conf in the container

Default nginx conf

It can be seen from the above that the error_log level is notice and can also be configured as warn/error if necessary. The specific log configuration can be described in 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 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 convenient to know how to modify the configuration. You only need to replace the default configuration with our newly modified 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 overwrite the default configuration in Dockerfile with the new configuration:

# Copy custom nginx configCOPY / conf/nginx.conf / etc/nginx/nginx.confMore

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

If you don't want to record, if you don't care about access_log, you can directly use off configuration to disable access_log. If you want to record selectively, for example, if 2xx/3xx does not record, you can also record other situations. It is also described in the nginx documentation, and you can choose according to your needs.

Map $status $loggable {~ ^ [23] 0; default 1;} access_log / path/to/access.log combined if=$loggable; here, I believe you have a deeper understanding of "how to adjust the log level of nginx in Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report