Lost nginx.pid solution after restarting nginx

  • 2020-05-07 20:54:24
  • OfStack

1, nginx stop operation

The stop operation is achieved by sending a signal to the nginx process.
Step 1: query the nginx main process number


ps -ef | grep nginx

Look for the master process in the process list, and its number is the main process number.

Step 2: send the signal

Leisurely stop Nginx:
kill-QUIT main process number
Quick stop Nginx:
kill-TERM main process number
Force Nginx to stop:


pkill -9 nginx

In addition, if the pid file storage path is configured in nginx.conf, the Nginx main process number will be stored in the Nginx file. If not specified, it will be placed in the logs directory of nginx. With the pid document, we do not need to query the main process number of Nginx first, but directly send a signal to Nginx. The command is as follows:


kill - Signal types  '/usr/nginx/logs/nginx.pid'

2, nginx smooth restart

If you change the configuration, restart Nginx, close Nginx and then open it. No, you can send a signal to Nginx to smooth the restart.
Smooth restart command:


kill -HUP  Live in title or process number file path 

Or use
/usr/nginx/sbin/nginx -s reload
Note that after modifying the configuration file, it is better to check whether the modified configuration file is correct or not first, so as to avoid errors appearing on Nginx after restart, which will affect the stable operation of the server. To determine whether the Nginx configuration is correct, the command is as follows:
 


nginx -t -c /usr/nginx/conf/nginx.conf
 or 
/usr/nginx/sbin/nginx -t

3, nginx smooth upgrade

If the Nginx running on the server needs to upgrade, add or delete modules, we need to stop the server and make corresponding modifications, so that the server will be out of service within 1 period of time, Nginx can carry out various upgrade actions without affecting the server.

Step 1:

To upgrade the Nginx program, replace the old program file with a new program, and compile and install the new program directly into the Nginx installation directory.

Step 2: execute the command

kill-USR2 the main process number or process file name of the older version of the program

At this point the old Nginx main process will rename its own process file to.oldbin and then execute the new Nginx. The new and old Nginx operate in conjunction with the city to process requests.
To phase out the old Nginx, type:

kill -WINCH old moderator process number

Over time, the old worker process will exit with the completion of the task, and the new Nginx worker process will gradually replace the old worker process.
You can decide whether to use the new version or revert to the old version.

4, starts the new/old worker process without overloading the configuration

kill -HUP old/new moderator process number
Gracefully close the old/new process
kill -QUIT old/new main process number
If an error is reported at this time, if the process is not finished, close the old/new worker process and then the main process number with the following command:
kill -TERM old/new working process number

In this way, if you want to restore to the old version, you only need a few steps above to operate the new moderator process number, if you want to use the new version of the above steps to operate the old moderator process number.

These are the basic operations of Nginx. Hopefully Nginx will have a better way to handle these operations in the future.

On ali cloud server, the process of nginx - s stop after restart nginx - s reload, always quote error nginx: [error] open () "/ alidata server nginx/logs/nginx pid" failed (2: No such file or directory).
Reason: nginx process killed after pid was lost, the next time to open nginx-s reload failed to start, reload can solve the problem, do not do, you decide, ha ha.

Or the English explanation is more professional:

issued a nginx -s stop and after that I got this error when trying to reload it.
[error]: invalid PID number "" in "/var/run/nginx.pid"
That /var/run/nginx/pid file is empty atm.
What do I need to do to fix it?

nginx -s reload is only used to tell a running nginx process to reload its config. After a stop, you don't have a running nginx process to send a signal to. Just run nginx (possibly with a -c /path/to/config/file)

Usage:


nginx -c /path/to/config/file
// My machine looks like this: /alidata/server/nginx/sbin/nginx -c /alidata/server/nginx/conf/nginx.conf


Related articles: