純靜態文件環境下的Nginx優化思路
原創【51CTO獨家特稿】Nginx以其消耗資源少,承受并發量大,配置文件簡潔等特點,深受廣大sa們的喜歡,但是網上傳播的nginx 配置并沒有對做過多的優化。那么接下來,我就從某大型媒體網站的實際運維nginx優化角度,來給大家講解一下nginx主要優化的那些方面。
一、編譯方面優化
1、首先就要從configure 參數分析,根據網上最常用的configure 參數來說,大都是
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
應該說這個參數是通用的,適用于各種環境的需要,比如php環境、純靜態文件環境、代理環境等等。編譯nginx程序文件大約有2M大小,跟全面優化的500多K,相差了不少。
下面我們修改一下參數,減少不必要的功能。
純靜態文件環境參數
./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模塊等,在純靜態文件用不到的模塊,現在看看nginx程序文件是不是少了一些。
Php環境的話,只需要去掉--with-http_fastcgi_module 重新編譯即可。
代理環境的話,只需要去掉--with_proxy_module重新編譯即可。
2、去掉nginx 默認的debug跟蹤設置。這一步需要修改nginx 源碼。
cd nginx-1.0.x vim auto/cc/gcc
第175行
CFLAGS="$CFLAGS -g"
前面加#注釋掉改行。
這樣的話,編譯的參數,就會減少到500多K的標準,這樣在大并發量的條件下,性能提升明顯。
二、利用google-perftools來優化高并發條件下的nginx
在32位系統下,可以直接安裝google-peftools,64位條件下,需要先安裝libunwind庫。然后再nginx configure 參數增加--with-google_perftools_module 重新編譯安裝nginx 。
這里以64位環境為準
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 參數加上--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 工作進程優化
通常的做法是在nginx.conf 的
worker_processes 8;
下面增加
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
但是在純靜態文件環境下,我們可以增加nginx 工作進程,來提升nginx的工作效率。
工作進程 為24個,worker_cpu_affinity 可以這樣來調整
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的良好的特性,大幅提升網站的訪問速度。
作者簡介:崔曉輝,網名coralzd,大眾網系統管理員,精通網站系統架構、Unix技術。gtalk:coralzd@gmail.com
【51CTO.com獨家特稿,轉載請注明原文作者和出處。】
【編輯推薦】