非常教程

Nginx参考手册

指南 | Guides

Controlling nginx

  • 更改配置
  • 旋转型日志文件
  • 即时升级可执行文件

nginx可以用信号来控制。 主进程的进程ID默认写入文件/usr/local/nginx/logs/nginx.pid。 这个名称可以在配置时更改,或者在nginx.conf中使用pid指令更改。 主进程支持以下信号:

TERM,INT

快速关机

QUIT

缓慢关机

HUP

更改配置,保持更改的时区(仅适用于FreeBSD和Linux),使用新配置启动新的工作进程,正常关闭旧的工作进程

USR1

重新打开日志文件

USR2

升级可执行文件

WINCH

正常关闭工作进程

个别工作进程也可以通过信号进行控制,尽管这不是必需的。支持的信号是:

TERM,INT

快速关机

QUIT

缓慢关机

USR1

重新打开日志文件

WINCH

调试异常终止(需要启用debug_points)

更改配置

为了让nginx重新读取配置文件,应该将一个HUP信号发送到主进程。主进程首先检查语法有效性,然后尝试应用新配置,即打开日志文件和新的侦听套接字。如果失败,它会回滚更改并继续使用旧配置。如果成功,它会启动新的工作进程,并向老工作进程发送消息,请求他们正常关闭。老工人进程关闭监听套接字并继续为老客户服务。在所有客户端都被服务之后,旧工作进程被关闭。

我们来举例说明这一点。想象一下,nginx在FreeBSD 4.x和命令上运行

ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'

产生以下输出:

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
33127 33126 nobody   0.0  1380 kqread nginx: worker process (nginx)
33128 33126 nobody   0.0  1364 kqread nginx: worker process (nginx)
33129 33126 nobody   0.0  1364 kqread nginx: worker process (nginx)

如果HUP发送到主进程,则输出变为:

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33129 33126 nobody   0.0  1380 kqread nginx: worker process is shutting down (nginx)
33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33135 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)

PID 33129的旧流程之一仍然继续工作。一段时间后退出:

  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  1368 kqread nginx: worker process (nginx)
33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)

旋转日志文件

为了旋转日志文件,他们需要首先重命名。之后,USR1信号应发送到主进程。然后,主进程将重新打开所有当前打开的日志文件,并将其作为所有者为其分配正在运行工作进程的非特权用户。成功重新打开后,主进程关闭所有打开的文件,并将消息发送给工作进程以要求他们重新打开文件。工作进程也会立即打开新文件并关闭旧文件。因此,旧文件几乎立即可用于后期处理,如压缩。

即时升级可执行文件

为了升级服务器可执行文件,应该先将新的可执行文件替换为旧文件。之后,USR2信号应发送到主进程。主进程首先使用进程标识将其文件重命名为带有.oldbin后缀的新文件,例如/usr/local/nginx/logs/nginx.pid.oldbin,然后启动一个新的可执行文件,然后启动新的工作进程:

  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)

之后,所有工作进程(旧的和新的)继续接受请求。如果WINCH信号发送到第一个主进程,它将向其工作进程发送消息,请求它们正常关闭,并且它们将开始退出:

  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)

一段时间后,只有新的工作进程会处理请求:

  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)

应该注意的是,旧的主进程没有关闭其侦听套接字,并且可以管理它,以便在需要时再次启动它的工作进程。如果由于某种原因新的可执行文件无法接受,可以采取以下措施之一:

  • 将HUP信号发送到旧的主进程。旧的主进程将启动新的工作进程而不重新读取配置。之后,通过发送QUIT信号到新的主进程,所有新进程都可以正常关闭。
  • 发送TERM信号到新的主进程。然后它会向其工作进程发送一条消息,要求他们立即退出,并且他们几乎立即退出。(如果新进程由于某种原因未退出,则应将KILL信号发送给它们以强制它们退出。)当新主进程退出时,旧主进程将自动启动新工作进程。

如果新的主进程退出,则旧的主进程将丢弃.oldbin具有进程ID的文件名后缀。

如果升级成功,则旧的主进程应该发送QUIT信号,并且只有新的进程将停留:

  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)
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module