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 scroll the nginx log in docker

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to scroll the nginx log in docker". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Nginx does not handle the scrolling problem of the log itself, it kicks the ball to the user. In general, you can use the logrotate tool to accomplish this task, or if you like, you can write a variety of scripts to accomplish the same task.

The Nginx official actually gives instructions on how to scroll the log:

Rotating Log-files

In order to rotate log files, they need to be renamed first. After that USR1 signal should be sent to the master process. The master process will then re-open all currently open log files and assign them an unprivileged user under which the worker processes are running, as an owner. After successful re-opening, the master process closes all open files and sends the message to worker process to ask them to re-open files. Worker processes also open new files and close old files right away. As a result, old files are almost immediately available for post processing, such as compression.

The main idea of this paragraph is:

Rename the old log file first

Then send the USR1 signal to the nginx master process

After receiving the signal, the nginx master process will do some processing, and then ask the worker process to reopen the log file.

The worker process opens the new log file and closes the old one

In fact, the only work we really need to do is the first two points!

Assuming that docker is already installed on your system, let's run a nginx container directly:

$docker run-d\-p 80:80\-v $(pwd) / logs/nginx:/var/log/nginx\-- restart=always\-- name=mynginx\ nginx:1.11.3

Notice that we mounted the log bindings of nginx to the logs directory under the current directory.

Save the following to the test.sh file:

#! / bin/bashfor ((iSuppli _ I / dev/null sleep 1done

Then run the script to simulate the generation of continuous logging.

Create a rotatelog.sh file with the following contents:

#! / bin/bashgetdatestring () {TZ='Asia/Chongqing' date "+% Y%m%d%H%M"} datestring=$ (getdatestring) mv / var/log/nginx/access.log / var/log/nginx/access.$ {datestring} .logmv / var/log/nginx/error.log / var/log/nginx/error.$ {datestring} .logkill-USR1 `cat / var/run/ nginx.pid`

The getdatestring function takes the current time and formats it as a string, such as "201807241310". I prefer to name files with date and time. Note that the time zone is specified through TZ='Asia/Chongqing', because the UTC time is formatted by default, which is weird to use (real-time brain supplement + 8 hours). The following two mv commands are used to rename log files. Finally, the USR1 signal is sent to the nginx master process by kill command.

Add executable permissions to the rotatelog.sh file and copy it to the $(pwd) / logs/nginx directory with the following command:

$chmod + x rotatelog.sh$ sudo cp rotatelog.sh$ (pwd) / logs/nginx

Our nginx runs in the container, so we need to send USR1 signals to the nginx master process in the container. So we need to execute the rotatelog.sh script in the mynginx container through the docker exec command:

$docker exec mynginx bash / var/log/nginx/rotatelog.sh

Execute the above command once, and a new batch of log files will be generated as scheduled:

Let's configure this command in a scheduled task to be executed at 1 o'clock every morning. Execute the crontab-e command and add the following line at the end of the file:

* 1 * docker exec mynginx bash / var/log/nginx/rotatelog.sh

Just save and exit. The following figure shows the effect of scrolling every 5 minutes during the test:

This is possible in theory, because the contents of the data volume mounted by binding are the same from the host as from the container. But when you do this, you are likely to have access issues. In the host, you generally use ordinary users, while the owners of log files generated in the container are special users and generally do not give other users permission to write and execute:

Of course, there will be no problem if you are using root users in your host.

In fact, the full name of this question should be: can you send a signal to the nginx master process in the docker container from the host?

The answer is yes.

We can do this by order:

$docker container kill mynginx-s USR1

Send a USR1 signal to process 1 (nginx master) in the container (this method can only send a signal to process 1):

Combining the above two questions, we can write another way to scroll the nginx log in docker. In this way, you do not need to execute the command in the container through the docker exec command, but complete all the operations in the host:

Rename the log file in the container data volume first

Send a USR1 signal to process 1 in the container

This is the end of "how to scroll the nginx log in docker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report