jiftle

# Nginx配置TCP服务负载均衡


## 前置条件

- nginx 1.18 (1.9之后支持tcp负载)
- 两个前置服务


## 配置文件修改

Legacy
Stable

```
# cat nginx.conf
user  nginx;
# 增加工作线程
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    # worker_connections  1024;
    # 工作者连接
    worker_connections 8192;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

worker_rlimit_nofile 40000;

stream {
    upstream appkey01 {
        least_conn;
        # server 192.168.1.14:9905 max_fails=3 fail_timeout=5s;
        server 192.168.1.192:9905 max_fails=3 fail_timeout=5s;
    }

    server {
        listen 9905;
        proxy_pass appkey01;
    }
}
```

分类:

技术点:

相关文章: