使用nginx负载均衡应对爱奇艺广告带来的超高流量访问

来源:互联网 发布:中国广电网络集团公司 编辑:程序博客网 时间:2024/05/16 04:20

PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,对于PHP 5.3.3之前的php来说,是一个补丁包[1]  ,旨在将FastCGI进程管理整合进PHP包中。如果你使用的是PHP5.3.3之前的PHP的话,就必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。相对Spawn-FCGI,PHP-FPM在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab进行监控,而PHP-FPM则没有这种烦恼。

一、爱奇艺广告前后的UV、PV

日期
浏览次数
独立访客
2017/7/31
6162
2708
2017/8/1
6084
2553
2017/8/2
6090
2660
2017/8/3
5992
2649
2017/8/4
6486
2659
2017/8/5
6282
2744
2017/8/6
6489
2473
2017/8/7
250642
167764
2017/8/8
626036
372079
2017/8/9
616853
366212
2017/8/10
471643
287063
2017/8/11
643394
400759
2017/8/12
708046
449825
2017/8/13
666541
424064
2017/8/14
459397
292093
2017/8/15
374168
141648
2017/8/16
371885
239105
2017/8/17
370876
242501
2017/8/18
495082
314805
2017/8/19
786818
468510
2017/8/20
766654
455550
2017/8/21
795773
460902
2017/8/22
878806
488432
2017/8/23
830644
468297
2017/8/24
839879
435866
2017/8/25
1027199
518090
2017/8/26
871558
469939
2017/8/27
846688
442265
2017/8/28
676614
379318

访问趋势数据可以看出,从爱奇艺广告时间8月7日开始,访问量是前一天的38倍。
我们是8月11日更换网线以支持千兆网络的,可以看出8月11日访问量增加了1.36倍。
在2017-8-22日凌晨3点13分将域名从我们的Windows Server 2012切换到Centos7。
可以看出得益于CENTOS的稳定性和NGINX的高并发支持,相比前一天访问量增加了1.104倍。

二、部署方案

使用3台机做集群,一台负责接入分发的负载均衡,另外两个做基于PHP-FPM的应用集群。

由于我们本身已有MySQL集群,直接将应用的数据库放到我们现有的MySQL集群中。


三、nginx负载均衡机配置

#放入HTTP配置段用于限流

limit_conn_zone $server_name zone=mobile:10m;

upstream ***mobile{
 server 1.1.1.11:8098 weight=1;
 server 1.1.1.12:8098 weight=1;
}
server{
 listen 81;
 server_name *.**.cn;
 charset utf-8;
 access_log /var/log/nginx/***mobile.access.logmain;

 location/{
  proxy_pass http://***mobile;
  limit_conn mobile 300;
  limit_rate 20k;
 }
 
}

四、nginx应用配置

upstream "sitemobile" {
 server unix:/run/php-cgi.sock weight=1;
}
#
server {
 listen 8098;
 server_name *.***.**;
 charset utf-8;
 access_log  /var/log/nginx/sitemobile.access.log  main;
 index index.php index.htm index.html;
 root /var/www/sitemobile;
 location / {
     try_files $uri $uri/ /index.php?$args;
 }
 location ~ .+\.php($|/) {
 
     fastcgi_pass sitemobile;
     fastcgi_index index.php;
 
     #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量,
     #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置
     fastcgi_split_path_info  ^(.+\.php)(/.*)$;
     fastcgi_param  PATH_INFO $fastcgi_path_info;
 
     #加载Nginx默认"服务器环境变量"配置
     include        fcgi.conf;
 }
 
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 {
     expires 5d;
 }
 location ~ .*\.(js|css)?$
 {
     expires 12h;
 }
 
 error_page   500 502 503 504  /50x.html;
 location = /50x.html {
     root   html;
 }
}