当前位置:   article > 正文

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

nginx 向 squid 转发请求

场景描述

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

解决方案

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

nginx部分

nginx支持TCP转发

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

  1. stream {
  2. ## TCP 代理日志格式定义
  3. log_format tcp_proxy '$remote_addr [$time_local] '
  4. '$protocol $status $bytes_sent $bytes_received '
  5. '$session_time "$upstream_addr" '
  6. '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
  7. ## TCP 代理日志配置
  8. access_log logs/tcp-access.log tcp_proxy;
  9. open_log_file_cache off;
  10. ## TCP 代理配置
  11. server {
  12. listen 172.17.4.80:8091; #监听本机地址和端口,当使用keeplived的情况下使用keeplived VIP
  13. proxy_connect_timeout 1s;
  14. proxy_timeout 3s;
  15. proxy_pass 172.17.9.223:3128; #这里填写对端的地址
  16. }
  17. }

squid配置

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

  1. acl localnet src 172.17.9.0/24 # RFC1918 possible internal network
  2. acl localnet src 172.17.4.0/24
  3. acl localnet src 55.66.8.0/24
  4. acl SSL_ports port 443
  5. acl Safe_ports port 80 # http
  6. acl Safe_ports port 21 # ftp
  7. acl Safe_ports port 443 # https
  8. acl Safe_ports port 70 # gopher
  9. acl Safe_ports port 210 # wais
  10. acl Safe_ports port 1025-65535 # unregistered ports
  11. acl Safe_ports port 280 # http-mgmt
  12. acl Safe_ports port 488 # gss-http
  13. acl Safe_ports port 591 # filemaker
  14. acl Safe_ports port 777 # multiling http
  15. acl CONNECT method CONNECT
  16. http_access deny !Safe_ports
  17. http_access deny CONNECT !SSL_ports
  18. http_access allow localhost manager
  19. http_access deny manager
  20. http_access allow localnet
  21. http_access allow localhost
  22. http_access allow all #允许所有的数据包通过
  23. http_port 3128
  24. coredump_dir /usr/local/squid/var/cache/squid
  25. refresh_pattern ^ftp: 1440 20% 10080
  26. refresh_pattern ^gopher: 1440 0% 1440
  27. refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
  28. refresh_pattern . 0 20% 4320
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号