纯静态文件环境下的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

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 installnginx

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的良好的特性,大幅提升网站的访问速度。 

相关内容