nginx+squid實現跨越兩層防火牆的正向代理

場景描述

應用服務器部署在內網中,須要和外網進行交互,可是中間間隔了兩個區域,一個區域是業務子區,這個區域不能直接和互聯網進行通訊,另外一個區域是DMZ區域能夠和互聯網進行通訊,所以咱們要和互聯網進行通訊中間隔了兩層網絡。nginx

解決方案

在業務子區放一臺服務器安裝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是一個專門的正向代理軟件其功能比較強大,能夠限制網段和端口的出訪,其具體配置以下:session

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
相關文章
相關標籤/搜索