使用Docker搭建Squid代理服務器

title: Docker搭建代理服務器
tags:
    - Squid
categories:
    - Linux

[TOC]html

環境說明

項目 說明
系統 Deepin 15.5

步驟

  • 安裝Docker
  • 安裝Squid容器
  • 生成認證文件
  • 配置Squid服務器
  • 啓動Squid容器
  • 設置代理地址
  • 使用代理

安裝Docker

# 下載安裝腳本
$ curl -fsSL get.docker.com -o get-docker.sh

# 安裝 docker
$ sudo sh get-docker.sh

# 啓動 docker
$ sudo service docker start

安裝Squid容器

# 從docker hub下載容器
$ docker pull sameersbn/squid

# 在docker中建立容器
$ docker run --name squid -d --restart=always \
  --publish 3128:3128 \
  --volume /srv/docker/squid/cache:/var/spool/squid3 \
  sameersbn/squid

配置用戶名密碼認證

# 生成認證文件
$ sudo htpasswd squid_passwd your-username
## 在這裏輸入兩次密碼

# 將認證文件拷貝至容器
$ sudo docker cp squid_passwd squid:/etc/squid3/

Squid配置

配置文件大概以下git

acl localnet src 10.0.0.0/8    # RFC1918 possible internal network
acl localnet src 172.16.0.0/12    # RFC1918 possible internal network
acl localnet src 192.168.0.0/16    # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
acl localnet src 0.0.0.0/0.0.0.0
acl localnet src 0.0.0.0/8

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

# username&password auth config
auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid3/squid_passwd
acl ncsa_users proxy_auth REQUIRED
http_access allow ncsa_users


http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access deny to_localhost
http_access allow localnet
http_access allow localhost
http_access deny all
http_port 3128

cache_dir ufs /var/spool/squid3 100 16 256
coredump_dir /var/spool/squid3

refresh_pattern ^ftp:        1440    20%    10080
refresh_pattern ^gopher:    1440    0%    1440
refresh_pattern -i (/cgi-bin/|\?) 0    0%    0
refresh_pattern (Release|Packages(.gz)*)$      0       20%     2880
refresh_pattern .        0    20%    4320

將配置文件導入Squid容器github

$ sudo docker cp squid-simple.conf squid:/etc/squid3/squid.conf

配置文件生成說明docker

# 從Squid容器中導出默認配置文件
$ sudo docker cp squid:/etc/squid3/squid.conf ./

# 去掉註釋
$ sudo awk '/^[^#]/' squid.conf > squid-simple.conf

# 編輯配置文件
$ sudo vim squid-simple.conf
## 在這裏添加幾行
## acl localnet src 0.0.0.0/0.0.0.0
## acl localnet src 0.0.0.0/8
## auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid3/squid_passwd
## acl ncsa_users proxy_auth REQUIRED
## http_access allow ncsa_users

啓動或重啓容器

# 啓動容器
$ sudo docker start squid

# 重啓容器
$ sudo docker restart squid

使用代理

## 代理地址
# http://{your-username}:{your-password}@{your-ip OR domain-name}:3128
## 例如
# http://root:root@202.118.1.100:3128

## 在Linux終端中使用代理
export ftp_proxy=http://{your-username}:{your-password}@{your-ip OR domain-name}:3128
export http_proxy=http://{your-username}:{your-password}@{your-ip OR domain-name}:3128
export https_proxy=http://{your-username}:{your-password}@{your-ip OR domain-name}:3128
## 例如
export ftp_proxy=http://root:root@202.118.1.100:3128
export http_proxy=http://root:root@202.118.1.100:3128
export https_proxy=http://root:root@202.118.1.100:3128

演示

代理成功

bovenson@ThinkCentre:~/Tmp$ export http_proxy=http://***:***@***.***.***.***:3128
bovenson@ThinkCentre:~/Tmp$ export https_proxy=http://***:***@***.***.***.***:3128
bovenson@ThinkCentre:~/Tmp$ wget www.baidu.com
--2018-04-15 00:40:52--  http://www.baidu.com/
正在鏈接 ***.***.***.***:3128... 已鏈接。
已發出 Proxy 請求,正在等待迴應... 200 OK
長度:2381 (2.3K) [text/html]
正在保存至: 「index.html.1」

index.html.1                             100%[==================================================================================>]   2.33K  --.-KB/s  用時 0s      

2018-04-15 00:40:52 (333 MB/s) - 已保存 「index.html.1」 [2381/2381])

認證失敗

bovenson@ThinkCentre:~/Tmp$ export http_proxy=http://***.***.***.***:3128
bovenson@ThinkCentre:~/Tmp$ export https_proxy=http://***.***.***.***:3128
bovenson@ThinkCentre:~/Tmp$ wget www.so.com
--2018-04-15 00:42:01--  http://www.so.com/
正在鏈接 ***.***.***.***:3128... 已鏈接。
已發出 Proxy 請求,正在等待迴應... 407 Proxy Authentication Required
2018-04-15 00:42:01 錯誤 407:Proxy Authentication Required。

參考

相關文章
相關標籤/搜索