Ubuntu安装squid并启用SSL

发布时间:2017/4/1 21:41:20 打印 字号:

由于公司网络限制,无法访问外网某些网站。为了突破限制,需要在外网服务器上安装一个 squid 作为代理,同时为了安全,启用 SSL。

安装openssl

首先需要安装 openssl 及相关的依赖。

apt-get install openssl
apt-get install libssl-dev
apt-get install ssl-cert

安装squid

直接使用 apt 安装的 squid 没有启用 SSL,所以需要使用源代码编译安装。

apt-get source squid
apt-get build-dep squid
apt-get install devscripts build-essential fakeroot

# squid 版本为 3.3.8
cd squid3-3.3.8

# 修改编译参数
vim debian/rules

# 在 DEB_CONFIGURE_EXTRA_FLAGS 配置中添加
# 注意 with-open-ssl 的值为实际 openssl.cnf 所在路径
--enable-ssl \
--with-open-ssl=/usr/lib/ssl/openssl.cnf \

# 编译 squid
./configure
debuild -us -uc -b

# 安装
cd ..
apt-get install squid-langpack
dpkg -i squid3-common_3.3.8-1ubuntu6.2_all.deb
dpkg -i squid3_3.3.8-1ubuntu6.2_amd64.deb

生成 SSL 证书

去Gworg申请SSL证书:https://shop151549897.taobao.com/

配置 squid

将配置文件修改成如下内容。我这里没有启用身份验证,如果需要可以自行添加。squid 端口号修改为 9999,也可以按需修改。

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 localhost
http_access allow all

# 修改为证书路径
https_port 9999 cert=/ssl/ssl.crt key=/ssl/ssl.private.key

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

via off
forwarded_for delete
dns_v4_first on

需要注意的是,ssl.private.key 是证书的私钥,不需要密码保护,不然 squid 无法启动。可以使用下列命令生成无密码保护的私钥:

openssl rsa -in ssl.key -out ssl.private.key

配置防火墙

我使用的是 ufw,所以运行

ufw allow 9999

重启 squid 就可以正常使用了。

禁用 squid 升级

由于我们是使用源代码编译的 squid,所以升级会破坏配置,需要禁用 squid 升级。

sudo apt-mark hold squid3