纯静态文件环境下的Nginx优化思路

原创
系统 Linux
Nginx以其消耗资源少,承受并发量大,配置文件简洁等特点,深受广大sa们的喜欢,但是网上传播的nginx配置并没有对做过多的优化。本文从某大型媒体网站的实际运维nginx优化角度,来给大家讲解一下在纯静态文件环境下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独家特稿,转载请注明原文作者和出处。】

【编辑推荐】

  1. Cacti如何监控Nginx运行状态
  2. FreeBSD 8.0+Nginx+PHP配置高性能Web平台
  3. 《Linux运维趋势》第13期:服务器优化

 

责任编辑:yangsai 来源: 51CTO.com
相关推荐

2010-07-23 10:42:03

2011-09-10 19:51:07

云计算云安全

2010-03-30 14:22:01

Nginx静态文件

2016-08-29 21:36:55

nginxWeb缓存

2010-03-29 09:23:00

2010-03-25 18:09:23

Nginx配置文件

2013-06-05 13:31:25

2010-01-08 09:43:40

Ubuntu ngin

2018-12-18 09:00:26

Kubernetes工作负载测试

2011-07-18 18:01:34

buffer cach

2023-06-23 15:22:28

JettyJava

2020-03-23 22:50:36

WindowsNginxTomcat

2022-03-02 11:13:50

Web前端开发

2012-04-25 09:24:17

Java

2011-09-08 11:02:39

Web2.0网康

2010-07-27 14:25:02

linux文件编码

2009-12-09 13:23:24

静态路由配置

2012-12-27 10:15:13

金融行业

2012-09-24 01:01:49

NginxNginx性能Web服务器

2014-04-04 10:16:51

Nginx配置Nginx性能优化
点赞
收藏

51CTO技术栈公众号