純靜態文件環境下的Nginx優化思路
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 |
一、編譯方面優化
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 |