純靜態(tài)文件環(huán)境下的Nginx優(yōu)化思路
原創(chuàng)【51CTO獨家特稿】Nginx以其消耗資源少,承受并發(fā)量大,配置文件簡潔等特點,深受廣大sa們的喜歡,但是網(wǎng)上傳播的nginx 配置并沒有對做過多的優(yōu)化。那么接下來,我就從某大型媒體網(wǎng)站的實際運維nginx優(yōu)化角度,來給大家講解一下nginx主要優(yōu)化的那些方面。
一、編譯方面優(yōu)化
1、首先就要從configure 參數(shù)分析,根據(jù)網(wǎng)上最常用的configure 參數(shù)來說,大都是
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
應(yīng)該說這個參數(shù)是通用的,適用于各種環(huán)境的需要,比如php環(huán)境、純靜態(tài)文件環(huán)境、代理環(huán)境等等。編譯nginx程序文件大約有2M大小,跟全面優(yōu)化的500多K,相差了不少。
下面我們修改一下參數(shù),減少不必要的功能。
純靜態(tài)文件環(huán)境參數(shù)
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --without-http_fastcgi_module --without-http_proxy_module --without-http_upstream_ip_hash_module --without-http_autoindex_module --without-http_ssi_module --without-http_proxy_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --without-http_memcached_module
去掉了在mail模塊fastcgi模塊 代理模塊 ip_hash模塊等,在純靜態(tài)文件用不到的模塊,現(xiàn)在看看nginx程序文件是不是少了一些。
Php環(huán)境的話,只需要去掉--with-http_fastcgi_module 重新編譯即可。
代理環(huán)境的話,只需要去掉--with_proxy_module重新編譯即可。
2、去掉nginx 默認(rèn)的debug跟蹤設(shè)置。這一步需要修改nginx 源碼。
cd nginx-1.0.x vim auto/cc/gcc
第175行
CFLAGS="$CFLAGS -g"
前面加#注釋掉改行。
這樣的話,編譯的參數(shù),就會減少到500多K的標(biāo)準(zhǔn),這樣在大并發(fā)量的條件下,性能提升明顯。
二、利用google-perftools來優(yōu)化高并發(fā)條件下的nginx
在32位系統(tǒng)下,可以直接安裝google-peftools,64位條件下,需要先安裝libunwind庫。然后再nginx configure 參數(shù)增加--with-google_perftools_module 重新編譯安裝nginx 。
這里以64位環(huán)境為準(zhǔn)
1)安裝libunwind庫
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz tar zxvf libunwind-0.99.tar.gz cd libunwind-0.99/ CFLAGS=-fPIC ./configure –prefix=/usr make CFLAGS=-fPIC make CFLAGS=-fPIC install
2)安裝google-perftools
wget http://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz tar xzvf google-perftools-1.7.tar.gz cd google-perftools-1.7
然后開始配置:
./configure --prefix=/usr --enable-frame-pointers (32位可以不添加--enable-frame-pointers) make --j4 && make install
nginx configure 參數(shù)加上--with-google-perftools 重新編譯nginx
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --without-http_fastcgi_module --without-http_proxy_module --without-http_upstream_ip_hash_module --without-http_autoindex_module --without-http_ssi_module --without-http_proxy_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --without-http_memcached_module –with-google_perftools_module make && make install
3、在nginx.conf 的pid部分下,增加
google_perftools_profiles /data0/google_cache;
重啟
service nginx restart
即可生效。
三、nginx 工作進(jìn)程優(yōu)化
通常的做法是在nginx.conf 的
worker_processes 8;
下面增加
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
但是在純靜態(tài)文件環(huán)境下,我們可以增加nginx 工作進(jìn)程,來提升nginx的工作效率。
工作進(jìn)程 為24個,worker_cpu_affinity 可以這樣來調(diào)整
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
重啟nginx后生效,可以充分利用nginx的對多核心cpu的良好的特性,大幅提升網(wǎng)站的訪問速度。
作者簡介:崔曉輝,網(wǎng)名coralzd,大眾網(wǎng)系統(tǒng)管理員,精通網(wǎng)站系統(tǒng)架構(gòu)、Unix技術(shù)。gtalk:coralzd@gmail.com
【51CTO.com獨家特稿,轉(zhuǎn)載請注明原文作者和出處?!?/p>
【編輯推薦】