linux nginx无证书情况用stream模块反向代理 https网站(部分服务器无法连接外网通过nginx反向代理方式)

公司研发一般在内网环境下,但是开发时需要调用某些第三方接口。

这时可以用一台服务器做nginx反向代理,然后研发机器修改host文件将域名指向服务器即可实现代理转发。

但是普通的nginx http反向代理代理https时需要配置证书,我们不可能有第三方接口域名的证书,所以要使用nginx 的stream模块。

普通的nginx反向代理时第七层代理,而stream模块是第四层代理,转发的tcp/ip协议,所以不需要证书。

stream模块要nginx 1.9.0后才开始支持,目前nginx-1.15.10默认已经包含此模块。

但是要实现代理多个接口,需要先解包,分析tcp包中的域名等信息,才能分发请求,所以还要用到ngx_stream_ssl_preread_module模块,这个模块官方的发布包里面没有包含,需要自行编译。
编译完成后,配置文件比较简单,根据$ssl_preread_server_name参数映射到不同的upstream就可以了

stream {
    
     log_format proxy '$proxy_protocol_addr $remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

     access_log logs/tcp-access.log proxy ;
     open_log_file_cache off;

    map $ssl_preread_server_name $backend_pool {
        api.weixin.qq.com server_weixin;
        pay.abchina.com  server_abc;
        ocr.tencentcloudapi.com server_ocr;
    }

    upstream server_weixin{
        server api.weixin.qq.com:443;
    }
    upstream server_abc{
        server pay.abchina.com:443;
    }
    upstream server_ocr{
        server  ocr.tencentcloudapi.com:443;
    }
    server {
        listen 443;
        ssl_preread on;
        proxy_pass $backend_pool;
        proxy_connect_timeout 15s;
        proxy_timeout 15s;
        proxy_next_upstream_timeout 15s;
    }

}
stream {
    resolver 114.114.114.114;
    error_log logs/error-proxy.log error;
    server {
        listen 80;
        ssl_preread on;
        proxy_connect_timeout 6000s;
        proxy_pass www.baidu.com:$server_port;
    }
    server {
        listen 443;
        ssl_preread on;
        proxy_connect_timeout 6000s;
        proxy_pass www.baidu.com:$server_port;
    }
}

参考:
https://blog.csdn.net/gggauss/article/details/89140446
https://renwoxing.blog.csdn.net/article/details/111504486?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-2.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-2.no_search_link

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值