Squid的环境搭建测试


=Start=

缘由:

简单记录一下 Squid 代理服务的搭建过程,方便有需要的时候参考。

正文:

参考解答:

Squid 是什么?以及能做什么?

Squid Cache(简称为Squid)是一个支持HTTP,HTTPS,FTP等服务的代理服务软件。Squid用途广泛,可以作为缓存服务器(它可以通过缓存页面来提高服务器的响应速度并降低带宽占用),可以过滤流量帮助网络安全(它具有强大的访问控制功能),也可以作为代理服务器链中的一环,向上级代理转发数据或直接连接互联网。

使用 Squid 简单快速的搭建HTTP代理

用Squid搭建HTTP代理以及支持简单的Basic认证还是很快的。

以下内容主要引用自 使用Squid,简单快速的搭建HTTP代理 一文,简单明了:

  1. 安装 Squid
$ sudo yum install squid -y
  1. 修改配置文件
$ sudo vim /etc/squid/squid.conf
...
# 将 http_access deny all 中deny改为allow,还有就是 http_port 后面的是端口号,随便改成没被占用的端口号就行
http_access allow all
http_port 13128
...
  1. 启动/重启服务
# 查看状态
$ sudo systemctl status squid.service

# 启动
$ sudo systemctl start squid.service

# 重启
$ sudo systemctl restart squid.service

高匿代理:

在/etc/squid/squid.conf中添加

request_header_access Via deny all
request_header_access X-Forwarded-For deny all

OK 拉~

使用 Squid 快速的搭建带认证的HTTP代理服务

自己搭建的HTTP代理服务如果没有认证还是不太安全(容易被扫描到然后被滥用),这里简单记录一下如何配置带认证的HTTP代理服务:

1. 安装相关依赖(htpasswd命令在这里面)
$ sudo yum install -y httpd-tools

2. 创建账号并设置密码
$ sudo htpasswd -c /etc/squid/passwords squid_username_here #指定创建的用户名,输入回车之后再输入密码
$ sudo cat /etc/squid/passwords

3. 更新 Squid 的配置文件
$ sudo vim /etc/squid/squid.conf
...
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
# 在上面这一行提示之后添加自己的允许规则(实际测试basic认证的逻辑直接添加在最后是无效的,因为前面可能有allow all了)

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
...

4. 重启 Squid 服务
$ systemctl status squid.service
$ sudo systemctl start squid.service
$ sudo systemctl stop squid.service
$ sudo systemctl restart squid.service

一些其它内容

# 1. 检查 Squid 配置文件是否存在语法错误
# squid -k parse


# 2. 当连接命令报错“No route to host(没有到主机的路由)”时该怎么排查和处理?
# 没有到主机的路由这种问题很常见,绝大多数是由机器的防火墙没有关闭,少数情况是真的因为没有路由而不通

# 查看防火墙状态
service iptables status
firewall-cmd --state

# 关闭防火墙,注意有些机器防火墙是iptables
systemctl stop firewalld && systemctl disable firewalld 

# 或者开放特定端口,然后重新加载防火墙配置
firewall-cmd --add-port=9092/tcp --permanent
firewall-cmd --reload

# 首先检查一下服务器的iptables规则,然后先备份,再清空已有规则
$ sudo iptables --list
$ sudo iptables-save > iptables-save-$(date +%F).txt
$ sudo iptables -F
$ sudo iptables --list


# 3. 当使用 yum 命令安装时发现网络不通或是网速较慢时,更换成更合适的repo源
# 备份然后更新repo文件
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget https://mirrors.163.com/.help/CentOS7-Base-163.repo
sudo mv CentOS7-Base-163.repo /etc/yum.repos.d/

# 生成缓存
sudo yum clean all
sudo yum makecache

查看 Squid 日志

$ sudo ls -lt /var/log/squid/
$ sudo cat /var/log/squid/access.log
$ sudo tail -f /var/log/squid/access.log

使用 Squid 搭建 HTTPS 代理

这里只简单摘录一下网上搜到的看上去还比较靠谱的步骤说明。实际测试发现用 Squid 配置 HTTPS 代理还是没那么简单直接的,后面实测OK了我再整理一篇文章说明。

  • 生成一个CA证书给Squid使用 (Generate a CA Certificate to be used by Squid)
    • 配置Squid以支持功能 (Configure Squid to Peek-N-Slice SSL Connections)
  • 在浏览器中导入证书CA (Import Certificate CA into the Browser for Squid)
    • 检查Squid日志 (Check out Squid Logs)
  • 使用代理自动配置(PAC)文件进行代理设置 (Using a proxy auto-config (PAC) file to Specify Proxy Settings)
    • 安全连接代理 (Secure Proxy Connections)

首先需要准备证书,有三种方式:

  1. 自己签名
  2. 找机构购买(如阿里云)
  3. 使用acme.sh免费生成

我用的第三种,操作官网上写着,这里不多说了。

……

使用 Squid 搭建使用 LDAP 认证的 HTTP 代理

根据以后的实际需求情况再看是否进行补充

使用 Squid 搭建使用 Kerberos 认证的 HTTP 代理

根据以后的实际需求情况再看是否进行补充

参考链接:

使用Squid,简单快速的搭建HTTP代理
https://blog.kieng.cn/2717.html

Centos7下使用Squid快速搭建带认证的HTTP代理服务器
https://blog.phpgao.com/squid_proxy_with_basic_auth.html

使用Squid搭建HTTP代理服务器
https://bu1.github.io/2021/12/04/%E4%BD%BF%E7%94%A8Squid%E6%90%AD%E5%BB%BAHTTP%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8/

squid代理及常见的代理上网
https://www.cnblogs.com/ssgeek/p/12302135.html

玩转VPS之快速搭建HTTP代理
https://blog.phpgao.com/vps_tinyproxy.html

Squid test config file for syntax errors
https://www.cyberciti.biz/faq/squid-test-config-file-for-syntax-errors/

Squid极简搭建HTTP/HTTPS代理服务器
https://cooolin.com/scinet/2020/06/21/squid-proxy-simple.html

CentOS 7 + TinyProxy 快速搭建HTTP代理
https://www.bestyii.com/topic/126

How to install Squid forward proxy with TLS enabled
https://www.googlecloudcommunity.com/gc/Cloud-Product-Articles/How-to-install-Squid-forward-proxy-with-TLS-enabled/ta-p/478008
https://elatov.github.io/2019/01/using-squid-to-proxy-ssl-sites/

acme.sh 实现了 acme 协议, 可以从 letsencrypt 生成免费的证书
https://github.com/acmesh-official/acme.sh/wiki
https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

Chapter 3. Configuring the Squid caching proxy server
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/deploying_web_servers_and_reverse_proxies/configuring-the-squid-caching-proxy-server_deploying-web-servers-and-reverse-proxies

Chapter 16. Configuring the Squid Caching Proxy Server
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/configuring-the-squid-caching-proxy-server

Squid Log Files
https://wiki.squid-cache.org/SquidFaq/SquidLogs

Squid Web Cache wiki
https://wiki.squid-cache.org/Features/LogFormat

Squid configuration directive access_log
https://www.squid-cache.org/Doc/config/access_log/

Squid configuration directive logformat
https://www.squid-cache.org/Doc/config/logformat/

Squid 代理服务之传统代理服务器架构搭建
https://blog.csdn.net/shenyuanhaojie/article/details/121128443

Squid 缓存代理(原理 + 安装配置)
https://blog.csdn.net/shenyuanhaojie/article/details/121123525

squid代理及常见的代理上网
https://www.ssgeek.com/post/squid-dai-li-ji-chang-jian-de-dai-li-shang-wang/

squid代理与缓存(上)
https://www.cnblogs.com/ywb123/p/11395954.html

squid代理与缓存(下)
https://www.cnblogs.com/ywb123/p/11396059.html

代理服务器之 squid、lvs、nginx、haproxy之间的区别
https://www.cnblogs.com/ywb123/p/17514940.html

=END=


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注