caddy的反向代理、重定向配置
```
# 配置示例
# 1. 反向代理
example.com {
reverse_proxy 127.0.0.1:8080 #这里输入你想反代的服务器IP和端口
encode gzip
}
# 2. 反向代理+基本认证
example.com {
basicauth / username password
reverse_proxy localhost:3000
}
# 3. 反向代理+负载均衡
example.com {
reverse_proxy backend1:3000 backend2:3000
}
# 4. 重定向
example.com {
redir https://a.example.com{uri}
}
更多配置
1. 负载均衡
你可以通过指定多个后端服务器来实现负载均衡。Caddy 会自动在这些服务器之间分配流量。
example.com {
reverse_proxy backend1:3000 backend2:3000 {
lb_policy round_robin
}
}
2. 路由和重定向
你可以配置不同的路由和重定向规则。
example.com {
reverse_proxy /api/* localhost:3001
reverse_proxy /app/* localhost:3002
redir /old-path /new-path 301
}
3. WebSocket 支持
Caddy 支持 WebSocket 代理。你只需像普通的 HTTP 请求一样配置。
example.com {
reverse_proxy /ws/* localhost:3000 {
header_up X-Real-IP {remote}
header_up X-Forwarded-For {remote}
header_up X-Forwarded-Proto {scheme}
}
}
4. SSL/TLS 配置
Caddy 默认启用 HTTPS。如果你需要自定义 SSL/TLS 设置,可以在 Caddyfile 中进行配置。
example.com {
reverse_proxy localhost:3000
tls {
cert_file /path/to/cert.pem
key_file /path/to/key.pem
}
}
5. 基于路径的反向代理
你可以根据请求的路径将请求代理到不同的服务。
example.com {
reverse_proxy /service1/* localhost:3001
reverse_proxy /service2/* localhost:3002
}
6. 自定义错误页面
你可以配置自定义错误页面。
example.com {
reverse_proxy localhost:3000
handle_errors {
respond 404 "Sorry, the page was not found" 404
}
}
7. 添加 HTTP 头
你可以在反向代理请求中添加或修改 HTTP 头。
example.com {
reverse_proxy localhost:3000 {
header_up X-Forwarded-For {remote}
header_up X-Real-IP {remote}
}
}
8. 使用插件
Caddy 有许多插件可以扩展其功能,比如身份验证、速率限制等。你可以根据需要安装这些插件并在 Caddyfile 中配置。
9. 设置速率限制
你可以对请求设置速率限制,以防止滥用。
example.com {
reverse_proxy localhost:3000
rate_limit {
zone rate_limit 10r/s
}
}
10. 访问日志和错误日志
你可以启用访问日志和错误日志来监控请求和错误。
example.com {
reverse_proxy localhost:3000
log {
output file /var/log/caddy/access.log
format json
}
log {
output file /var/log/caddy/error.log
}
}
11.真实ip
header_up:用于将请求头信息添加到代理请求中。 X-Real-IP:直接传递客户端的 IP 地址。 X-Forwarded-For:包含客户端的 IP 地址链,允许后端识别原始客户端 IP。 X-Forwarded-Proto:指示原始请求的协议(HTTP 或 HTTPS)
example.com {
reverse_proxy localhost:3000 {
header_up X-Real-IP {remote}
header_up X-Forwarded-For {remote}
header_up X-Forwarded-Proto {scheme}
}
}
```
- 共 0 条回复
- 需要登录 后方可回复, 如果你还没有账号请点击这里注册。
wiseAI
✨ 梦初醒 茅塞开
- 不经他人苦,莫劝他人善。
- 能量足,心态稳,温和坚定可以忍。
- 辛苦决定不了收入,真正决定收入的只有一个,就是不可替代性。
- 要么忙于生存,要么赶紧去死!
- 内心强大到混蛋,比什么都好!
- 规范流程比制定制度更重要!
-
立志需要高远,但不能急功近利;
行动需要迅速,却不可贪图速成。 - 不要强求人品,要设计高效的机制。
-
你弱的时候,身边都是鸡零狗碎;
你强的时候,身边都是风和日丽。 - 机制比人品更可靠,契约比感情更可靠。
- 合作不意味着没有冲突,却是控制冲突的最好方法。
- 误解是人生常态,理解本是稀缺的例外。
- 成功和不成功之间,只差一次坚持!
- 祁连卧北雪,大漠壮雄关。
- 利益顺序,过程公开,机会均等,付出回报。
