nginx+squid实现跨越两层的正向代理,


场景描述

应用服务器部署在内网中,需要和外网进行交互,但是中间间隔了两个区域,一个区域是业务子区,这个区域不能直接和互联网进行通信,另一个区域是DMZ区域可以和互联网进行通信,因此我们要和互联网进行通信中间隔了两层网络。

解决方案

在业务子区放一台服务器安装nginx,并安装插件使其可以实现TCP的转发,然后DMZ区域放一台服务器安装squid实现正向代理。

nginx部分

nginx支持TCP转发

我们的目的就是将应用层的数据转发到squid,实际上转发的数据使用的是TCP协议,nginx从1.9之后开始支持转发TCP协议,负责TCP转发的模块为stream,stream默认不安装的,需要手动添加参数:–with-stream
nginx TCP代理模块配置文件如下

stream {
    ## TCP 代理日志格式定义
    log_format tcp_proxy '$remote_addr [$time_local] '
                         '$protocol $status $bytes_sent $bytes_received '
                         '$session_time "$upstream_addr" '
                         '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    ## TCP 代理日志配置
    access_log logs/tcp-access.log tcp_proxy;
    open_log_file_cache off;

    ## TCP 代理配置
    server {
       listen 172.17.4.80:8091; #监听本机地址和端口,当使用keeplived的情况下使用keeplived VIP
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass 172.17.9.223:3128; #这里填写对端的地址
    }
}

squid配置

squid是一个专门的正向代理软件其功能比较强大,可以限制网段和端口的出访,其具体配置如下:

acl localnet src 172.17.9.0/24    # RFC1918 possible internal network
acl localnet src 172.17.4.0/24
acl localnet src 55.66.8.0/24

acl SSL_ports port 443
acl Safe_ports port 80        # http
acl Safe_ports port 21        # ftp
acl Safe_ports port 443        # https
acl Safe_ports port 70        # gopher
acl Safe_ports port 210        # wais
acl Safe_ports port 1025-65535    # unregistered ports
acl Safe_ports port 280        # http-mgmt
acl Safe_ports port 488        # gss-http
acl Safe_ports port 591        # filemaker
acl Safe_ports port 777        # multiling http
acl CONNECT method CONNECT
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access allow all #允许所有的数据包通过
http_port 3128
coredump_dir /usr/local/squid/var/cache/squid
refresh_pattern ^ftp:        1440    20%    10080
refresh_pattern ^gopher:    1440    0%    1440
refresh_pattern -i (/cgi-bin/|\?) 0    0%    0
refresh_pattern .        0    20%    4320

相关内容