如何配置反向转发?

SSLcat 的反向转发功能基于域名进行智能转发,支持多种协议和负载均衡策略。以下是详细的配置方法。

基本配置结构

SSLcat 的配置文件采用 YAML 格式,基本结构如下:

# sslcat.conf
server:
  port: 443
  http_port: 80

# 反向转发配置
proxies:
  - domain: "example.com"
    target: "http://localhost:3000"
    protocol: "http"
  
  - domain: "api.example.com"
    target: "https://backend.example.com"
    protocol: "https"
    ssl_verify: false

域名配置

每个转发规则都需要指定域名和目标服务器:

多域名配置

可以配置多个域名指向不同的后端服务:

proxies:
  # 主站
  - domain: "www.example.com"
    target: "http://localhost:3000"
    protocol: "http"
  
  # API 服务
  - domain: "api.example.com"
    target: "http://localhost:8080"
    protocol: "http"
  
  # 管理后台
  - domain: "admin.example.com"
    target: "http://localhost:9000"
    protocol: "http"
  
  # 静态资源
  - domain: "static.example.com"
    target: "http://localhost:4000"
    protocol: "http"

负载均衡配置

SSLcat 支持多种负载均衡策略:

proxies:
  - domain: "app.example.com"
    targets:
      - "http://backend1.example.com:3000"
      - "http://backend2.example.com:3000"
      - "http://backend3.example.com:3000"
    protocol: "http"
    load_balance: "round_robin"  # 轮询
    # load_balance: "least_conn"  # 最少连接
    # load_balance: "ip_hash"     # IP 哈希

WebSocket 支持

SSLcat 原生支持 WebSocket 代理:

proxies:
  - domain: "ws.example.com"
    target: "ws://localhost:8080"
    protocol: "ws"
    websocket:
      enabled: true
      ping_interval: 30
      pong_timeout: 10

健康检查配置

配置后端服务的健康检查:

proxies:
  - domain: "app.example.com"
    target: "http://localhost:3000"
    protocol: "http"
    health_check:
      enabled: true
      path: "/health"
      interval: 30
      timeout: 5
      retries: 3

请求头配置

可以添加或修改请求头:

proxies:
  - domain: "api.example.com"
    target: "http://localhost:8080"
    protocol: "http"
    headers:
      add:
        X-Forwarded-Proto: "https"
        X-Real-IP: "$remote_addr"
      remove:
        - "X-Forwarded-For"

路径重写

支持 URL 路径重写:

proxies:
  - domain: "api.example.com"
    target: "http://localhost:8080"
    protocol: "http"
    path_rewrite:
      "/v1/api": "/api"
      "/old": "/new"

连接池配置

优化连接池性能:

proxies:
  - domain: "app.example.com"
    target: "http://localhost:3000"
    protocol: "http"
    connection_pool:
      max_connections: 100
      max_idle_connections: 10
      idle_timeout: 90
      keep_alive: true

故障转移配置

配置后端服务的故障转移:

proxies:
  - domain: "app.example.com"
    targets:
      - "http://primary.example.com:3000"
      - "http://backup1.example.com:3000"
      - "http://backup2.example.com:3000"
    protocol: "http"
    failover:
      enabled: true
      max_failures: 3
      recovery_time: 60

SSL 配置

配置与后端的 SSL 连接:

proxies:
  - domain: "secure.example.com"
    target: "https://backend.example.com"
    protocol: "https"
    ssl:
      verify: true
      cert_file: "/path/to/ca.crt"
      key_file: "/path/to/ca.key"
      insecure_skip_verify: false

配置验证

启动前验证配置:

# 验证配置文件语法
sslcat --config sslcat.conf --check

# 测试配置
sslcat --config sslcat.conf --test

# 启动服务
sslcat --config sslcat.conf

监控和调试

SSLcat 提供详细的代理统计信息:

最佳实践

配置反向转发时的建议:

通过以上配置,你可以灵活地设置 SSLcat 的反向转发功能,满足各种复杂的业务需求。