A服务器配置(IPV6+IPV4)

json
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
blog.goai.love { # 配置服务器监听的端口 bind :80 # 监听所有接口的 HTTP 端口(80) bind :443 # 监听所有接口的 HTTPS 端口(443) # 站点访问日志配置 (外部于任何 handle 块) log { output file /var/log/caddy/blog.ulucky.cn.access.log { roll_size 10MB # 日志文件达到10MB时滚动 roll_keep 10 # 保留最近10个滚动日志文件 roll_keep_days 7 # 保留最近7天的日志文件 } format console { time_format "2006-01-02 15:04:05" # 日志时间格式 time_local level_format "upper" # 日志级别使用大写 } } # 创建IPv6匹配器 - 匹配所有IPv6请求 @ipv6 { remote_ip 2000::/3 # 匹配IPv6地址范围 } # 创建IPv4匹配器 - 匹配所有非IPv6地址范围的请求 @ipv4 { not remote_ip 2000::/3 # 匹配非IPv6地址的请求(即IPv4请求) } # 处理IPv6请求 - 重定向到IPv6专用域名 handle @ipv6 { redir https://blog.ulucky.cn{uri} permanent # 301永久重定向,保留URI路径 } # 处理IPv4请求 - 反向代理 handle @ipv4 { # 设置反向代理,将IPv4请求转发到B服务器 # 明确指定协议和端口,确保Caddy知道目标是HTTPS reverse_proxy https://blog.ulucky.cn:443 { # 明确指定端口号443 header_up Host blog.ulucky.cn # 设置转发给上游的Host头 # 传递客户端IP信息 header_up X-Real-IP {remote} # 设置X-Real-IP头为客户端实际IP header_up X-Forwarded-For {remote} # 添加X-Forwarded-For头,某些应用依赖此头 header_up X-Forwarded-Proto {scheme} # 添加原始协议信息,帮助上游服务器理解请求来源 # 配置上游TLS连接 transport http { tls # 启用TLS,因为上游是HTTPS服务器 tls_insecure_skip_verify # 忽略上游服务器证书验证 } # 添加健康检查,确保上游服务器可用 health_timeout 5s # 健康检查超时时间 health_status 200 # 期望的健康状态码 } } }

B服务器配置(IPV6)

json
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
# 全局配置块 { # 配置全局日志 log { # 输出到控制台,使用 JSON 格式 output stdout # 设置日志格式为控制台友好格式 format console # 设置日志级别(debug, info, warn, error, fatal, panic) level INFO } } # 你的域名配置 blog.ulucky.cn { # 监听端口配置(同时支持 IPv4 和 IPv6) bind [::]:80 bind [::]:443 # 配置访问日志(access log) log { # 输出到文件 output file /var/log/caddy/blog.ulucky.cn.access.log { # 日志轮转配置 roll_size 10MB # 当文件达到10MB时轮转 roll_keep 10 # 保留最近10个文件 roll_keep_days 7 # 保留最近7天的日志 } # 设置日志格式 format console { time_format "2006-01-02 15:04:05" time_local level_format "upper" } } # HTTP 请求重定向到 HTTPS @http { protocol http } redir @http https://{host}{uri} permanent # 反向代理配置 reverse_proxy * http://127.0.0.1:8090 { # 设置超时时间 transport http { dial_timeout 30s # 连接超时 response_header_timeout 30s # 响应头超时 read_timeout 30s # 读取超时 write_timeout 30s # 写入超时 } # 添加代理头信息 header_up Host {host} header_up X-Real-IP {remote_host} # 健康检查 health_uri / health_interval 30s } # 启用压缩 encode gzip zstd # 设置安全响应头 header { # HSTS 配置 Strict-Transport-Security "max-age=31536000; includeSubDomains" # 防止点击劫持 X-Frame-Options "SAMEORIGIN" # XSS 保护 X-Content-Type-Options "nosniff" } }