Varnish + nginx 503错误后端获取失败

Varnish + nginx 503错误后端获取失败

非常感谢您的帮助。我的设置:LB -> VARNISH -> NGINX

由于未知原因,我开始在客户端上收到随机错误 503。我检查了 varnish 和 Nginx 上的日志...

Varnish:在日志中我看到客户端请求,RespStatus 503,ResReason Backend 获取失败。

Nginx:如果我在 Nginx 日志中搜索相同的客户端请求,则看不到它。

我猜问题应该出在 Nginx 服务器部分?我查看了网络流量/CPU/内存等... 看起来没有出现过载。

我的 Nginx 配置:

user www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
pcre_jit on;

error_log /var/log/nginx/error.log error;
include /etc/nginx/modules/*.conf;

events {
        worker_connections 65535;

}

http {

        include /etc/nginx/sites-available/*;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        server_tokens off;

        client_max_body_size 1m;

        sendfile on;

        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

        ssl_dhparam /etc/ssl/nginx/dh2048.pem;

        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:2m;
        ssl_session_timeout 1h;
        ssl_session_tickets off;

        gzip  on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_disable "msie6";
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.apple.mpegurl;

        map $http_upgrade $connection_upgrade {
                default upgrade;
                '' close;
        }
        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

}

漆:

backend server1 { 
  .host = "192.168.245.167";    
  .port = "80";           
  .max_connections = 3000; 

  .first_byte_timeout     = 300s;  
  .connect_timeout        = 10s;    
  .between_bytes_timeout  = 6s;     
}

sub vcl_init {
  # Called when VCL is loaded, before any requests pass through it.
  # Typically used to initialize VMODs.

  new vdir = directors.round_robin();
  vdir.add_backend(server1);
...

编辑

还为 1 个请求添加 varnislog 输出:

*   << Request  >> 609730940
-   Begin          req 640024598 rxreq
-   Timestamp      Start: 1661355668.683547 0.000000 0.000000
-   Timestamp      Req: 1661355668.683547 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       192.168.245.165 40772 a0
-   ReqMethod      GET
-   ReqURL         someurl
-   ReqProtocol    HTTP/1.1
-   ReqHeader      user-agent: agent
-   ReqHeader      accept-encoding: gzip
-   ReqHeader      host: example.com
-   ReqHeader      x-real-ip: 172.31.68.46
-   ReqHeader      x-ssl: 1
-   ReqHeader      x-forwarded-for: 172.31.68.46
-   ReqUnset       x-forwarded-for: 172.31.68.46
-   ReqHeader      X-Forwarded-For: 172.31.68.46, 192.168.245.165
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: 172.31.68.46, 192.168.245.165
-   ReqHeader      X-Forwarded-For: 172.31.68.46
-   ReqUnset       host: example.com
-   ReqHeader      Host: example.com
-   ReqURL         someurl
-   VCL_return     pass
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       PASS
-   VCL_return     fetch
-   Link           bereq 609730941 pass
-   Timestamp      Fetch: 1661355668.683759 0.000212 0.000212
-   RespProtocol   HTTP/1.1
-   RespStatus     503
-   RespReason     Backend fetch failed
-   RespHeader     Date: Wed, 24 Aug 2022 15:41:08 GMT
-   RespHeader     Server: Varnish
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   RespHeader     X-Varnish: 609730940
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.5)
-   VCL_call       DELIVER
-   RespHeader     X-Cache: MISS
-   RespHeader     X-Cache-Hits: 0
-   VCL_return     deliver
-   Timestamp      Process: 1661355668.683778 0.000231 0.000019
-   Filters
-   RespHeader     Content-Length: 286
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1661355668.683838 0.000291 0.000060
-   ReqAcct        429 0 429 286 286 572
-   End
**  << BeReq    >> 609730941
--  Begin          bereq 609730940 pass
--  VCL_use        boot
--  Timestamp      Start: 1661355668.683670 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       someurl
--  BereqProtocol  HTTP/1.1
--  BereqHeader    user-agent: agent
--  BereqHeader    accept-encoding: gzip
--  BereqHeader    x-real-ip: 172.31.68.46
--  BereqHeader    x-ssl: 1
--  BereqHeader    X-Forwarded-For: 172.31.68.46
--  BereqHeader    Host: example.com
--  BereqHeader    X-Varnish: 609730941
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  FetchError     backend server1: busy
--  Timestamp      Beresp: 1661355668.683706 0.000036 0.000036
--  Timestamp      Error: 1661355668.683709 0.000038 0.000002
--  BerespProtocol HTTP/1.1
--  BerespStatus   503
--  BerespReason   Backend fetch failed
--  BerespHeader   Date: Wed, 24 Aug 2022 15:41:08 GMT
--  BerespHeader   Server: Varnish
--  VCL_call       BACKEND_ERROR
--  BerespHeader   Content-Type: text/html; charset=utf-8
--  BerespHeader   Retry-After: 5
--  VCL_return     deliver
--  Storage        malloc Transient
--  Length         286
--  BereqAcct      0 0 0 0 0 0
--  End

关于我应该寻找什么,有什么想法/提示吗?

答案1

-- FetchError backend server1: busy消息表明 Varnish 无法连接到 Nginx,因为 Nginx忙碌的。这意味着它已达到其最大连接数限制。

.max_connections如果您认为服务器可以处理更多同时连接,请在 Nginx 中增加此限制,或者通过降低后端定义中的属性来限制 Varnish 可以向 Nginx 打开的后端连接数量。

但是限制 Varnish 中的最大连接数并不能解决这个问题。

弄清楚为什么 Varnish 需要打开这么多连接是很重要的。

  • 命中率是不是太低了?
  • 对不可缓存的资源的请求是否太多?
  • 你的 VCL 中是否存在缺陷,导致后端服务器过载

我建议你读一下https://www.varnish-software.com/developers/tutorials/troubleshooting-varnish/#varnish-is-not-caching了解如何发现不可缓存的内容。根据日志,您可以进行评估,并可能在 VCL 中采取纠正措施。

我还建议您阅读以下教程:https://www.varnish-software.com/developers/tutorials/varnish-builtin-vcl/。它解释了 Varnish 默认缓存的内容以及哪些内容被视为不可缓存。这将帮助您了解需要在多大程度上进行潜在的 VCL 改进。

答案2

谢谢你的回答。问题在于连接限制,因为我有很多文件没有缓存。也谢谢你的链接……我们会看看如何改进我们的配置。

BR,罗伯特

相关内容