阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx 负载均衡在金山逍遥网中的实际应用

136次阅读
没有评论

共计 7082 个字符,预计需要花费 18 分钟才能阅读完成。

在金山逍遥网中,前端负载均衡服务器采用的是 Nginx,两台 Nginx 服务器为一组,承担多种类型的负载均衡服务,两台负载均衡服务器均处于活动状态,各自绑定一个公网虚拟 IP,作为负载均衡服务器,当其中一个发生故障时,另一台接管发生故障服务器的虚拟 IP。配置 nginx.conf 代码如下

代码:

user www www;
work_processes 8;
error_log /data1/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;

#specifies the value for maximum file descriptors that can be opened by this process worker_rlimit_nofile 51200

events
{
    use epoll;
    worker_connections 51200;
}

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

    #charset utf-8

    server_names_hash_bucket_size 128k;
    client_header_buffer_size  32k;
    large_client_header_buffers 4 32k;

    sendfile on;
    #tcp_nopush on;

    keepalive_timeout 30;
    tcp_nodelay on;

    fastcgi_connect_timeout  300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size  128k;
    fastcgi_temp_file_write_size  128k;

    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;
    gzip_vary on;

    limit_zone anti_attack $binary_remote_addr 10m;

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

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

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

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

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

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

    #缓存
    proxy_temp_path /data2/proxy_temp_path;
    proxy_cache_path  /data2/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=5;

    upstream myserver_pool{
        server xx.xx.xx.1:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.2:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.3:80 weight=1 max_fails=2 fail_timeout=30s;
    }

    upstream php_server_pool{
        server xx.xx.xx.4:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.5:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.6:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.7:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.8:80 weight=1 max_fails=2 fail_timeout=30s;
    }

    upstream bbs_server_pool{
        ip=hash;
        server xx.xx.xx.9:80 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.10:80 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.11:80 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.12:80 max_fails=2 fail_timeout=30s;
    }

    upstream cms_server_pool{
        server xx.xx.xx.13:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.14:80 weight=1 max_fails=2 fail_timeout=30s;
    }

    upstream pic_server_pool{
        server xx.xx.xx.15:80 weight=1 max_fails=2 fail_timeout=30s;
        server xx.xx.xx.16:80 weight=1 max_fails=2 fail_timeout=30s;
    }

    upstream xoyohimsg_server_pool{
        server xx.xx.xx.17:3245;
        server xx.xx.xx.18:3245 down;
    }

    #xoyo.com 域名调转到 www.xoyo.com

    server
    {
        listen 80;
        server_name xiyo.com;

        rewrite ^/(.*) http:xoyo.com/ permanent;

        acces_log /data1/logs/xoyo.com_access.log;
    }

    #用户中心 HTTP/SSL 加密浏览
    server
    {
        listen 443;
        server_name my.xoyo.com

        ssl on;
        ssl_cretificate  my.xoyo.com.crt;
        ssl_cretificate_key my.xoyo.com.key;

    location /
    {
        proxy_pass http://php_server_pool;
        proxy_set_header Host my.xoyo.com;
        proxy_set_header X-Forward-For $remote_addr;
    }
    access_log /data1/logs/my.xoyo.com_access.log;

    }

    #图片服务器,不同的路径访问后端不同的服务器
    server
    {
        listen 80;
        server_name pic.xoyo.com;

        location /cms/
        {
        proxy_pass http://cms_server_pool;
        proxy_set_header Host pic.xoyo.com;
        proxy_set_header X-Forward-For $remote_addr;
        }
        access_log /data1/logs/pic.xoyo.com_access.log;
    }

    #音频电台文件下载,进行简单防盗链
    #limit_zone media %binary_remote_addr 10m;
    server
    {
        listen 80;
        server_name media.xoyo.com;

        location /
        {
        proxy_pass http://cms_server_pool;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;

        valid_reference nine blocked www.xoyo.com *.xoyo.com  www.kingsoft.com  *.kingsoft.com  www.kingsoft.cn  *.kingsoft.cn;
        if ($invalid_referers){
            rewrite ^/ http://www.xoyo.com;
        }
        }
        access_log /data1/logs/media.xoyo.com_access.log;
    }
    #“逍遥有聊”WebIM 产品的负载均衡,反向代理两种 HTTP 服务器
    server
    {
        listen  80;
        server_name hi.xoyo.com;
    #反向代理一款定制开发的高性能消息队列 HTTP 服务器
    location /recmessage.xoyo
    {
        proxy_pass http://xoyohimsg_server_pool;
        proxy_set_header Host $host;
    }

    location /
    {
        proxy_pass http://php_server_pool;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;

    }

    access_log /data1/logs/hi.xoyo.com_access.log;

    #论坛负载均衡并对图片、Flash、JavaScript、CSS、静态 HTML 进行 Web 缓存
    server {
    listen  80;
    server_name bbs.xoyo.com *.bbs.xoyo.com bbs.xoyo.kingsoft.com;

    location /
    {
        proxy_pass http://bbs_server_pool;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;

    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html|shtml)$
    {
        proxy_cache  cache_one;
        proxy_cache_valid  200 10m;
        proxy_cache_valid  304 1m;
        proxy_cache_valid  301 302 ih;
        proxy_cache_valid  any 1m;
        proxy_cache_key $host$uri$is_args$args;
        proxy_set_header  Host  $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_pass http://bbs_server_pool;

    }
    log_format bbs ‘$remote_addr $host  $remote_user [$time_local] “$request”‘
    ‘”$status” $body_bytes_sent “$http_referer”‘
    ‘”$http_user_agent”  “$http_x_forwarded_for”‘;
access_log /data1/logs/bbs.xoyo.com_access.log bbs;

}
    #论坛附件反向代理,限制下载速度为 256k/ 秒
    server{
        listen 80;
        server_name  att03.bbs.xoyo.com  att02.bbs.xoyo.com  att01.bbs.xoyo.com;

    location /{
        #限制下载速度为 256k/s
        limit_rate 256k;
        proxy_pass http://xx.xx.xx.19;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;
    }
    access_log off;
}

# 逍遥江湖 SNS 社���,管理后台定位到一台服务器上,并对图片,flash,javascript,CSS 进行 web 缓存

    server
    {
        listen  80;
        server_name  hu.xoyo.com  *.hu.xoyo.com;

        location /
        {

        proxy_pass http://php_server_pool;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;
        }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {
        proxy_cache  cache_one;
        proxy_cache_valid  200 10m;
        proxy_cache_valid  304 1m;
        proxy_cache_valid  301 302 ih;
        proxy_cache_valid  any 1m;
        proxy_cache_key $host$uri$is_args$args;
        proxy_set_header  Host  $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_pass http://php_server_pool;
    }

    location ~ ^/admincp.php
    {
        proxy_set_header  Host  $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_pass http://xx.xx.xx.4;

    }
    access_log /data1/logs/hu.xoyo.com_accsee.log;
    }
}

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

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

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

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-08/121858.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计7082字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中