Title: Nginx 分段配置负载均衡[原创]

Categories: lua

Description: nginx

Keywords: linux,负载均衡,Nginx

Nginx 分段配置负载均衡

date:2017-11-06 author:zhangjianxin

[*] 解决方案

nginx config

```bash user nginx; worker_processes 5; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; worker_rlimit_nofile 1024;

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

    log_format  error '$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;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    include /etc/nginx/conf.d/upstream/*.conf;
    include /etc/nginx/conf.d/vhost/*.conf;
}

```

nginx upstream config

```bash

upstream post_servers {
    ip_hash;
    server dscn01:80 weight=5 backup;
    server dscn02:80 weight=10 max_fails=3 fail_timeout=30s;
}

```

nginx vhost config

server {
    listen       80;      #监听80端口
    access_log   logs/post.access.log main;  #使用main等级配置访问日志。
    error_log    logs/post.error.log error;          #使用error等级配置错误日志。

    set $post_servers_upstream   "post_servers";   #使用set指令配置upstream为:'post_servers'
    location / {  #将所有的请求代理到post_servers_upstream中。
      proxy_pass      http://$post_servers_upstream;
    }
  }