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 |
應該說這個參數(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 默認的debug跟蹤設置。這一步需要修改nginx 源碼。
cd nginx-1.0.x vim auto/cc/gcc |
第175行
前面加#注釋掉改行。
這樣的話,編譯的參數(shù),就會減少到500多K的標準,這樣在大并發(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)境為準
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 |
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 |
應該說這個參數(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 默認的debug跟蹤設置。這一步需要修改nginx 源碼。
cd nginx-1.0.x vim auto/cc/gcc |
第175行
前面加#注釋掉改行。
這樣的話,編譯的參數(shù),就會減少到500多K的標準,這樣在大并發(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)境為準
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 |