### 进入nginx编译安装目录

```bash
cd /root/software/
### 备份nginx编译安装目录###
cp -r nginx-1.12.2  BFnginx-1.12.2BF
###进入编译安装目录###
cd nginx-1.12.2/
### 添加 --with-stream TCP转发模块 ###
 ./configure  --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_gzip_static_module  --without-http_map_module --without-http_geo_module --with-http_flv_module --with-http_realip_module --with-pcre=/root/software/pcre-8.34 --with-zlib=/root/software/zlib-1.2.8 --with-http_ssl_module --with-openssl=/root/software/openssl-1.0.1c --add-module=/root/software/nginx_upstream_check_module-master --with-debug --with-stream
### 重新编译 ###
make
### 重新编译安装 ###
make install
### 进入正在使用的nginx目录 ##
cd /opt/nginx/sbin/
### 备份正在使用的nginx文件 ###
mv nginx nginx.bak
### 复制有TCP转发模块的nginx执行文件 ###
cp /root/software/nginx-1.12.2/objs/nginx /opt/nginx/sbin/
### 查看是否已经安装模块成功 ###
/opt/nginx/sbin/nginx -V
### 新建TCP转发配置文件夹 ###
mkdir /opt/nginx/conf/tcp.d

### stream模块放在http模块的下面 ###
### stream配置不能放到http内，即不能放到/opt/nginx/conf/conf.d/，因为stream是通过tcp层转发，而不是http转发 ###
# vim /opt/nginx/conf/nginx.conf
stream {

    log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

    #access_log /opt/nginx/logs/tcp-access.log proxy;
    open_log_file_cache off;

        # 统一放置，方便管理
        include /opt/nginx/conf/tcp.d/*.conf;
        }
        
### 新建tcp转发配置文件 ###
    upstream TCP8056 {
    #定义调度算法
    hash $remote_addr consistent;
    server 127.0.0.1:8056;
  }
    server {
    error_log       /opt/nginx/logs/TCP8056_error.log;
    access_log      /opt/nginx/logs/TCP8056.log proxy;
    listen 8056;
    ###连接超时时间###
    proxy_connect_timeout 1s;
    ###转发超时时间###
    proxy_timeout 3s;
    ###转发到具体服务器组###
    proxy_pass TCP8056;
    }
```







##### server支持的parameters如下：

```bash
weight=number     #设置权重，默认为1。
max_conns=number  #给当前server设置最大活动链接数，默认为0表示没有限制。
max_fails=number  #对后端服务器连续监测失败多少次就标记为不可用。
fail_timeout=time #对后端服务器的单次监测超时时间，默认为10秒。
backup            #设置为备份服务器，当所有服务器不可用时将重新启用次服务器。
down              #标记为down状态。
resolve           #当server定义的是主机名的时候，当A记录发生变化会自动应用新IP而不用重启Nginx。
```

```bash
### nginx日志配置 ###
参考文档:  http://www.ttlsa.com/linux/the-nginx-log-configuration/
### Nginx反向代理实现均衡负载及调度方法  ### 
参考文档:  https://www.cnblogs.com/struggle-1216/p/12050482.html
### Nginx支持TCP端口转发 ###
参考文档:  https://blog.51cto.com/u_14286115/5004492
### 编译安装nginx添加stream模块支持tcp（stream）代理 ###
参考文档:  https://blog.csdn.net/chouyi5916/article/details/100662446
### Nginx 配置TCP代理转发 ###
参考文档:  https://blog.csdn.net/jeikerxiao/article/details/87863341

```

