zkdog/docs/Squid代理服务器.md

263 lines
8.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 在 Ubuntu 22.04 | 20.04 | 安装和配置 Squid 代理服务器
Squid 是一个代理和缓存服务器。它充当代理,将请求转发到所需的目的地,但也保存请求的内容。如果另一台服务器在鱿鱼服务器中尚未过期之前请求相同的信息,它将向请求者提供相同的内容,从而提高下载速度并节省带宽。
---
Squid 代理服务器支持缓存通过不同协议请求的内容例如http、SSL 请求、DNS 查找和FTP。本指南介绍如何在 Ubuntu 22.04|20.04 上安装和配置 Squid 代理
## **在 Ubuntu 22.04|20.04 上安装 Squid 代理服务器**
首先,更新您的系统软件包。请注意,对于所有命令,我将以 root 用户身份运行它们
```sql
sudo apt update && sudo apt upgrade -y
```
检查升级成功后是否需要重启系统。
```
[ -e /var/run/reboot-required ] && sudo reboot
```
接下来,在 Ubuntu 上安装鱿鱼代理。在 Ubuntu 中安装 Squid 代理很容易,因为它已经在 Ubuntu 20 存储库中可用。使用以下命令确认这一点。
```
sudo apt-cache policy squid
```
输出
![https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/How-to-check-if-Squid-proxy-is-avaiable-in-Ubuntu-for-installation.png](https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/How-to-check-if-Squid-proxy-is-avaiable-in-Ubuntu-for-installation.png)
要安装 Squid 代理,请运行以下命令。还可以在系统启动时启动然后检查状态
```
sudo apt install -y squid
sudo systemctl start squid
sudo systemctl enable squid
sudo systemctl status squid
```
如果鱿鱼正确安装并运行,您应该输出如下所示:
![https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/How-to-check-if-Squid-Proxy-is-running.png](https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/How-to-check-if-Squid-Proxy-is-running.png)
## **在 Ubuntu 22.04|20.04 上配置 Squid 代理服务器**
默认的 Squid 代理配置文件位于 */etc/squid/squid.conf 中。* 该文件已经有许多至少可以工作的设置,但我们可以修改以适应我们的偏好。首先,创建原始文件的备份。
```powershell
sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.orig
```
现在,在*/etc/squid/squid.conf*中进行自定义设置。使用您喜欢的文件编辑器打开文件
```
sudo vim /etc/squid/squid.conf
```
### **1.更改squid默认端口**
默认的 Squid tcp 端口是 3128。要更改此端口请打开 squid.conf 文件并查找 *http_port* 行。更改为您所需的端口号并保存文件。
```powershell
#http_port 3128
http_port 8080
```
确保允许该端口通过防火墙
```
sudo ufw allow 8080/tcp
sudo ufw enable
```
### **2.设置Squid缓存大小**
要设置所需的高速缓存,请使用以下设置。对于我的情况,我使用 *256* MB
```
cache_mem 256 MB
```
### **3. 指定要使用的 DNS 名称服务器**
要定义您自己的 DNS 服务器,请使用如下所示的命令
```
dns_nameservers 8.8.8.8 8.8.4.4
```
### **4.Squid ACL和http_access**
现在编辑squid.conf 添加您选择的规则。代理服务器会选择性地选择通过它的内容。我们可以允许来自特定网络/IP 地址的访问并拒绝其他网络/IP 地址。它还可用于通过限制对某些网站的访问或基于某些关键字阻止内容来过滤流量。这是通过使用 ACL访问控制列表来实现的ACL 定义允许什么和拒绝什么。 Http_access 根据 ACL 定义允许或拒绝。
## **如何为 Squid 代理服务器定义 ACL**
ACL访问控制列表是定义允许哪些内容通过代理服务器以及拒绝哪些内容的语句。每个语句必须以 **acl** 开头,后跟规则名称。名称后面是**acltype**,最后是**参数**或**文件。** 在使用文件的情况下,文件中的每一行必须仅包含一项。
```
acl aclname acltype argument..
acl aclname acltpe “file”…
```
默认定义的ACL规则如图所示。您可以通过在每行开头添加 # 来选择禁用。要创建新规则,请按照以下示例操作:
示例1允许局域网网络通过Squid代理服务器
创建acl规则
```
acl my_lan src 192.168.100.0/24
```
现在,使用 **http_access** 指令根据定义的规则允许或拒绝。在我们的例子中,我们需要允许
```
http_access allow my_lan
```
请注意,每次创建 ACL 访问规则时的最后一条规则应该是**拒绝全部**。当您允许所有必需的站点时应执行此操作,否则您可能会阻止自己访问某些所需的站点。
```sql
http_access deny all
```
## **拒绝访问 Squid 代理服务器中的特定网站**
当处理多个网站时,将所有网站放在一个文件中然后调用它会更容易且更有组织性,否则您将在 acl 规则中列出引用。在我们的示例中我将在squid 目录中创建一个名为deniedsites.squid 的文件。
```
sudo vim /etc/squid/deniedsites.squid
```
添加您希望拒绝访问的站点。就我而言,我使用的是 facebook 和 youtube。之后保存文件。
```
.facebook.com
.youtube.com
```
现在打开squid.conf并为被拒绝的站点创建acl规则并添加拒绝规则然后保存文件。
```
acl deniedsites dstdomain “/etc/squid/deniedsites.squid”
http_access deny deniedsites
```
如果您要在 acl 规则中列出站点:
```
acl deniedsites dstdomain facebook.com youtube.com
http_access deny deniedsites
```
注意每次修改后都必须重启squid服务器
```
sudo systemctl restart squid
```
## **如何在Squid代理服务器中根据某些关键字来阻止流量**
创建一个包含关键字的文件。使用文件名创建拒绝流量的 ACL 规则。
```
sudo vim /etc/squid/keywords.squid.
```
添加您的关键字并保存。
```
gamble
nudes
xxx
```
编辑squid.conf以创建acl和拒绝规则保存并记住**重新启动squid。**
```powershell
acl keywords url_regex -i "/etc/squid/keywords.squid"
http_access deny keywords
```
您的文件最终出现并添加了如下内容。
对于 ACL
![https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/How-to-create-access-lists-in-Squid-proxy-server-configuration-file.png](https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/How-to-create-access-lists-in-Squid-proxy-server-configuration-file.png)
对于http_访问
![https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/Define-your-http-access-in-Squid-proxy-server-configuration-file.png](https://cn.linux-console.net/common-images/install-and-configure-squid-proxy-server-on-ubuntu/Define-your-http-access-in-Squid-proxy-server-configuration-file.png)
要在 Squid 代理服务器中打开端口,请使用如下所示的命令语法
```xml
acl Safe_ports port <port-number>
```
## **如何屏蔽 Squid 代理服务器上的传出流量**
代理服务器应该通过暴露代理 IP 地址而不是我们自己的 IP 地址来隐藏我们的身份。但是,代理可以通过 http 传出流量让我们的 IP 获知。要禁用此功能,请编辑 **squid.conf** 文件并通过标头禁用。 为此,请检查带有 **#via on** 的行。取消注释并从***开启***更改为***关闭。***
```powershell
# via on
via off
```
此外,代理服务器不应在其转发的 http 请求中附加客户端 IP 地址。通过修改 **squid.conf** 文件中的以下行来禁用此功能。
为了避免泄露您的 Squid 代理服务器,您可以删除 Squid 代理标头。在TAG中添加以下行请求头访问。
```sql
request_header_access From deny all
request_header_access Server deny all
request_header_access WWW-Authenticate deny all
request_header_access Link deny all
request_header_access Cache-Control deny all
request_header_access Proxy-Connection deny all
request_header_access X-Cache deny all
request_header_access X-Cache-Lookup deny all
request_header_access X-Forwarded-For deny all
request_header_access Pragma deny all
request_header_access Keep-Alive deny all
```
保存squid.conf文件并记得重新启动squid
```
systemctl restart squid
```
## **如何检查Squid配置错误**
下面的命令将指出您的配置文件中可能存在错误的地方
```
sudo squid -k parse
```
## **连接代理服务器**
```bash
sudo nano /etc/environment
export http_proxy="http://username:password@proxyipaddress:proxyport"
export https_proxy="http://username:password@proxyipaddress:proxyport"
export no_proxy="localhost, 127.0.0.1"
# 用代理的IP地址和端口替换proxyipaddress和proxyport。
export http_proxy="http://192.168.12.246:3128"
export https_proxy="http://192.168.12.246:3128"
export no_proxy="localhost, 127.0.0.1"
# 接下来
sudo visudo
# 将下面的行添加到文件中这样sudo将使用您刚刚创建的环境变量:
Defaults env_keep+="http_proxy https_proxy no_proxy"
# 重启
```