Clash是一个开源的代理服务器,支持多种协议的代理转发,包括HTTP、HTTPS、WebSocket等,以下是设置Clash代理的详细教程:
安装Clash
确保你的系统已经安装了Clash,以下是几种常见的安装方法:
使用Homebrew(Mac/Linux)
brew install clash
安装完成后,Clash会被安装到 /usr/local/bin/clash。
使用源码编译(推荐)
下载Clash的源码并编译:
git clone https://github.com/Liaotuo/clash.git cd clash make
编译完成后,Clash可执行文件位于 ./clash。
设置Clash配置文件
Clash使用JSON格式配置文件来指定代理规则和设置,默认配置文件位于 ~/.clash/config.json,以下是一个基本的配置示例:
{
"http": 789,
"https": 7891,
"ws": 7892,
"wss": 7893,
"socks": 7894,
"socks_all": 7895,
"exclude": ["*.utm.io"],
"rules": [
{
"prefix": "http://example.com",
"replacements": [
{
"domain": "www.example.com",
"replacement": "http://new.example.com"
}
]
}
]
}
启动Clash代理
使用命令启动Clash代理服务器,并确保其一直运行:
clash -o
系统代理设置
根据你的操作系统,配置系统代理设置:
Mac
- 打开系统偏好设置(
System Preferences)。 - 选择
网络>网络偏好。 - 选择当前网络连接,点击
编辑。 - 在
代理标签下,设置以下内容:- 代理服务器:
localhost - 代理端口:
789 - HTTPS代理:
localhost - HTTPS端口:
7891
- 代理服务器:
- 点击
确定。
Windows
- 右键点击桌面,选择
新建>记事本。 - 输入以下内容:
@echo off clash -o
- 保存文件为
.bat文件(如clash.bat)。 - 双击运行该批处理文件,Clash会在后台运行。
Linux
-
使用
sudo创建一个新的systemd服务文件,clash.service:[Unit] Description=Clash Proxy After=network.target [Service] ExecStart=/usr/bin/clash -o Restart=always User=non-root [Install] WantedBy=multi-user.target
-
保存文件后,创建符号链接:
sudo ln -sf /etc/systemd/system/clash.service /etc/rc.local sudo systemctl enable clash sudo systemctl start clash
测试代理配置
- 打开浏览器,访问
http://localhost:789,确保Clash代理正常运行。 - 测试应用是否能通过代理服务器,例如访问
http://example.com。
高级配置(可选)
启用SSL/TLS
如果Clash代理需要处理HTTPS流量,可以启用SSL/TLS:
{
"https": 7891,
"ssl": true,
"cert-file": "/path/to/cert.pem",
"key-file": "/path/to/key.pem"
}
负载均衡
添加负载均衡规则:
{
"rules": [
{
"prefix": "http://example.com",
"replacements": [
{
"domain": "www.example.com",
"replacement": "http://new.example.com"
}
],
"loadbalance": {
"strategy": "roundrobin",
"interval": 100
}
}
]
}
过滤规则
添加域名过滤规则:
{
"rules": [
{
"prefix": "http://example.com",
"replacements": [
{
"domain": "www.example.com",
"replacement": "http://new.example.com"
}
]
},
{
"prefix": "http://block.example.com",
"replacements": [
{
"domain": "ads.example.com",
"replacement": ""
}
]
}
]
}
日志监控
Clash提供详细的日志输出,用于调试和监控:
clash -o
卸载Clash
当不再需要Clash代理时,可以卸载:
使用Homebrew
brew uninstall clash
使用源码
cd /usr/local/bin rm clash
常见问题
- 端口冲突:确保目标端口未被占用。
- 配置错误:检查JSON格式,使用
clash -v查看版本信息。 - 权限问题:确保Clash运行用户有足够权限。
通过以上步骤,你可以成功配置并使用Clash代理,管理多种协议的网络流量,Clash的高级功能和灵活配置使其成为一个强大的网络代理工具。









