Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/control.xml
1 views
1
<!--
2
Copyright (C) Igor Sysoev
3
Copyright (C) Nginx, Inc.
4
-->
5
6
<!DOCTYPE article SYSTEM "../../../dtd/article.dtd">
7
8
<article name="Controlling nginx"
9
link="/en/docs/control.html"
10
lang="en"
11
rev="7">
12
13
<section>
14
15
<para>
16
nginx can be controlled with signals.
17
The process ID of the master process is written to the file
18
<path>/usr/local/nginx/logs/nginx.pid</path> by default.
19
This name may be changed at configuration time, or in
20
<path>nginx.conf</path> using the
21
<link doc="ngx_core_module.xml" id="pid"/>
22
directive.
23
The master process supports the following signals:
24
<note>
25
<table>
26
27
<tr><td width="20%">TERM, INT</td><td>fast shutdown</td></tr>
28
<tr><td width="20%">QUIT</td><td>graceful shutdown</td></tr>
29
<tr><td width="20%">HUP</td><td>changing configuration,
30
keeping up with a changed time zone (only for FreeBSD and Linux),
31
starting new worker processes with a new configuration,
32
graceful shutdown of old worker processes</td></tr>
33
<tr><td width="20%">USR1</td><td>re-opening log files</td></tr>
34
<tr><td width="20%">USR2</td><td>upgrading an executable file</td></tr>
35
<tr><td width="20%">WINCH</td><td>graceful shutdown of worker processes</td></tr>
36
37
</table>
38
</note>
39
</para>
40
41
<para>
42
Individual worker processes can be controlled with signals as well,
43
though it is not required.
44
The supported signals are:
45
<note>
46
<table>
47
48
<tr><td width="20%">TERM, INT</td><td>fast shutdown</td></tr>
49
<tr><td width="20%">QUIT</td><td>graceful shutdown</td></tr>
50
<tr><td width="20%">USR1</td><td>re-opening log files</td></tr>
51
<tr><td width="20%">WINCH</td><td>abnormal termination for debugging
52
(requires <link doc="ngx_core_module.xml" id="debug_points"/> to be enabled)
53
</td></tr>
54
55
</table>
56
</note>
57
</para>
58
59
</section>
60
61
62
<section id="reconfiguration" name="Changing Configuration">
63
64
<para>
65
In order for nginx to re-read the configuration file, a HUP
66
signal should be sent to the master process.
67
The master process first checks the syntax validity, then tries
68
to apply new configuration, that is, to open log files and new
69
listen sockets.
70
If this fails, it rolls back changes and continues to work
71
with old configuration.
72
If this succeeds, it starts new worker processes, and
73
sends messages to old worker processes requesting them to
74
shut down gracefully.
75
Old worker processes close listen sockets and continue to service
76
old clients.
77
After all clients are serviced, old worker processes are shut down.
78
</para>
79
80
<para>
81
Let’s illustrate this by example.
82
Imagine that nginx is run on FreeBSD and the command
83
<programlisting>
84
ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'
85
</programlisting>
86
produces the following output:
87
<programlisting>
88
PID PPID USER %CPU VSZ WCHAN COMMAND
89
33126 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
90
33127 33126 nobody 0.0 1380 kqread nginx: worker process (nginx)
91
33128 33126 nobody 0.0 1364 kqread nginx: worker process (nginx)
92
33129 33126 nobody 0.0 1364 kqread nginx: worker process (nginx)
93
</programlisting>
94
</para>
95
96
<para>
97
If HUP is sent to the master process, the output becomes:
98
<programlisting>
99
PID PPID USER %CPU VSZ WCHAN COMMAND
100
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
101
33129 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx)
102
33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
103
33135 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
104
33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
105
</programlisting>
106
</para>
107
108
<para>
109
One of the old worker processes with PID 33129 still continues to work.
110
After some time it exits:
111
<programlisting>
112
PID PPID USER %CPU VSZ WCHAN COMMAND
113
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
114
33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
115
33135 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
116
33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
117
</programlisting>
118
</para>
119
120
</section>
121
122
123
<section id="logs" name="Rotating Log-files">
124
125
<para>
126
In order to rotate log files, they need to be renamed first.
127
After that USR1 signal should be sent to the master process.
128
The master process will then re-open all currently open log files and
129
assign them an unprivileged user under which the worker processes
130
are running, as an owner.
131
After successful re-opening, the master process closes all open files and
132
sends the message to worker process to ask them to re-open files.
133
Worker processes also open new files and close old files right away.
134
As a result, old files are almost immediately available for post
135
processing, such as compression.
136
</para>
137
138
</section>
139
140
141
<section id="upgrade" name="Upgrading Executable on the Fly">
142
143
<para>
144
In order to upgrade the server executable, the new executable file
145
should be put in place of an old file first.
146
After that USR2 signal should be sent to the master process.
147
The master process first renames its file with the process ID to a
148
new file with the <path>.oldbin</path> suffix, e.g.
149
<path>/usr/local/nginx/logs/nginx.pid.oldbin</path>,
150
then starts a new executable file that in turn starts new
151
worker processes:
152
<programlisting>
153
PID PPID USER %CPU VSZ WCHAN COMMAND
154
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
155
33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
156
33135 33126 nobody 0.0 1380 kqread nginx: worker process (nginx)
157
33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
158
36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
159
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
160
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
161
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
162
</programlisting>
163
</para>
164
165
<!--
166
167
<para>
168
The process 36264 with a new executable file creates its own file
169
with the <path>.newbin</path> suffix that will keep the process ID,
170
e.g. <path>/usr/local/nginx/logs/nginx.pid.newbin</path>.
171
</para>
172
173
-->
174
175
<para>
176
After that all worker processes (old and new ones) continue to accept requests.
177
If the WINCH signal is sent to the first master process, it will
178
send messages to its worker processes, requesting them to shut
179
down gracefully, and they will start to exit:
180
<programlisting>
181
PID PPID USER %CPU VSZ WCHAN COMMAND
182
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
183
33135 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx)
184
36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
185
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
186
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
187
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
188
</programlisting>
189
</para>
190
191
<para>
192
After some time, only the new worker processes will process requests:
193
<programlisting>
194
PID PPID USER %CPU VSZ WCHAN COMMAND
195
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
196
36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
197
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
198
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
199
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
200
</programlisting>
201
</para>
202
203
<para>
204
It should be noted that the old master process does not close its listen
205
sockets, and it can be managed to start its worker processes again if needed.
206
If for some reason the new executable file works unacceptably, one of the
207
following can be done:
208
<list type="bullet">
209
210
<listitem>
211
<para>
212
Send the HUP signal to the old master process.
213
The old master process will start new worker processes
214
without re-reading the configuration.
215
After that, all new processes can be shut down gracefully,
216
by sending the QUIT signal to the new master process.
217
</para>
218
</listitem>
219
220
<listitem>
221
<para>
222
Send the TERM signal to the new master process.
223
It will then send a message to its worker processes requesting them
224
to exit immediately, and they will all exit almost immediately.
225
(If new processes do not exit for some reason,
226
the KILL signal should be sent to them to force them to exit.)
227
When the new master process exits, the old master process will start new
228
worker processes automatically.
229
</para>
230
</listitem>
231
232
</list>
233
234
</para>
235
236
<para>
237
If the new master process exits then the old master process discards
238
the <path>.oldbin</path> suffix from the file name with the process ID.
239
</para>
240
241
<para>
242
If upgrade was successful, then the QUIT signal should be sent to
243
the old master process, and only new processes will stay:
244
<programlisting>
245
PID PPID USER %CPU VSZ WCHAN COMMAND
246
36264 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
247
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
248
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
249
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
250
</programlisting>
251
</para>
252
253
<!--
254
255
<para>
256
To complete the upgrade process, the
257
<path>/usr/local/nginx/logs/nginx.pid.newbin</path> file should be renamed to
258
<path>/usr/local/nginx/logs/nginx.pid</path>.
259
</para>
260
261
-->
262
263
</section>
264
265
</article>
266
267