首页 > 代码库 > 大型网站架构-性能优化
大型网站架构-性能优化
性能调优
硬件优化
cpu 主频高、更多核CPU http://product.pconline.com.cn/hot/server_cpu/ E3->i7处理器
内存 主频高、内存量大,ECC内存可以容错,奇偶校验,支持热拔插
磁盘 raid(磁盘阵列,数据同时保存到多块磁盘中速度提升,备份容错)、ssd(新的技术,固态硬盘,不用磁头,读写速度很快,成本高一些)
网卡 100M网卡指的是数据流,单位比特bite,1Byte字节=8bite比特,100M/8=12.5M字节/s秒
服务器网卡使用的千兆网卡,对应千兆交换机和路由器,双网卡绑定类似磁盘的raid传输加快。
架构优化
接入优质CDN 代理+缓存
优化nginx配置 && 调整内核参数 http://blog.chinaunix.net/uid-25266990-id-2985541.html(为什么不用apache http://blog.csdn.net/xifeijian/article/details/17385831)
worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数。
use epoll; Nginx采用epoll模型,异步非阻塞。对于Nginx来说,把一个完整的连接请求处理都划分成了事件,一个一个的事件。比如accept(), recv(),磁盘I/O,send()等,每部分都有相应的模块去处理,一个完整的请求可能是由几百个模块去处理。真正核心的就是事件收集和分发模块,这就是管理所有模块的核心。只有核心模块的调度才能让对应的模块占用CPU资源,从而处理请求。
keepalive_timeout 60; keepalive 超时时间。
open_file_cache max=102400 inactive=20s; open_file_cache_valid 30s;这个是指多长时间检查一次缓存的有效信息。
下面贴一个完整的内核优化设置:
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
下面是一个简单的nginx 配置文件:
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000
01000000;
error_log /www/log/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 8k;
fastcgi_temp_file_write_size 8k;
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{
listen 8080;
server_name backup.aiju.com;
index index.php index.htm;
root /www/html/;
location /status
{
stub_status on;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
expires 30d;
}
log_format access ‘$remote_addr -- $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" $http_x_forwarded_for‘;
access_log /www/log/access.log access;
}
设置浏览器缓存
(apache http://www.apelearn.com/bbs/thread-5357-1-1.html
http://www.apelearn.com/bbs/thread-11450-1-1.html
nginx http://www.apelearn.com/bbs/thread-11821-1-1.html)
启用压缩技术节省带宽消耗
(apache http://www.apelearn.com/bbs/thread-5528-1-1.html
nginx http://www.111cn.net/sys/nginx/69492.htm)
业务划块
不使用或者少使用共享存储,
nfs共享了数据,附件共享,但是程序就不应该共享,程序很久都不用修改,读取代码反而需要使用网路传输,占用网络资源,性能降低。
用nosql替代mysql,使用MongoDB做存储,不做缓存。
(http://blog.sina.com.cn/s/blog_493a84550101e9a1.html)
静态库:个人的联系方式,企业相关的资料文档,企业的地址信息,企业的账户信息
动态库:争取企业的拜访记录,不断变化
使用异步通信 (http://kb.cnblogs.com/page/537753/)
小案例(小米抢购系统 http://www.csdn.net/article/2014-11-07/2822545)
限流排队 (
http://my.oschina.net/yonghan/blog/473790)
程序优化
在线检测网站元素 http://www.webpagetest.org/
减少http请求数(合并css,js,图片)
记录慢执行/查询日志
将动态内容静态化
图片延迟加载
动静分离
应用无状态(淘宝session框架)
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1843018
大型网站架构-性能优化