Nginx 代理Http及TCP请求

0x00 背景

    三台机器

                Server: 内网环境,只能访问内网,外网无法访问   192.168.1.200

                ClientA:只能访问外网,没有内网权限   192.168.1.212

                ClientB :既能访问外网,也能访问内网,且能在公网上找到某端口  192.168.1.211

    ClientA若想访问Server,则可以通过ClientB搭建反向代理,代理Server的端口,则可实现ClientA访问Server

0x01 准备

    1. Linux服务器(需源码编译安装)或Windows服务器

    2. Nginx软件,官网:http://nginx.org/en/download.html,注:只下载1.9以上的版本,1.9之后才支持TCP代理

    3. nc 用于测试TPC代理(若有其它测试方式,可忽略)

0x02 安装Nginx(只针对Linux,Windows图形界面安装即可)

    1. 创建Nginx源码存放目录  

mkdir /home/${user}/soft     //${user} 为用户目录
mkdir /home/${user}/soft/nginx  //${user} 为用户目录

    2. 将从官网下载的Nginx源码包传到目录下 

                目录:  /home/${user}/soft/nginx           ${user} 为用户目录 

    3. 解压源码文件

cd /home/${user}/soft/nginx               ${user} 为用户目录 
tar -xzvf nginx-1.12.2.tar.gz             nginx-1.12.2.tar.gz 为Nginx源码包的名称,可变

    4. 切换到Nginx源码目录

cd /home/htdf02/Soft/nginx/nginx-1.12.2    

    5. 编译前准备,安装编译所需软件

yum install gcc gcc-c++ openssl_devel 
yum install openssl openssl-devel
yum install pcre-devel
yum install zlib-devel

    6. 开始编译安装 

        nginx1.90对TCP协议的代理并不是默认开启的,需要在编译的时候配置 --with-stream 参数

./configure --prefix=/opt/nginx  --with-stream          
make
make install 

    7.  安装完成,配置TCP反向代理

cd /opt/nginx/conf/              切换到安装的Nginx配置目录下
vim nginx.conf                   编辑Nginx配置文件

       在Nginx配置文件(nginx.conf)中最下面添加配置 ,并保存退出

            与配置文件中的server并列

stream {
    upstream proxy_card {
        # simple round-robin  转发IP和端口
        server 192.168.1.200:9092;
        #check interval=3000 rise=2 fall=5 timeout=1000;
        #check interval=3000 rise=2 fall=5timeout=1000
        #check interval=3000 rise=2 fall=5timeout=1000
        #check_http_send "GET /HTTP/1.0\r\n\r\n";
        #check_http_expect_alive http_2xxhttp_3xx;
    }
    server {
        listen 12340; #监听端口
        proxy_pass proxy_card;  #转发请求
    }
}

 

        8. 配置Http反向代理

             在nginx.conf添加配置, 并保存退出


    server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        ############ 反向代理配置  其它配置默认 ###################
        location /wechat {
            proxy_pass http://localhost:8087/wechat;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
        }


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    9. 启动Nginx

cd /opt/nginx/sbin                           切换到Nginx启动程序目录下
./nginx                                      启动Nginx

 

0x03 代理测试(可使用其它方式进行测试)

    1. HTTP代理测试,打开浏览器打开ClientB的HTTP端口即可

    2. TCP代理测试

        ① 在Server、ClientA端安装NC

yum install nc

       ② 在Server端运行端口监听 

nc -lk 9092    监听9092端口

        ③ 在ClinetA上发送TCP请求到ClinetB端的暴露的端口

nc 192.168.1.211 12340
输入要传输的数据

       ④ Server 上可接收到ClientA的数据

0x04 服务器搭建完成

注意: 

     1. 编译过程中报错,报错内容: 

C compiler cc is not found

       是因为没有安装gcc引起的

    2. 编译过程中报错,报错内容:

./configure: error: the HTTP rewrite module requires the PCRE library.

        是由于没有正确安装依赖引起的,执行以下命令可解决: 

yum install gcc gcc-c++ openssl_devel 
yum install pcre-devel openssl openssl-devel

     3. 编译过程中报错,报错内容: 

error: the HTTP rewrite module requires the PCRE library

         需要安装依赖

yum install pcre-devel

 

转载于:https://my.oschina.net/jutao/blog/1576955

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值