nginx网站性能优化和测试2

yelinag 2011-09-19

目标

1、缓存动态脚本生成的html

2、缓存静态文件(ico|css|js|gif|jpe?g|png|txt),让nginx直接从缓存中提供静态文件,不用再透过apache2来提供(众所周知,Apache2的静态文件性能远不如nginx)

3、对首页($uri=/),控制合适的过期时间,既要考虑性能,也要考虑用户到即时访问最新信息。

环境

OS(操作系统):DebianGNU/LinuxLenny5.0

前端WebServer(反向代理):Nginx0.7.67

后端WebServer(处理动态页面):Apache2

涉及网站类型:主要是PHP,Typecho,Wordpress

参照:ispconfig使用nginx反向代理

过程记录

关闭Apache2的gzip压缩,交给nginx去压缩。

echo'SetEnvno-gzip'>>/etc/apache2/apache2.conf

nginx的配置文件添加如下内容:

http{

......

#添加一个名为STATIC的cache空间

proxy_cache_path/var/tmp/nginx-cache/STATIClevels=1:2keys_zone=STATIC:1000m

inactive=24hmax_size=1g;

}

server{

listen8.8.8.8:80;#你自己的IP

server_name_;#泛域名支持

gzipon;

gzip_staticon;

gzip_proxiedany;

gzip_disable"MSIE[1-5]\.";

gzip_comp_level9;

gzip_min_length1000;

gzip_buffers48k;

gzip_typestext/plainapplication/x-javascripttext/csstext/htmlapplication/xmltext/javascript;

location/{

proxy_passhttp://127.0.0.1:80;#后端的Apache2

proxy_redirectoff;

proxy_set_headerHost$host;

proxy_set_headerX-Real-IP$remote_addr;

proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

proxy_pass_headerSet-Cookie;#对用户传输Set-Cookie的http头,不然无法支持一些包含cookie的应用,比如我的typecho

proxy_hide_headerX-Powered-By;#隐藏不必要的头,减少传输数据

proxy_hide_headerX-Mod-Pagespeed;#隐藏不必要的头,减少传输数据

proxy_cacheSTATIC;#使用先前定义的cache空间

proxy_cache_valid2004043041m;

proxy_cache_use_staleerrortimeoutinvalid_headerupdating

http_500http_502http_503http_504;

proxy_cache_key"$host$uri$is_args$args$http_cookie";#这是关键,以免用户的cookie混用

location~*\.(ico|css|js|gif|jpe?g|png)${#针对静态文件单独处理

proxy_passhttp://127.0.0.1:80;#后端的Apache2

proxy_set_headerHost$host;

expiresmax;

break;

proxy_cacheSTATIC;

proxy_cache_valid2004043041m;

proxy_cache_use_staleerrortimeoutinvalid_headerupdating

http_500http_502http_503http_504;

proxy_cache_key$host$uri$is_args$args;#可以看出,与上面的相比,这里没有cookie,最大化利用静态文件的缓存。

}

}

}

压力测试

优化之前:

之所以用并发100,请求100,是因为1000的时候已经挂掉了(测试环境的硬件性能有限)……

ispconfig:/tmp#ab-c100-n100http://www.linuxzh.org/jobs.html

ThisisApacheBench,Version2.3<$Revision:655654$>

Copyright1996AdamTwiss,ZeusTechnologyLtd,http://www.zeustech.net/

LicensedtoTheApacheSoftwareFoundation,http://www.apache.org/

Benchmarking127.0.0.1(bepatient).....done

ServerSoftware:Apache

ServerHostname:www.linuxzh.org

ServerPort:80

DocumentPath:/jobs.html

DocumentLength:31483bytes

ConcurrencyLevel:100

Timetakenfortests:18.370seconds

Completerequests:100

Failedrequests:0

Writeerrors:0

Totaltransferred:3178200bytes

HTMLtransferred:3148300bytes

Requestspersecond:5.44[#/sec](mean)

Timeperrequest:18369.758[ms](mean)

Timeperrequest:183.698[ms](mean,acrossallconcurrentrequests)

Transferrate:168.96[Kbytes/sec]received

ConnectionTimes(ms)

minmean[+/-sd]medianmax

Connect:120.523

Processing:46798374912.1982518365

Waiting:45598204912.1980418350

Total:47098394911.6982718367

Percentageoftherequestsservedwithinacertaintime(ms)

50%9827

66%12902

75%14182

80%15097

90%16446

95%17511

98%18322

99%18367

100%18367(longestrequest)

优化之后:

ispconfig:tmp#ab-c1000-n1000http://www.linuxzh.org/jobs.html

ThisisApacheBench,Version2.3<$Revision:655654$>

Copyright1996AdamTwiss,ZeusTechnologyLtd,http://www.zeustech.net/

LicensedtoTheApacheSoftwareFoundation,http://www.apache.org/

Benchmarking210.56.192.69(bepatient)

Completed100requests

Completed200requests

Completed300requests

Completed400requests

Completed500requests

Completed600requests

Completed700requests

Completed800requests

Completed900requests

Completed1000requests

Finished1000requests

ServerSoftware:nginx

ServerHostname:www.linuxzh.org

ServerPort:80

DocumentPath:/

DocumentLength:17117bytes

ConcurrencyLevel:1000

Timetakenfortests:0.160seconds

Completerequests:1000

Failedrequests:0

Writeerrors:0

Totaltransferred:17451000bytes

HTMLtransferred:17117000bytes

Requestspersecond:6236.55[#/sec](mean)

Timeperrequest:160.345[ms](mean)

Timeperrequest:0.160[ms](mean,acrossallconcurrentrequests)

Transferrate:106283.28[Kbytes/sec]received

ConnectionTimes(ms)

minmean[+-sd]medianmax

Connect:35577.75868

Processing:55624.96271

Waiting:124015.73569

Total:1041195.7119130

Percentageoftherequestsservedwithinacertaintime(ms)

50%119

66%121

75%123

80%124

90%126

95%128

98%129

99%130

100%130(longestrequest)

注意

1、proxy_key中只有用户发出的Cookie才会单独生成一个缓存,也就是说,不会每个未登陆的用户都生成一个缓存文件。

另外,针对静态文件,不记录cookie。

2、记得关闭后端Server的gzip压缩,nginx会直接缓存gzip的内容,不支持gzip的浏览器可能会出现乱码等问题。

相关推荐

lwplvx / 0评论 2020-11-22
岁月如歌 / 0评论 2020-07-21