nginx配置负载均衡

负载策略

  1. 权重轮询:wrr 【weight round-robin】

  2. ip_hash:

    nginx会根据客户端ip的hash结果选择一个真实服务器,而且每次都会固定访问这台服务器,可以解决session保存问题。

  3. least_conn:当前活跃连接数越小,权重越大,优先选择。

  4. 第三方负载策略,另查找资料

配置块

  1. weight
  2. max_fails
  3. fail_timeout
  4. down
  5. backup

nginx.conf

重点关注 upstream 块里边的内容 upstream server_group { }

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

#user nginx;
worker_processes auto;
error_log /usr/local/soft/nginx/logs/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /usr/local/soft/nginx/logs/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /usr/local/soft/nginx/conf/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /usr/local/soft/nginx/conf/conf.d/*.conf;

    # 定义一个服务器组
    upstream server_group {
        # 默认的轮询算法
        wrr;
        server 49.234.55.50:8080 weight=2 max_fails=3 fail_timeout=15;
        server 49.234.55.50:8081 down;
        server 49.234.55.50:8082 backup;
    }

    server {
        listen       80;
        server_name 159.75.79.151;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /usr/local/soft/nginx/conf/default.d/*.conf;

       location /load_balancing_test {
            proxy_http_version 1.1;
            proxy_pass http://server_group/;
            proxy_set_header   Host    $host:$server_port;
            proxy_set_header   X-Real-IP   $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size  1000M;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljt-tiger

thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值