Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388145
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1741
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-21 22:46
文章分类
文章存档

2014年(22)

2013年(45)

分类: IT业界

2014-01-06 11:25:27

 自从上次搭建完小型架构以后,还是比较稳定的,但有一个让人担心的烦恼,那就是机器的负载都很高,基本上都是百分之七八十的占用,不管是CPU还是内存,所以决定进行一次大规模的优化,决定优化完后在把成绩告诉老大,眼瞅着过年了,到时候没准多发点奖金啊,一想到奖金就来精神了,立马行动。

       重新设计架构图如下:

wKiom1LH4nKzLJs7AAChyRvKKSc190.jpg

主要的优化点:

   1、在负载均衡服务器上加上了web缓存

   2、在php服务器上加上了zendopcache(这个在项目成长记(二)中已经讲过了,这里就省略了)

   3、mysql配置优化(针对集群)

   4、利用现有的机器组成一个Memcached集群(也就是说这个Memcached机器不是机器)


下面就具体说一下升级点:

   一、web缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    
#Nginx主要通过proxy_cache模块来实现缓存
worker_processes  8;    #CPU多少核就写多少,利用cpu多核的优势
error_log  logs/error.log warn; #随时记录错误
pid        logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections  51200;   #允许更多的请求量
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local]'
                      ' "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    #关闭header头显示Nginx的版本号
    server_tokens off;
    keepalive_timeout  60;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 2 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
    gzip_vary on;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    upstream p2pwd {
            server 192.168.1.99 weight=1 max_fails=2 fail_timeout=30s;
            server 192.168.1.115 weight=1 max_fails=2 fail_timeout=30s;
        }
    #proxy_temp_path和proxy_cache_path必须是在一个分区,proxy_temp用于存储临时文件
    proxy_temp_path /data/soft/cache/proxy_temp_path;
    #levels用于设置目录结构(第一层是一个字符,第二层是2个字符,最多可以设置3层),keys_zone设置名称和热点内容存放的内存大小(由于Nginx会把热点内容放到内存中,所以速度比Squid等快很多,可以和varinsh媲美),inactive是多长时间没有访问就删掉,max_size是硬盘缓存的大小
    proxy_cache_path /data/soft/cache/proxy_cache_path levels=1:2 keys_zone=p2pwd:200m inactive=1d max_size=30g;
    server {
        listen       80;
        server_name  a.bcd.com;
        root /data/www/domain;
        index index.html index.php;
        access_log  logs/a.access.log  main;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        location ~ .*\.(gif|jpg|png|swf|bmp|jpeg|js|css)$ {
            proxy_cache p2pwd;
            #设置状态码对应的缓存时间
            proxy_cache_valid 200 304 12h;
            proxy_cache_valid any 1m;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
            #设置缓存键
            proxy_cache_key $host$uri$is_args$args;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass
        }
    }
}

当然这里没有删除缓存的功能,这个需要nginx编译一个第三方模块叫ngx_cache_purge,可以参考修改,这里还有一个缓存方式fastcgi_cache模块,他和proxy_cache基本一样,只是他负责缓存动态的内容,比如php生成的内容。


二、opcache和Memcached在前面的章节已经说过,这里不在赘述,但是需要说的一点是Memcached本身就支持分布式,可以通过编程语言实现分布式,比如php可以通过Memcached::addServers ( array$servers )添加多个服务器,就算有一台挂了也不怕。还有一个地方就是php.ini文件里,有一个expose_php On配置,为了安全把这个On改成Off可以隐藏header头里显示PHP信息。


三、MySQL优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    
#mysql5.5以后innodb优化的已经很好了,完全可以代替MyISAM了,所以下面主要针对Innodb引擎
[mysqld]
basedir = /data/soft/mysql
datadir = /data/soft/mysql/data
port = 3306
server_id = 1
socket = /tmp/mysqld.sock
default_storage_engine = InnoDB
log-bin = binlog
expire_logs_days = 14
max_binlog_size = 5G
binlog_cache_size = 10M
max_binlog_cache_size = 20M
slow_query_log
long_query_time = 1
slow_query_log_file = /data/soft/mysql/data/slow.log
open_files_limit = 65535
innodb = FORCE
#innoDB控制日志刷新频度,1是默认值,对于写入频率大并且对数据安全性没有那么苛刻,可以考虑设置成2,可以大幅度提高写入性能
innodb_flush_log_at_trx_commit = 1
innodb_buffer_pool_size = 5G
innodb_log_file_size = 1G
innodb_file_per_table = 1
innodb_flush_method = 0_DIRECT
#query_cache_size设置0的原因就是我这里写入的要求比较多,缓存的话反而性能降低
query_cache_type = 0
query_cache_size = 0
thread_cache_size = 64
table_definition_cache = 512
table_open_cache = 512
#设置连接最大数,可以设置大一点,但是不宜太大,可以避免造成瓶颈
max_connections = 2000
sort_buffer_size = 10M
#一次请求
max_allowed_packet = 6M
#sql_mode设置用于设置很多安全方面的东西,具体可以百度一下
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


上面只是觉得应该设置的地方,具体应该设置多少,还是应该根据实际环境,通过基准测试来确认。


       好了,就优化这些吧,现在速度已经明显上来了,这下老大应该满意了,赶紧去跟老大汇报情况:“老大,咱们都系统已经很稳定了,不出意外应该没问题”,老大立马回了一句:“意外是什么?”,“我靠,嘴贱”,心里暗骂,“那就是机器挂了,机房完蛋了,地球毁灭了”,我半开玩笑的说,这个时候老板来了,听到我刚说的话,接着说:”那怎么行,机器挂了,机房挂了就出问题,那我岂不是得天天烧香给机器还有机房,咱们做的可是跟钱打交道的,只要有人或者我就要服务一直在,我给你们提供足够的资金,把服务给我搞的天衣无缝!!“,老板说完,我不知道是该哭还是该笑,这时候说给钱了,早干嘛去了,唉~~,看来要项目要真正的成长起来真不容易,看来我又要继续”苦逼了“。

   欲知后事如何,请听下回分解……
阅读(1109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~