Nginx四层代理配置

作者: admin 分类: Linux 发布时间: 2018-05-21 23:06

Nginx 从1.9.0以后版本支持四层代理,如果做四层代理需要在编译时添加 –with-stream模块

1、获取nginx源码包

[root@localhost ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz

2、解压

[root@localhost data]# tar -xf nginx-1.22.0.tar.gz

3、编译安装

[root@localhost data]# cd nginx-1.22.0/
[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-stream --without-http_rewrite_module
[root@localhost nginx-1.22.0]# make -j 2 && make install

4、nginx配置文件

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
user  nginx;
worker_processes  auto;



stream {                    # stram模块,和http模块是同级别(四层代理时需要添加上这个模块);
      server {
          listen 88;        # 监听88端口;
          proxy_pass qunniao_server;  
      }
      upstream qunniao_server{         # 定义qunniao_server ;
          server 127.0.0.1:81 ;
          server 127.0.0.1:82 ;
      }
}

5、添加nginx至systemctl

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

6、启动

[root@localhost conf]# systemctl enable nginx --now

7、测试

[root@localhost conf]# netstat -antpl | grep 88
tcp 0 0 0.0.0.0:88 0.0.0.0:* LISTEN 104147/nginx: maste


温馨提示:如无特殊说明,本站文章均为作者原创,转载请注明出处!

发表回复