nginx四层网络代理实现

1.下载源码

wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -xzf nginx-1.14.0.tar.gz
cd nginx-1.14.0

2.编译环境

Debian 环境需要安装的软件包

apt install gcc make
apt install libpcre3 libpcre3-dev  //【正则表达式库】 官网http://www.pcre.org/
apt install openssl libssl-dev     //【openssl库】 官网https://www.openssl.org/
apt install zlib1g-dev

3.nginx的编译与安装

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-stream
make && make install

4.配置nginx配置文件

vim /usr/local/nginx/conf/nginx.conf
配置是ip和端口是需要代理的地址端口,listen的端口是提供给外部用来访问的端口

worker_processes  1;
events {
    worker_connections  1024;
}
stream {  
        upstream tcp_proxy {
        hash $remote_addr consistent;  #远程地址做个hash
        server 192.168.230.131:22;
   }
      server {
        listen 2222;
        proxy_connect_timeout 1s;
        proxy_timeout 36000s;  #后端连接超时时间
        proxy_pass tcp_proxy;
     }
  }

5.启动nginx服务

/usr/local/nginx/sbin/nginx

6.测试nginx

查看端口映射

netstat -ntpl|grep 2222
tcp        0      0 0.0.0.0:2222            0.0.0.0:*               LISTEN      13661/nginx: master 

测试远程ssh访问

ssh -p 2222 th@10.95.42.136
th@10.95.42.136's password: 
Last login: Sat Apr  8 22:32:14 2017 from linux-node1
[root@linux-node2 ~]# ls
test.txt

7.过程中遇到的错误:找不到www用户

错误日志

./sbin/nginx 
nginx: [emerg] getpwnam("www") failed

解决方法,添加www用户

groupadd -f www
useradd -g www www
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值