標籤:style class blog code http ext
3 Nginx基本配置
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 效能測試
測試效能的工具很多,我在這裡僅介紹三種測試伺服器效能的工具。這三種工具是為壓力測試而設計的,各有各的優勢。
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 runscontinuous tests andautomatically increases request rates until your server getssaturated. One of the interestingfeatures of Autobench is that it generates a .tsvreport that you can open withvarious applications to generate graphs. You maydownload the source code from theauthor's personal website: http://www.xenoclast.org/autobench/. Onceagain, extract the files from the archive, runmake then make install.Although it supports testing ofmultiple hosts at once, we will only be using thesingle host test for moresimplicity. The command we will execute resembles thehttperf one:[[email protected] ~]$ 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.tsvThe 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 byafter 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 isconsidered 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的優雅升級。