Linux - Nginx的集群与负载均衡

本文介绍如何使用Nginx进行负载均衡配置,通过实例演示了如何为两台服务器分配图片请求流量,并确保高可用性。配置中包含了upstream、proxy_pass等关键指令的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx的集群与负载均衡

集群就是一群人干同样的活,负载均衡就是保证每个人都干得差不多。或者大人干得多一些,小孩干得少一些。

Nginx实现负载均衡很方便。

准备三台服务器,一台是用于访问图片(66)。另外是两台用于提供图片服务的集群(61,62)。

先准备三个logo.png图片。

66上如下:

422101-20180112143006816-2132045563.png

61上如下:

422101-20180112143012082-730233256.png

62上如下:

422101-20180112143016066-768180408.png

设置图片组66:

upstream imgserver {
        server 192.168.70.61:80 weight=1 max_fails=2 fail_timeout=30s;    
        server 192.168.70.62:80 weight=1 max_fails=2 fail_timeout=30s;    
    }

处理反向代理:

location ~ .*\.(jpg|jpeg|png|gif)$ {
        proxy_pass http://imgserver;
        proxy_set_header X-Forwarded-For $remote_addr;
}

下面是完整的配置。

#user  nobody;
user nginx nginx;           # 指定Nginx服务的用户和用户组
worker_processes auto;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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"';

    access_log  logs/access.log  main;
    server_tokens off;
    sendfile        on;
    #tcp_nopush     on;
    tcp_nodelay on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    send_timeout 30;
    gzip  on;
    
    upstream imgserver {
        server 192.168.70.61:80 weight=1 max_fails=2 fail_timeout=30s;    
        server 192.168.70.62:80 weight=1 max_fails=2 fail_timeout=30s;    
    }
    
    server {
        listen       80;
        server_name  localhost;

        charset UTF-8;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        set $root /var/webroot/tp5admin;        

        location / {
            root   $root;
            index  index.php index.html index.htm;
            if ( -f $request_filename) {
               break;
            }
            if ( !-e $request_filename) {
                rewrite ^(.*)$ /index.php/$1 last;
                break;
            }
        }

        location ~ .*\.(jpg|jpeg|png|gif)$ {
            proxy_pass http://imgserver;
            proxy_set_header X-Forwarded-For $remote_addr;
        }

        location ~ .*\.(js|css)$ {
            proxy_pass http://192.168.70.62:80;
            proxy_set_header X-Forwarded-For $remote_addr;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/webroot;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php(.*)$ {
            root           $root;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
            fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listen       81;
        server_name  localhost:81;
        set $root /var/webroot;
        location / {
            root   $root;
            index  index.php index.html index.htm;
            if ( -f $request_filename) {
               break;
            }
            if ( !-e $request_filename) {
                rewrite ^(.*)$ /index.php/$1 last;
                break;
            }
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/webroot;
        }
        
        location ~ .+\.php($|/) {
            root           $root;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param    SCRIPT_FILENAME    $root$fastcgi_script_name;
            include        fastcgi_params;
        }


    
    }


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

422101-20180112150351316-1453870041.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值