Docker中配置子路径反向代理Varnish遇503错误的解决方法
解决方案
1. 问题根源
出现Error 503 Backend fetch failed是因为Varnish转发请求时路径不匹配:pg_tileserv设置了TS_BASEPATH=/tiles,服务路径前缀为/tiles,但原Varnish配置直接将请求转发到后端根路径/,导致找不到对应资源。
2. 修改Varnish配置
创建自定义VCL文件处理路径重写,确保请求能正确转发到pg_tileserv:
- 在项目目录下新建
./config/varnish/default.vcl,内容如下:
vcl 4.1; backend default { .host = "tiles"; .port = "7800"; } sub vcl_recv { # 将/cache开头的请求重写为/tiles前缀,转发到pg_tileserv if (req.url ~ "^/cache/(.*)") { set req.url = "/tiles/" + regsub(req.url, "^/cache/", ""); } return (hash); } sub vcl_backend_response { # 设置缓存有效期为10分钟(600秒) set beresp.ttl = 600s; # 移除响应中的Cookie,确保内容能被缓存 unset beresp.http.Set-Cookie; }
3. 更新docker-compose.yml中的cache服务
挂载自定义VCL文件,移除原冲突的环境变量:
cache: image: eeacms/varnish volumes: - ./config/varnish/default.vcl:/etc/varnish/default.vcl environment: DNS_ENABLED: 'false' depends_on: tiles: condition: service_started restart: always
4. 修正Nginx配置
确保转发路径正确匹配后端服务端口:
location /tiles { proxy_pass http://tiles:7800; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location /cache { proxy_pass http://cache:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
5. 验证配置
- 重启所有服务:
docker-compose down && docker-compose up -d - 访问
http://localhost/tiles,确认能正常获取pg_tileserv的元数据 - 访问
http://localhost/cache,确认能通过Varnish缓存访问瓦片资源,且不再出现503错误
内容的提问来源于stack exchange,提问作者kartoza-geek
相关产品推荐
相关产品推荐

