post

解决 Varnish 频繁出现 Error 503/400错误

最近发现使用了 Varnish 缓存后,后台操作经常返回 Error 503 Service unavailable,尤其是发布文章的时候,经排查,我知道原因和解决办法了!

Varnish Error 503

p.s. 该文章的方案也不能完全解决我的问题,无奈下我暂时去掉了varnish,改用dnspod的cdn缓存了,方便省事。

解决Varnish 503 错误办法

出现503错误,是因为 Varnish 对后端服务器响应header有限制,默认长度是2048,将其调大一些就好了,在varnish启动参数中加入:

-p http_resp_hdr_len=8192

再重启 Varnish, 即可解决其503错误。

如果这步仍然无效,在 default.vcl 文件中,可以适当调大后端服务的超时参数 connect_timeout first_byte_timeoutbetween_bytes_timeout,示例如下:

backend default {
  .host = "www.izhangheng.com";
  .connect_timeout = 10s;
  .first_byte_timeout = 20s;
  .between_bytes_timeout = 30s;
}

但从实际结果看,有时在vcl脚本中的设置,可能不生效,所以可考虑在运行参数中增加这些设置,如,varnishd xxxx -p connect_timeout=20s。

解决Varnish 400 错误办法

出现400错误,是因为 Varnish 认为客户端请求header行数及长度过大,其默认最大接受的请求header行数为64,最大长度(所有请求header行长度之和)为2048,解决这个问题比较简单,在varnish启动参数中加入:

-p http_max_hdr=256 -p http_req_hdr_len=8192

再重启 Varnish, 即可解决其400错误。

Comments

  1. 过来学习,欢迎经常互访,共同进步。

Speak Your Mind

*

· 930 次浏览