3 Nginx基本配置
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvam9obl9mX2xhdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
3.1 时间模型
事件模型尾随着指令,它同意你网络机制。有一些參数对于应用程序的性能有重要的影响。
比方,以下的指令片段所看到的:
user nginx nginx;
master_process on;
worker_processes 4;
events {
worker_connections 1024;
use epoll;
}
[...]
配置了4个进程,每一个进程的处理事务的个数能够同一时候支持1024事件,使用epoll选择机制。
3.2 配置模型
Nginx配置模型是一个简单模型,它使用include指令来激活文件的包括功能,还包括organization 和inclusions。这个指令可以被插入到配置文件的不论什么位置,接受一个文件路径作为參数。比方,以下这些指令。
include /file/path.conf;
include sites/*.conf;
3.3 性能測试
測试性能的工具非常多,我在这里仅介绍三种測试server性能的工具。这三种工具是为压力測试而设计的,各有各的优势。
l Httperf: 它是由惠普开发的,一个相对著名的开源有用程序,只执行在Linux操作系统中。
l Autobench:Perl为Httperf的打包器,提升了測试机制和产生更细节的报告。
l OpenWebLoad:更小尺寸开源压力測试应用程序,它支持windows和linux平台。
更为具体的介绍,请查阅有关资料。比方,介绍Autobench,外文资料。
Autobench is a Perl script that makes use of httperf more efficiently—it runs
continuous tests andautomatically increases request rates until your server gets
saturated. One of the interestingfeatures of Autobench is that it generates a .tsv
report that you can open withvarious applications to generate graphs. You may
download the source code from theauthor's personal website: http://www.
xenoclast.org/autobench/. Onceagain, extract the files from the archive, run
make then make install.
Although it supports testing ofmultiple hosts at once, we will only be using the
single host test for moresimplicity. The command we will execute resembles the
httperf one:
[alex@example ~]$ autobench--single_host --host1 192.168.1.10 --uri1 /
index.html --quiet --low_rate20 --high_rate 200 --rate_step 20 --num_
call 10 --num_conn 5000--timeout 5 --file results.tsv
The switches can be configured asfollows:
• --host1: The website host name you wish to test
• --uri1: The path of the file that will be downloaded
• --quiet: Does not display httperf information on the screen
• --low_rate: Connections per second at the beginning of the test
• --high_rate: Connections per second at the end of the test
• --rate_step: The number of connections to increase the rate by
after each test
• --num_call: How many requests should be sent per connection
• --num_conn: Total amount of connections
• --timeout: The number of seconds elapsed before a request is
considered lost
• --file: Export results as specified (.tsv file)
3.4优雅升级Nginx
幸运地是,Nginx嵌入了一种机制。它同意你不中断执行时间的情况下切换二进制文件。假设你依照以下的步骤操作,你不会有请求数据的丢失。
- 用新的二进制文件代替老的Nginx二进制(默认情况下。/usr/local/nginx/sbin/nginx)文件。
- 找到Nginx主(master)进程pid。比方,使用命令ps x|grep nginx |grep master,或者查看在pid文件里的值。
- 发送一个USR2 (12)信号给master进程,使用kill –USR2 ***。用步骤2产生的pid值代替***。这将会通过又一次命名老的pid和执行新的二进制文件来初始化升级。
- 给老的master进程发送WINCH (28),kill –WINCH ***(同上),这将会着手于优雅地关闭老的工作进程。
- 确保全部的老的进程中断了,然后给老的进程发送QUIT信号,去kill –QUIT ***。
恭喜,这样就完毕Nginx的优雅升级。