Nginx has only a few command-line parameters. Unlike many other software systems, the configuration is done entirely via the configuration file (imagine that).
Options
-c </path/to/config> Specify which configuration file Nginx should use instead of the default.
-g Set global directives. (version >=0.7.4)
-t Don't run, just test the configuration file. nginx checks configuration for correct syntax and then try to open files referred in configuration.
-v Print version.
-V Print nginx version, compiler version and configure parameters.
Example
/usr/bin/nginx -t -c ~/mynginx.conf -g "pid /var/run/nginx.pid; worker_processes 2;"
Controlling Nginx Via the Signals
You can control the master process by using signals. By default nginx writes its master process pid to /usr/local/nginx/logs/nginx.pid. You can change this by passing parameter with ./configure at compile-time or by using pid directive in the configuration file.
The master process can handle the following signals:
TERM, INT |
Quick shutdown |
QUIT |
Graceful shutdown |
HUP |
Configuration reload |
USR1 |
Reopen the log files |
USR2 |
Upgrade Executable on the fly |
WINCH |
Gracefully shutdown the worker processes |
There's no need to control the worker processes yourself. However, they support some signals, too:
TERM, INT |
Quick shutdown |
QUIT |
Graceful shutdown |
USR1 |
Reopen the log files |
Loading a New Configuration Using Signals
Nginx supports a few signals that you can use to control it's operation while it's running.
The most common of these is 15, which just stops the running process:
# ps aux | egrep '(PID|nginx)' USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2213 0.0 0.0 6784 2036 ? Ss 03:01 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf # kill -15 2213
The more interesting option however, is being able to change the nginx configuration on the fly (notice that we test the configuration prior to reloading it):
# nginx -t -c /etc/nginx/nginx.conf 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf syntax is ok 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf was tested successfully # ps aux | egrep '(PID|nginx)' USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2213 0.0 0.0 6784 2036 ? Ss 03:01 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf # kill -HUP 2213
What happens is that when nginx receives the HUP signal, it tries to parse the configuration file (the specified one, if present, otherwise the default), and if successful, tries to apply a new configuration (i.e. re-open the log files and listen sockets). If successful, nginx runs new worker processes and signals graceful shutdown to old workers. Notified workers close listen sockets but continue to serve current clients. After serving all clients old workers shutdown. If nginx couldn't successfully applying the new configuration, it continues to work with an old configuration.
RequestForReviewCategory -- (Request For Review: Just What Happens With The Worker Processes at a HUP? -Olle)
Upgrading To a New Binary On The Fly
If you need to replace nginx binary with a new one (when upgrading to a new version or adding/removing server modules), you can do it without any service downtime - no incoming requests will be lost.
First, replace old binary with a new one, then send USR2 signal to the master process. It renames its .pid file to .oldbin (e.g. /usr/local/nginx/logs/nginx.pid.oldbin), then executes a new binary, which in turn starts a new master process and the new worker processes:
PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 33135 33126 nobody 0.0 1380 kqread nginx: worker process (nginx) 33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
At this point, two instances of nginx are running, handling the incoming requests together. To phase the old instance out, you have to send WINCH signal to the old master process, and its worker processes will start to gracefully shut down:
PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33135 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
After some time, old worker processes all quit and only new worker processes are handling the incoming requests:
PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
At this point you can still revert to the old server because it hasn't closed its listen sockets yet, by following these steps:
- Send HUP signal to the old master process - it will start the worker processes without reloading a configuration file
- Send QUIT signal to the new master process to gracefully shut down its worker processes
- Send TERM signal to the new master process to force it quit
- If for some reason new worker processes do not quit, send KILL signal to them
After new master process quits, the old master process removes .oldbin suffix from its .pid file, and everything is exactly as before the upgrade attempt.
If an update is successful and you want to keep the new server, send QUIT signal to the old master process to leave only new server running:
PID PPID USER %CPU VSZ WCHAN COMMAND
36264 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
