使用Caddy配置Edge代理
-
安装Caddy
- 在终端运行以下命令安装Caddy:
curl -L https://github.com/gohugo/hugo/releases/download/v1.25./hugo-1.25.-linux-amd64.gz | gunzip -c | sudo tee /usr/local/bin/hugo > /dev/null sudo chmod +x /usr/local/bin/hugo
- 在终端运行以下命令安装Caddy:
-
启动Caddy
- 在终端运行启动命令:
hugo server --baseURL http://localhost:808
- 这将启动Caddy,并自动打开浏览器。
- 在终端运行启动命令:
-
设置基本路由
- 在浏览器中,访问
http://localhost:808,你会看到Caddy的默认页面。 - 修改
Caddyfile文件(位于~/.caddy),添加以下内容:http://localhost:808 { reverse_proxy localhost:808 # 或者直接 reverse_proxy http://localhost:808 }
- 在浏览器中,访问
-
启用重写规则
- 如果需要处理路径重写,添加以下内容:
http://localhost:808/abc/ { reverse_proxy http://localhost:808/ }
- 如果需要处理路径重写,添加以下内容:
-
设置访问日志
- 修改Caddy配置,添加日志记录:
http://localhost:808 { reverse_proxy localhost:808 log_output true access_log /var/log/caddy/access.log }
- 修改Caddy配置,添加日志记录:
-
启用HTTPS(可选)
- 修改Caddyfile,添加TLS配置:
http://localhost:808 { reverse_proxy localhost:808 tls { cert_file /path/to/ssl.crt key_file /path/to/ssl.key } }
- 修改Caddyfile,添加TLS配置:
-
自动化部署(可选)
- 使用GitHub Actions自动化测试和更新:
git clone https://github.com/your-repo.git cd your-repo echo 'webhook' > .git/hooks/hugo git add .git/hooks/hugo git commit -m "Add webhook" git push
- 添加Caddy的配置文件到
.gitignore,避免被提交。
- 使用GitHub Actions自动化测试和更新:
使用Nginx配置Edge代理
-
安装Nginx
- 运行以下命令安装Nginx:
sudo apt-get update sudo apt-get install nginx
- 运行以下命令安装Nginx:
-
创建配置文件
- 打开文本编辑器,创建
/etc/nginx/sites-available/default文件:server { listen 80; server_name localhost; location / { proxy_pass http://localhost:808; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
- 打开文本编辑器,创建
-
启用Nginx
- 启动并重启Nginx:
sudo systemctl start nginx sudo systemctl enable nginx
- 启动并重启Nginx:
-
设置重写规则(可选)
- 修改
/etc/nginx/sites-available/default,添加重写:location /abc/ { rewrite ^/abc/(.*) /$1; proxy_pass http://localhost:808; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
- 修改
-
访问日志(可选)
- 修改
/etc/nginx/sites-available/default,添加日志:access_log /var/log/nginx/access.log;
- 修改
-
配置HTTPS(可选)
- 修改
/etc/nginx/sites-available/default,添加TLS配置:server { listen 443 ssl; server_name localhost; ssl on; ssl_certificate /path/to/ssl.crt; ssl_private_key /path/to/ssl.key; location / { proxy_pass http://localhost:808; # 其他配置 } }
- 修改
使用Traefik配置Edge代理
-
安装Traefik
- 运行以下命令安装:
curl -L -o traefik https://github.com/traefik/traefik/releases/download/v3.8.5/traefik-v3.8.5-linux-x64.tar.gz tar -xzf traefik-v3.8.5-linux-x64.tar.gz cd traefik sudo ./traefik --no-default-config --config-file /path/to/traefik.yaml
- 运行以下命令安装:
-
配置Traefik
- 创建
traefik.yaml文件:accessLog: /var/log/traefik/access.log logLevel: debug defaultEntryPoints: http entryPoints: http: address: :80 compression: true options: ssl: { enabled: true, certFile: /path/to/ssl.crt, keyFile: /path/to/ssl.key }
- 创建
-
定义域和路由
- 在
traefik.yaml中添加路由:routes: - entryPoint: http match: ["paths}/{path}/^/"] service: localhost:808 rule: "pathMatches(/^/)"
- 在
-
启动Traefik
- 运行:
sudo ./traefik --no-default-config --config-file /path/to/traefik.yaml
- 运行:
-
测试访问
- 在浏览器中访问
http://localhost,检查是否正确转发到本地服务器。
- 在浏览器中访问
测试配置
- Caddy:访问
http://localhost:808,检查转发是否正常。 - Nginx:访问
http://localhost,检查是否正确反向代理。 - Traefik:访问
http://localhost,确保HTTPS和域名配置正确。
常见问题
- 配置错误:仔细检查每一步,确保语法正确。
- 路径问题:确保路径转换正确,避免出现404错误。
- HTTPS问题:检查证书路径和配置,确保Traefik或Nginx正确加载证书。
通过以上步骤,你可以根据需求选择合适的代理工具,配置Edge代理来满足你的网络需求。









