设为首页 收藏本站
查看: 759|回复: 0

[经验分享] nginx网站性能优化和测试2

[复制链接]

尚未签到

发表于 2016-12-26 08:08:09 | 显示全部楼层 |阅读模式
  目标
1、缓存动态脚本生成的html
2、缓存静态文件(ico|css|js|gif|jpe?g|png|txt),让nginx直接从缓存中提供静态文件,不用再透过apache2来提供(众所周知,Apache2的静态文件性能远不如nginx)
3、对首页($uri = /),控制合适的过期时间,既要考虑性能,也要考虑用户到即时访问最新信息。
环境
OS(操作系统):Debian GNU/Linux Lenny 5.0
前端Web Server(反向代理):Nginx 0.7.67
后端Web Server(处理动态页面):Apache2
涉及网站类型:主要是PHP,Typecho,Wordpress
参照:ispconfig使用nginx 反向代理
过程记录
关闭Apache2的gzip压缩,交给nginx去压缩。
echo 'SetEnv no-gzip' >> /etc/apache2/apache2.conf
nginx的配置文件添加如下内容:
http {
......
#添加一个名为STATIC的cache空间
proxy_cache_path /var/tmp/nginx-cache/STATIC levels=1:2 keys_zone=STATIC:1000m
inactive=24h max_size=1g;
}
server {
listen 8.8.8.8:80; #你自己的IP
server_name _; #泛域名支持
gzip on;
gzip_static on;
gzip_proxied any;
gzip_disable "MSIE [1-5]\.";
gzip_comp_level 9;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css text/html application/xml text/javascript;
location / {
proxy_pass http://127.0.0.1:80; #后端的Apache2
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header Set-Cookie; #对用户传输Set-Cookie的http头,不然无法支持一些包含cookie的应用,比如我的typecho
proxy_hide_header X-Powered-By; #隐藏不必要的头,减少传输数据
proxy_hide_header X-Mod-Pagespeed; #隐藏不必要的头,减少传输数据
proxy_cache STATIC; #使用先前定义的cache空间
proxy_cache_valid 200 404 304 1m;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
proxy_cache_key "$host$uri$is_args$args$http_cookie"; #这是关键,以免用户的cookie混用
location ~* \.(ico|css|js|gif|jpe?g|png)$ { #针对静态文件单独处理
proxy_pass http://127.0.0.1:80; #后端的Apache2
proxy_set_header Host $host;
expires max;
break;
proxy_cache STATIC;
proxy_cache_valid 200 404 304 1m;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
proxy_cache_key $host$uri$is_args$args; #可以看出,与上面的相比,这里没有cookie,最大化利用静态文件的缓存。
}
}
}
压力测试
优化之前:
之所以用并发100,请求100,是因为1000的时候已经挂掉了(测试环境的硬件性能有限)……
ispconfig:/tmp# ab -c 100 -n 100 http://www.linuxzh.org/jobs.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient).....done
Server Software: Apache
Server Hostname: www.linuxzh.org
Server Port: 80
Document Path: /jobs.html
Document Length: 31483 bytes
Concurrency Level: 100
Time taken for tests: 18.370 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 3178200 bytes
HTML transferred: 3148300 bytes
Requests per second: 5.44 [#/sec] (mean)
Time per request: 18369.758 [ms] (mean)
Time per request: 183.698 [ms] (mean, across all concurrent requests)
Transfer rate: 168.96 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 2 0.5 2 3
Processing: 467 9837 4912.1 9825 18365
Waiting: 455 9820 4912.1 9804 18350
Total: 470 9839 4911.6 9827 18367
Percentage of the requests served within a certain time (ms)
50% 9827
66% 12902
75% 14182
80% 15097
90% 16446
95% 17511
98% 18322
99% 18367
100% 18367 (longest request)
优化之后:
ispconfig:tmp# ab -c 1000 -n 1000 http://www.linuxzh.org/jobs.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 210.56.192.69 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx
Server Hostname: www.linuxzh.org
Server Port: 80
Document Path: /
Document Length: 17117 bytes
Concurrency Level: 1000
Time taken for tests: 0.160 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 17451000 bytes
HTML transferred: 17117000 bytes
Requests per second: 6236.55 [#/sec] (mean)
Time per request: 160.345 [ms] (mean)
Time per request: 0.160 [ms] (mean, across all concurrent requests)
Transfer rate: 106283.28 [Kbytes/sec] received
Connection Times (ms)
min mean[+-sd] median max
Connect: 35 57 7.7 58 68
Processing: 55 62 4.9 62 71
Waiting: 12 40 15.7 35 69
Total: 104 119 5.7 119 130
Percentage of the requests served within a certain time (ms)
50% 119
66% 121
75% 123
80% 124
90% 126
95% 128
98% 129
99% 130
100% 130 (longest request)
注意
1、proxy_key 中只有用户发出的Cookie才会单独生成一个缓存,也就是说,不会每个未登陆的用户都生成一个缓存文件。
另外,针对静态文件,不记录cookie。
2、记得关闭后端Server的gzip压缩,nginx会直接缓存gzip的内容,不支持gzip的浏览器可能会出现乱码等问题。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-319360-1-1.html 上篇帖子: 搭建rails+passenger+nginx开发环境 下篇帖子: nginx根据cookie里的信息分流
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表