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 [email protected]
[email protected]'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
发布了141 篇原创文章 · 获赞 107 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/ternence_hsu/article/details/100939787