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 realize Hot deployment and Log cutting in Nginx

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

It is believed that many inexperienced people have no idea about how to achieve hot deployment and log cutting in Nginx. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Nginx Command Line

Format: nginx-s stop

Help: -?-h

Use the specified profile:-c

Specify the configuration instruction:-g (the purpose is to override the instructions in the configuration file)

Specify the running directory:-p

Send signal:-s (stop service immediately: stop, graceful stop service: quit, reconfigure file: reload, restart logging file: reopen)

Test the configuration file for syntax errors:-t-T

Print nginx version information, compilation information, etc.:-v-V

Nginx commands are very similar to most Linux commands, which are nginx plus basic instructions, plus instruction-related parameters. By default, nginx looks for the configuration file that was specified when you executed the configure command, but you can specify the configuration file with-c, and you can specify the configuration instruction with-g.

The way for nginx to operate a running process is usually by sending a signal, either through the general kill command of linux, or by using the-s command of nginx.

Next, let's familiarize ourselves with the command-line operation of Nginx through a few chestnuts.

Reload configuration fil

By default, the configuration file is under the conf file in the installation directory, and the file name is nginx.conf. We can open it and have a look at it:

Worker_processes 1 leading events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; sendfile on; # tcp_nopush on; keepalive_timeout 65; # gzip on; server {listen 80; server_name localhost; location / {root html; index index.html index.htm } error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html;}

If we need to enable gzip compression, we can remove the comments in front of it, and when we have finished modifying the nginx configuration file, we can restart the nginx service through the command of nginx. / nginx-s reload.

Hot deployment of Nginx

When replacing the old version with the new version of nginx, if the deployment is not hot, you will need to cancel the nginx service and restart the service to successfully replace it, which will disconnect the users who are visiting. Therefore, in order to upgrade the version without affecting the user's experience, hot deployment is required to upgrade the version.

Next, let's make a hot deployment together.

Because the main purpose of upgrading is to replace binaries, back up old binaries before upgrading.

# back up the old version of nginx binaries mv / usr/local/nginx/sbin/nginx / usr/local/nginx/sbin/nginx.old

Then download the latest version of nginx, extract it, compile it, and copy the compiled nginx binaries to the sbin directory under the installation directory.

# download the latest version of nginxwget http://nginx.org/download/nginx-1.17.2.tar.gz# decompress tar-xzvf nginx-1.17.2.tar.gzcd nginx-1.17.2./configure-- prefix=/usr/local/nginx# compile make# to replace the old nginx executor cp-r / usr/local/nginx-1.16.1/objs/nginx / usr/local/nginx/sbin/-f from the official website

Use ps-ef | grep nginx to view the nginx health:

[root@wupx sbin] # ps-ef | grep nginx root 1752 1 0 20:39? 00:00:00 nginx: master process. / sbin/nginxnobody 1783 1752 0 20:41? 00:00:00 nginx: worker processroot 1787 1 0 20:41? 00:00:00 wget http://nginx.org/download/nginx-1.17.2.tar.gzroot 4357 1708 21:00 pts/2 00:00:00 grep-- color=auto nginx

You can see that the PID of the currently launched nginx is 1752, so you need to signal the master process of the running nginx to tell it that we are going to hot deploy.

# send a USR2 signal to the old version of the main process number to make the old version of nginx stop receiving requests and replace kill-USR2 1752 with the new version of nginx

Then check the health status of nginx through ps-ef | grep nginx:

[root@wupx sbin] # ps-ef | grep nginx root 1752 1 0 20:39? 00:00:00 nginx: master process. / sbin/nginxnobody 1783 1752 0 20:41? 00:00:00 nginx: worker processroot 1787 1 0 20:41? 00:00:00 wget http://nginx.org/download/nginx-1.17.2.tar.gzroot 4391 1752 21:02? 00 00:00 nginx: master process. / sbin/nginxnobody 4392 4391 0 21:02? 00:00:00 nginx: worker processroot 4394 1708 0 21:07 pts/2 00:00:00 grep-- color=auto nginx

At this point we need to send a signal to the old nginx, telling the old nginx to gracefully shut down all worker processes.

# send a WINCH signal to the old main process, which will notify the old worker process to shut down gracefully, and then exit kill-WINCH 1752

Re-viewing the nginx status:

[root@wupx sbin] # ps-ef | grep nginxroot 1752 10 20:39? 00:00:00 nginx: master process. / sbin/nginxroot 1787 10 20:41? 00:00:00 wget http://nginx.org/download/nginx-1.17.2.tar.gzroot 4391 1752 0 21:02? 00:00:00 nginx: master process. / sbin/nginxnobody 4392 4391 0 21:02? 00:00:00 nginx: worker processroot 4402 1708 0 21:08 pts/2 00:00:00 grep-color=auto nginx

We can also find that the old nginx maser process still exists, and its meaning is: if there is a problem and we need to return to the old version, we can send it a reload command and ask him to pull up the worker process again and turn off the new version. Keep it here to make it easier for us to do version fallback.

If you want to exit the reserved master process, you can do so with the kill-QUIT command:

# send a QUIT signal to the old main process, and it will exit the reserved master process kill-QUIT 1752

After execution, the 1752 process exits, and you can see through the netstat lntup that port 80 has been listened to by the 4391 process (the process of the new version of nginx).

At this point, we have completed the hot deployment of nginx.

Log cutting

In order to prevent the log file from being too large and inconvenient to view, it is necessary to cut the log. First, back up the original log:

# back up the original log mv error.log old_error.log

View the log size:

[root@wupx logs] # lltotal 20 lltotal RW old_error.log-1 root root 6789 Nov 6 22:28 access.log-rw-r--r-- 1 root root 5 Nov 6 22:16 nginx.pid-rw-r--r-- 1 root root 7831 Nov 6 22:28 old_error.log

Next, do log cutting:

# Log cutting / usr/local/nginx/sbin/nginx-s reopen

Check again:

[root@wupx logs] # lltotal 24 nginx.pid-rw-r--r-- nginx.pid-rw-r--r---1 nobody root 6789 Nov 6 22:28 access.log-rw-r--r-- 1 nobody root 60 Nov 6 22:30 error.log-rw-r--r-- 1 root root 5 Nov 6 22:16 nginx.pid-rw-r--r-- 1 root root 7831 Nov 6 22:28 old_error.log

After the above operation, we have completed the log cutting, the above operation is only to understand the operation flow of log cutting, it is not recommended to use it in direct production. It is recommended that you first write a shell script and execute it regularly through the shell script.

Sample script:

#! / bin/bashLOGS_PATH=/usr/local/nginx/logs/historyCUR_LOGS_PATH=/usr/local/nginx/logsYESTERDAY=$ (date-d "yesterday" +% Y-%m-%d) mv ${CUR_LOGS_PATH} / access.log ${LOGS_PATH} / old_access_$ {YESTERDAY} .logmv ${CUR_LOGS_PATH} / error.log ${LOGS_PATH} / old_error_$ {YESTERDAY} .log # # sends USR1 signals to the NGINX main process. The USR1 signal is a summary of reopening the log file kill-USR1 $(cat / usr/local/nginx/logs/nginx.pid)

This article mainly introduces the relevant knowledge of Nginx command line, and introduces the operations such as overloaded configuration file, Nginx hot deployment, log slicing and so on.

After reading the above, have you mastered how to achieve hot deployment and log slicing in Nginx? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report