You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Varnish适配WordPress与Ghost多应用及解决503错误

多应用场景下Varnish多VCL配置方案及Ghost 503错误解决方法

一、核心问题定位

Ghost出现503错误,核心原因大概率是Varnish路由到Ghost后端时配置错误,或后端健康检查失败。先确认三个关键项:

  • Ghost服务的IP/端口是否正常(你指定的3021端口)
  • Varnish中Ghost后端的定义是否准确
  • 后端健康检查是否通过

二、多VCL标准配置方案

推荐采用「主VCL做域名路由+各应用独立VCL片段」的架构,便于维护和隔离逻辑。Varnish支持通过include引入多个VCL文件,无需修改主文件即可扩展应用规则。

1. 主VCL配置(/etc/varnish/default.vcl)

主文件负责域名区分和后端路由,同时引入各应用的专属逻辑:

vcl 4.1;

# 定义WordPress后端(假设WordPress运行在Nginx的8080端口)
backend wordpress {
    .host = "127.0.0.1";
    .port = "8080";
    .probe = {
        .url = "/wp-admin/install.php";
        .interval = 5s;
        .timeout = 2s;
        .window = 5;
        .threshold = 3;
    }
}

# 定义Ghost后端
backend ghost {
    .host = "127.0.0.1";
    .port = "3021";
    .probe = {
        .url = "/";
        .interval = 5s;
        .timeout = 2s;
        .window = 5;
        .threshold = 3;
    }
}

# 引入各应用的VCL片段
include "/etc/varnish/wordpress.vcl";
include "/etc/varnish/ghost.vcl";

sub vcl_recv {
    # 按域名路由到对应后端逻辑
    if (req.http.host ~ "^(www\.)?your-wp-domain\.com$") {
        call wordpress_recv;
        return (pipe);
    } elsif (req.http.host ~ "^(www\.)?your-ghost-domain\.com$") {
        call ghost_recv;
        return (pipe);
    }
    # 未配置域名返回404
    return (synth(404, "Domain not configured"));
}

sub vcl_backend_response {
    # 按后端类型处理响应缓存
    if (bereq.backend == wordpress) {
        call wordpress_backend_response;
    } elsif (bereq.backend == ghost) {
        call ghost_backend_response;
    }
}

2. WordPress专属VCL(/etc/varnish/wordpress.vcl)

保留你原有的可正常运行的配置,封装为独立子程序:

sub wordpress_recv {
    # 排除后台及登录路径缓存
    if (req.url ~ "^/wp-admin/" || req.url ~ "^/wp-login.php") {
        return (pass);
    }
    # 静态资源去Cookie并移除参数
    if (req.url ~ "^/(wp-content|wp-includes)/.*\.(css|js|png|jpg|jpeg|gif|ico)$") {
        unset req.http.cookie;
        set req.url = regsub(req.url, "\?.*$", "");
    }
}

sub wordpress_backend_response {
    # 静态页面缓存1小时
    if (beresp.status == 200 || beresp.status == 301) {
        set beresp.ttl = 1h;
        unset beresp.http.set-cookie;
    }
}

3. Ghost专属VCL(/etc/varnish/ghost.vcl)

针对Ghost特性配置,解决503问题:

sub ghost_recv {
    # Ghost后台及API路径跳过缓存
    if (req.url ~ "^/ghost/" || req.url ~ "^/api/") {
        return (pass);
    }
    # 静态图片资源永久缓存,移除版本参数
    if (req.url ~ "^/content/images/.*\.(png|jpg|jpeg|gif|webp)$") {
        unset req.http.cookie;
        set req.url = regsub(req.url, "\?v=[0-9]+", "");
        set req.http.Cache-Control = "public, max-age=31536000";
    }
    # 确保Host头正确传递给Ghost后端
    set req.http.host = req.http.host;
}

sub ghost_backend_response {
    # 动态页面缓存10分钟
    if (beresp.status == 200) {
        set beresp.ttl = 10m;
        unset beresp.http.Cache-Control;
        set beresp.http.Cache-Control = "public, max-age=600";
    }
    # 后端返回503时短时间重试
    if (beresp.status == 503) {
        set beresp.ttl = 5s;
        return (restart);
    }
}

三、503错误排查步骤

  1. 验证后端连通性:在Varnish服务器执行curl http://127.0.0.1:3021,确认Ghost服务是否正常响应。如果失败,先排查Ghost进程是否启动、端口是否被占用。
  2. 查看Varnish错误日志:执行varnishlog -g request -q "ReqStatus == 503",过滤503请求日志,确认是后端连接失败还是健康检查不通过。
  3. 检查后端健康状态:执行varnishadm backend.list,查看ghost后端的状态。如果显示sick,调整probe的URL或超时参数(比如将/改为/ghost/api/v3/admin/site/)。
  4. 确认Nginx配置(若Ghost通过Nginx代理):如果Ghost先经过Nginx再到Varnish,确保Nginx虚拟主机监听3021端口,且能正常响应请求。

四、Nginx前端转发配置

确保Nginx将80/443端口的请求转发到Varnish的6081端口:

server {
    listen 80;
    server_name your-wp-domain.com your-ghost-domain.com;

    location / {
        proxy_pass http://127.0.0.1:6081;
        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;
    }
}

内容的提问来源于stack exchange,提问作者Happy Home Furniture

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 18:55:26