http://nginx.org/en/docs/beginners_guide.html
This guide gives a basic introduction to nginx and describes some simple tasks that can be done with it. It is supposed that nginx is already installed on the reader’s machine. If it is not, see the Installing nginx page. This guide describes how to start and stop nginx, and reload its configuration, explains the structure of the configuration file and describes how to set up nginx to serve out static content, how to configure nginx as a proxy server, and how to connect it with a FastCGI application.
这个指南给出了nginx的基本介绍,并描述了一些可以用它来完成的简单任务。假设nginx已经安装在读者的机器上了。如果没装,请查看安装nginx页面。这个指南描述了如何启动和停止nginx,以及如何重新加载它的配置,解释了配置文件的结构,描述了如何设置nginx以提供静态内容,如何配置nginx作为代理服务器,以及如何将其与FastCGI应用程序连接。
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 .
nginx有一个主进程和几个工作进程。主进程的主要目的是读取和评估配置,并维护辅助进程。工作进程对请求进行实际处理。nginx采用基于事件的模型和依赖于操作系统的机制来有效地在worker进程之间分配请求。工作进程的数量在配置文件中定义,可以根据给定的配置进行固定,也可以根据可用CPU内核的数量进行自动调整。
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及其模块的工作方式在配置文件中确定。默认情况下,配置文件名为nginx,且放置在以上3个目录中。
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,请运行可执行文件。一旦nginx被启动,它可以通过调用可执行文件的-s参数来控制。使用以下语法:
nginx -s signal
Where signal may be one of the following:
其中信号可为下列其中一种: (可以看出 -s ,其中 s 是signal
单词的缩写。)
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进程,等待工作进程完成当前的请求,可以执行以下命令:
nginx -s quit
This command should be executed under the same user that started nginx.
这个命令应该在启动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或它被重新启动。
要重新加载配置,请执行:
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.
一旦主进程收到重新加载配置的信号,它将检查新配置文件的语法有效性,并尝试应用其中提供的配置。如果成功,主进程将启动新的工作进程,并向旧的工作进程发送消息,请求它们关闭。否则,主进程回滚更改并继续使用旧配置。旧的工作进程,不再接收命令,停止接受新的连接,继续服务当前请求,直到所有这些请求都得到服务。之后,就进程处理退出。
A signal may also be sent to nginx processes with the help of Unix tools such as the kill
utility. In this case a signal is sent directly to a process with a given process ID. The process ID of the nginx master process is written, by default, to the nginx.pid
in the directory /usr/local/nginx/logs
or /var/run
. For example, if the master process ID is 1628, to send the QUIT signal resulting in nginx’s graceful shutdown, execute:
在kill工具等Unix工具的帮助下,信号也可以被发送到nginx进程。在这种情况下,信号被直接发送到具有给定进程ID的进程。默认情况下,nginx主进程的进程ID被写入nginx。在/usr/local/nginx/logs或/var/run目录中的pid。例如,如果主进程ID为1628,要发送退出信号导致nginx优雅关机,请执行:
kill -s QUIT 1628
这个命令相当于,执行nginx -s quit
。
For getting the list of all running nginx processes, the ps
utility may be used, for example, in the following way:
为了获得所有运行的nginx进程的列表,ps实用程序可以使用,例如,以下方式:
ps -ax | grep nginx
Nginx升级
第一步:将正在运行的nginx
二进制可执行文件复制一份,命名为nginx.old
。cp nginx nginx.old
第二步:将新编译好的nginx
二进制可执行文件覆盖,现正运行的nginx
二进制可执行文件。cp xxx/nginx nginx
第三步:给正在运行的master进程发送信号,版本升级。kill -USR2 13195
,此时请求不再进入老的master的工作进程了。
第四步:给老的master进程发送信号,关闭老的工作进程。kill -WINCH 13195
。
此时,老的master进程还不会退出,以供我们做版本回退。
Nginx日志切割
mv access.log access.log.backup && ../sbin/nginx -s reopen
先将日志改名或移动其他位置,再使用nginx -s reopen
命令重新打开日志。
通常会把以上命令放入bash脚本中,实现一个定时任务,每天有新的日志文件生成。
以上的内容,不妨来尝试一下。如果还有不理解的地方,我请客,请你看个视频[视频地址],你就懂了。