Nginx负载均衡在新浪播客中的实际应用


2008年的新浪播客由静态服务器集群和动态服务器集群两部分组成,静态服务器集群采用Squid做前端缓存,动态服务器也称接口服务器,主要用来实现显示播放数,记录播放日志等等。

接口服务器上采用F5 BIG-IP硬件四/七层负载均衡交换机,对4台Nginx反向代理服务器进行四层负载均衡,由这四台nginx服务器判断 URL,进行分组,对后端3组web服务器进行七层负载均衡。

F5 BIG-IP后端的3组web服务器,配置不一样,第一组内存密集型,技术主要是PHP+Mencache服务; 第2组为 CPU密集型服务,主要消耗CP资源;第三组为磁盘密集型,为记录日志等操作,要求大空间。

以下代码是接口服务器Nginx负载均衡配置;

/***代码是N年前新浪接口服务器的nginx负载均衡配置,提供按URL分组服务,负载均衡服务***/

usr www www;
worker_processes 10;
error_log /data1/logs/nginx_error.log crit;
pid  /tmp/nginx.pid
worker_rlimt_nofile 51200;

events
{
    use epull;

    worker_connections 51200;
}

http
{
    include  conf/mine.types;
    default_type application/octet-stream;

    charset gb2312;

    server_name_hash_bucket_size 128;

    keeplaive_timeout 15;

    sendfile on;
    tcp_mopush on;
    tcp_nodelay on;
    #第一组接口机:Memcache相关(点击数)
    upstream count.interface.video.sian.com.cn{
        server xx.xx.xx.55:80;
        server xx.xx.xx.58:80;
        server xx.xx.xx.47:80;
    }
    #第二组接口机:提供数据类程序
    upstream data.interface.video.sian.com.cn{
        server xx.xx.xx.59:80;
        server xx.xx.xx.64:80;
        server xx.xx.xx.48:80;
    }
    #第三组接口机:打日志类程序,功能相关,嵌套页面
    upstream log.interface.video.sian.com.cn{
        server xx.xx.xx.72:80;
        server xx.xx.xx.49:80;
    }
}
server
{
    listen 80;
    server_name interface.video.sian.com.cn;

    location / {
    proxy_redirect off;

    #后端的web服务器可以直接通过X-Forward-For 获取用户真实ip
    proxy_set_header X-Forward-For $remote_adr;

    #按URL进行分组,第一组:Memcache相关(点击数)
    if ($request_uri ~ "^\/app\/count\/")
    {
        proxy_pass http://count.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/app\/online\/")
    {
        proxy_pass http://count.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/user\/getLoginGap.php")
    {
        proxy_pass http://count.interface.video.sian.com.cn;
    }
    #按URL进行分组,第二组:外部提供数据类程序
    if ($request_uri ~ "^\/crossdomain.xml")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/client\/topVideoClient.php")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/common\/")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/randplay\/randplay.php")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/topic\/suggTop.php")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/uploadClient\/")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/xml\/")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/outinterface\/")
    {
        proxy_pass http://data.interface.video.sian.com.cn;
    }
    #按URL进行分组,第三组:打日志类程序
    if ($request_uri ~ "^\/interface\/flash\/")
    {
        proxy_pass http://log.interface.video.sian.com.cn;
    }
    if ($request_uri ~ "^\/interface\/playrank\/playrank2008_10.php")
    {
        proxy_pass http://log.interface.video.sian.com.cn;
    }
    #按URL进行分组,其他组:功能相关,嵌套页面等未匹配到的URL
    proxy_pass  http://log.interface.video.sina.com.cn;
}
#定义日至格式
log_format count '$remote_addr - $remote_user [$time_local] $request'
    '"$status" $body_bytes_sent "$http_referer"'
    '"$http_user_agent"  "$http_x_forwarded_for"';
#打日志
access_log /data1/logs/interface.log count;

#允许客户端请求的最大单文件字节数
client_max_body_size 10m;

#缓冲区代理缓冲用户端的最大字节数 可以理解为现存到本地再传给用户
client_body_size 128k;

#跟后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_time 600;

#连接成功后_等待后端服务器响应时间_其实已经进入后端的派对等候处理
proxy_read_timeout  600;

#后端回传时间_规定时间内传完所有数据
proxy_send_timeout  600;

#代理请求缓存区,保存用户的头信息以供Nginx进行规则处理
proxy_buffer_size  8k;
proxy_buffers  4  32k;
proxy_busy_buffers_size  64k;
proxy_temp_file_write_size 64k;
}
}

更多Nginx相关教程见以下内容

CentOS 6.2实战部署Nginx+MySQL+PHP

使用Nginx搭建WEB服务器

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

CentOS 6.3下Nginx性能调优

CentOS 6.3下配置Nginx加载ngx_pagespeed模块

CentOS 6.4安装配置Nginx+Pcre+php-fpm

Nginx安装配置使用详细笔记

Nginx日志过滤 使用ngx_log_if不记录特定日志

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址

相关内容