Nginx and command line Demo: heavy-duty, hot deployment, log cutting

Nginx command line:

  01 Format: nginx parameter signals

  02 Help: -? -H

  03 using the specified configuration file: -c

  04 specify the configuration instructions: -g

  05 specifies the run directory: -p

  06 sends a signal: -s

    Immediately stop the service: stop

    Gracefully stop the service: quit

    Reload the configuration file: reload

    Restart log file: reopen

  07 test whether the configuration file syntax error: -t -T

  08 print version information of nginx, compiling information: -v -V

Reload the configuration file:

[root@Alen ~]# cd /opt/nginx
[root@Alen nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@Alen nginx]# ./sbin/nginx 
[root@Alen nginx]# ps -ef | grep nginx
root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
nobody     2889   2888  0 19:47 ?        00:00:00 nginx: worker process
root       2891   2865  0 19:47 pts/0    00:00:00 grep --color=auto nginx
[root@Alen nginx]# netstat -anpult | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2888/nginx: master 
[root@Alen nginx]# vim conf/nginx.conf
[root@Alen nginx]# netstat -anpult | grep 800
[root@Alen nginx]# ./sbin/nginx -s reload
[root@Alen nginx]# netstat -anpult | grep 800
tcp        0      0 0.0.0.0:800             0.0.0.0:*               LISTEN      2888/nginx: master  

  After starting Nginx, view port and found it occupied is the default port 80, then, by modifying the configuration file, it will change the port to 800. View port again, because there is no restart service, its new configuration is not in force. In this case, it can be manipulated reload the configuration file, it is arranged by the old smooth transition to the new configuration. After the heavy load profile, you can see the nginx occupied port into a 800.

Hot deployment:

# Backup the original binary file
[root@Alen nginx]# cd sbin/ && ls
nginx
[root@Alen sbin]# cp nginx nginx.old
[root@Alen sbin]# ls
nginx  nginx.old
# With the binary source package covering the active binaries
[root @ Alen sbin] # cd / opt / nginx- 1:16 . 1 / Objs / 
[root @ Alen Objs] # ls
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
Makefile nginx. 8   ngx_auto_headers.h ngx_modules.o
[root @ Alen objs] # cp nginx / opt / nginx / sbin /
 cp : overwrite " / opt / nginx / sbin / nginx " ? y
# Observe the process ID of the main process
[root @ Alen objs] # ps -ef | grip nginx
root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
nobody     2929   2888  0 19:57 ?        00:00:00 nginx: worker process
root       2988   2865  0 20:06 pts/0    00:00:00 grep --color=auto nginx
# Sending signals to the main version upgrade process
[root@Alen objs]# kill -USR2 2888
# In this case nginx nginx will serve as a new primary process, old and new work processes at the same time, but the old master process is no longer listening port of 800
[root @ Alen objs] # ps -ef | grip nginx
root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
nobody     2929   2888  0 19:57 ?        00:00:00 nginx: worker process
root       2989   2888  0 20:07 ?        00:00:00 nginx: master process ./sbin/nginx
nobody     2990   2989  0 20:07 ?        00:00:00 nginx: worker process
root       2992   2865  0 20:07 pts/0    00:00:00 grep --color=auto nginx
# Nginx sends a signal to the old master process, asking it to gracefully shut down all worker processes
[root@Alen objs]# kill -WINCH 2888
# At this point, all requests have been migrated to the new nginx upgraded, the old worker process has exited, but the old master process still exists. Because there may be some problems, we need to roll back the new version of the old version, then you can also send commands to reload the old master process, the worker processes pull it back up. Then put the new master process to switch off, so the old master process is not automatically exit, allowed to do a good version rollback.
[root @ Alen objs] # ps -ef | grip nginx
root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
root       2989   2888  0 20:07 ?        00:00:00 nginx: master process ./sbin/nginx
nobody     2990   2989  0 20:07 ?        00:00:00 nginx: worker process
root       2994   2865  0 20:08 pts/0    00:00:00 grep --color=auto nginx

Cutting logs:

# Manual backup of the original log
[root@Alen objs]# cd /opt/nginx/logs/
[root@Alen logs]# mv access.log access.log.bak
[root@Alen logs]# ls
access.log.bak  error.log  nginx.pid  nginx.pid.oldbin
# Transmit signal will regenerate a new log file
[root@Alen logs]# ../sbin/nginx -s reopen
[root@Alen logs]# ls
access.log  access.log.bak  error.log  nginx.pid  nginx.pid.oldbin
# Write Scheduled Tasks
[root@Alen logs]# crontab -e
# View Scheduled Tasks
[root@Alen logs]# crontab -l
0 0 1 * * bash /root/rotate.sh

  Here Scheduled Tasks script in two steps, one for the log backups, to name a timestamp, you can use the system variable date; two for sending signals to generate a new log. Log log cutting speed according to the frequency of the generated definable.

 

Guess you like

Origin www.cnblogs.com/Axiao-47/p/12296630.html