nginx内网代理TCP端口

环境,电信云:CentOS6.8 64位
目的:增加内网nginx tcp代理

1.准备工作

如果通了外网可以通过指令下载压缩包:包位置一般放于/usr/local/src

wget http://nginx.org/download/nginx-1.14.2.tar.gz
wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.43.tar.gz

实际这是内网所以需要手动下载上述包放入/usr/local/src目录下

  • 安装c++编译环境
yum install gcc-c++
2.安装组件
  • openssl安装
tar zxvf openssl-fips-2.0.10.tar.gz
cd openssl-fips-2.0.10
./config && make && make install
  • pcre安装
tar zxvf pcre-8.43.tar.gz
cd pcre-8.43
./configure && make && make install
  • zlib安装
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure && make && make install
  • nginx安装
tar zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --with-stream --prefix=/usr/local/nginx && make && make install
3.启动nginx
whereis nginx
cd /usr/local/nginx/sbin/
./nginx

启动nginx,发现报错:
error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
64位系统执行:

ln -s /usr/local/lib/libpcre.so.1 /lib64
cd /usr/local/nginx/sbin/
./nginx
ps -aux | grep nginx

发现nginx已经启动起来了
修改配置文件:cd /usr/local/nginx/conf/nginx.conf 增加如下配置(和http同级)

stream {
    upstream tcp_29900 {
        hash $remote_addr consistent;
        server 192.168.0.247:29900 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 29900;
        proxy_connect_timeout 10s;
        proxy_timeout 60s;
        proxy_pass tcp_29900;
    }
    upstream tcp_29903{
        hash $remote_addr consistent;
        server 192.168.0.247:29903 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 29903;
        proxy_connect_timeout 10s;
        proxy_timeout 60s;
        proxy_pass tcp_29903;
    }
}

执行指令查看端口是否监听成功:

netstat -nap|grep 29900
netstat -nap|grep 29903
4.防火墙处理
  • 可关闭linux自带防火墙
service iptables stop
chkconfig iptables off
chkconfig --list|grep ipt
  • 可添加例外-方式1
#修改文件设置白名单
vim /etc/sysconfig/iptables
#增加下面一行代码
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 29900 -j ACCEPT
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 29903 -j ACCEPT
  • 可添加例外-方式2
/sbin/iptables -I INPUT -p tcp --dport 29900 -j ACCEPT #开启29900 端口 
/sbin/iptables -I INPUT -p tcp --dport 29903 -j ACCEPT #开启29903 端口 
/etc/rc.d/init.d/iptables save #保存配置 
/etc/rc.d/init.d/iptables restart #重启服务 
/etc/init.d/iptables status #查看已开放端口 
#保存退出,重启防火墙
service iptables restart

最后需要修改电信云该实例的安全规则

你可能感兴趣的:(nginx内网代理TCP端口)