当前位置:Gxlcms > PHP教程 > nginx基本介绍(基于官方文档)

nginx基本介绍(基于官方文档)

时间:2021-07-01 10:21:17 帮助过:42人阅读

nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests. nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. The number of worker processes is defined in the configuration file and may be fixed for a given configuration or automatically adjusted to the number of available CPU cores (see worker_processes).

The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

nginx由一个master进程和若干worker进程组成,master进程主要用于读取配置文件和管理worker进程.worker进程负责处理请求。nginx基于事件模型并且能够根据操作系统的特性高效的利用worker进程处理请求。worker进程的数量在配置文件中定义,也可以通过指定的配置文件来定义其数量,或者自动根据CPU核心数确定其数量。
nginx以及相关模块的工作方式通过配置文件来指定。默认情况下,配置文件命名为nginx.conf,位于/usr/local/nginx/conf 或 /etc/nginx 或 /usr/local/etc/nginx 路径下。

以下内容为nginx启动,关闭,重载配置文件的相关操作

Starting, Stopping, and Reloading Configuration

To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

nginx -s signal

Where signal may be one of the following:

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

For example, to stop nginx processes with waiting for the worker processes to finish serving current requests, the following command can be executed:

nginx -s quit
This command should be executed under the same user that started nginx.

Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, execute:

nginx -s reload

Once the master process receives the signal to reload configuration, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes and sends messages to old worker processes, requesting them to shut down. Otherwise, the master process rolls back the changes and continues to work with the old configuration. Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.


一旦启动了nginx,就可以通过nginx -s [signal]命令的方式来控制nginx.其中[signal]可以为如下的命令:

  • stop — 快速停止
  • quit — 平滑关闭
  • reload — 重新加载配置文件
  • reopen — 重新打开日志文件
例如,等待worker进程处理完当前请求后再关闭nginx,可以使用

nginx -s quit

当配置文件被更改,只有当nginx被重启或者接收到重载配置文件的命令时新的配置才会生效,为了更改配置文件后生效,可以使用

nginx -s reload

一旦master进程接收到重载配置文件的信号,它首先会检查新的配置文件是否有语法错误,如果没有错误,master进程将会采用新的配置,并启动新的worker进程,同时通知旧的worker进程让他们停止工作。否则,若配置文件存在错误,那么master进程仍然使用旧的配置,并且旧的worker进程将继续保持工作。一旦master进程通知worker进程停止工作,worker进程首先会停止接收链接,然后处理完当前的所有请求,之后再exit,结束执行。

以上就介绍了nginx基本介绍(基于官方文档),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行