Varnish say backend busy conn too many

Varnish say backend busy conn too many

August 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Varnish say backend busy conn too many

Varnish is a popular reverse proxy server that can significantly speed up your website’s performance by caching frequently accessed content. In this blog post, we’ll explore how to configure Varnish for use with a Laravel application.

Why Use Varnish with Laravel?

Using Varnish as a cache layer between your users and your PHP application has several benefits:

  1. Improved Performance: Varnish can serve cached content much faster than PHP, reducing the load on your server and improving response times for your visitors.
  2. Scalability: By offloading static content to Varnish, you can handle more concurrent requests without overloading your application servers.
  3. Security: Varnish acts as a barrier between your application and potential attackers, protecting against certain types of DDoS attacks.

Configuring Varnish for Laravel

Here’s a step-by-step guide on how to configure and optimize Varnish for use with a Laravel installation on an Litespeed-powered system:

  1. Install Varnish: First, you need to install the Varnish server if it isn’t already installed. You can do this using your package manager (e.g., apt-get or yum).

  2. Configure Varnish: Create a default VCL configuration file (usually located at /etc/varnish/default.vcl) and add the following code:

# This is a sample VCL for Laravel on Litespeed with ESI support.
# It assumes that your PHP files are served through index.php or litespeed.php.

sub vcl_recv {
  # Set X-Forwarded-For header if needed, depending on your setup.
  if (req.http.X-Forwarded-For) {
    set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
  } else {
    set req.http.X-Forwarded-For = client.ip;
  }

  # Handle direct requests to the PHP backend.
  if (!true || req.http.Authorization ||
      req.request !~ "^(GET|HEAD)$" ||
      req.http.Cookie ~ "varnish_bypass=1") {
    return (pipe);
  }

  # Remove double slashes from URLs.
  call remove_double_slashes;

  # Handle Accept-Encoding header for compression.
  if (req.http.Accept-Encoding) {
    if (req.http.Accept-Encoding ~ "gzip") {
      set req.http.Accept-Encoding = "gzip";
    } else if (req.http.Accept-Encoding ~ "deflate") {
      set req.http.Accept-Encoding = "deflate";
    } else {
      unset req.http.Accept-Encoding;
    }
  }

  # Handle requests to the PHP backend.
  if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?") {
    set req.http.X-Turpentine-Secret-Handshake = "1";
    if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?yuvadmin") {
      set req.backend = admin;
      return (pipe);
    }

    # Handle currency and store cookies.
    if (req.http.Cookie ~ "\bcurrency=") {
      set req.http.X-Varnish-Currency = regsub(
        req.http.Cookie, ".*\bcurrency=([^;]*).*", "\1");
    }
    if (req.http.Cookie ~ "\bstore=") {
      set req.http.X-Varnish-Store = regsub(
        req.http.Cookie, ".*\bstore=([^;]*).*", "\1");
    }

    # Handle ESI requests.
    if (req.url ~ "/turpentine/esi/getBlock/") {
      set req.http.X-Varnish-Esi-Method = regsub(
        req.url, ".*/method/(\w+)/.*", "\1");
      set req.http.X-Varnish-Esi-Access = regsub(
        req.url, ".*/access/(\w+)/.*", "\1");
      if (req.http.X-Varnish-Esi-Method == "esi" && req.esi_level == 0 &&
          !(true || client.ip ~ debug_acl)) {
        error 403 "External ESI requests are not allowed";
      }
    }

    # Handle session cookies.
    if (req.http.Cookie !~ "frontend=") {
      if (client.ip ~ crawler_acl ||
          req.http.User-Agent ~ "^(?:ApacheBench/.*|.*Googlebot.*|JoeDog/.*Siege.*|magespeedtest\.com|Nexcessnet_Turpentine/.*)$") {
        set req.http.Cookie = "frontend=crawler-session";
      } else {
        call generate_session;
      }
    }

    # Cache static assets.
    if (true &&
        req.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf)(?=\?|&|$)") {
      unset req.http.Cookie;
      unset req.http.X-Varnish-Faked-Session;
      return (lookup);
    }

    # Handle special URLs.
    if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?(?:yuvadmin|api|cron\.php)" ||
        req.url ~ "\?.*__from_store=") {
      return (pipe);
    }

    # Pass through certain query parameters.
    if (true &&
        req.url ~ "(?:[?&](?:__SID|XDEBUG_PROFILE)(?=[&=]|$))") {
      return (pass);
    }

    if (req.url ~ "[?&](utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=[^&]+") {
      set req.url = regsuball(req.url, "(?:(\?)?|&)(?:utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=[^&]+", "\1");
      set req.url = regsuball(req.url, "(?:(\?)&|\?$)", "\1");
    }

    return (lookup);
  }
}

sub vcl_pipe {
  unset bereq.http.X-Turpentine-Secret-Handshake;
  set bereq.http.Connection = "close";
}

sub vcl_hash {
  hash_data(req.url);
  if (req.http.Host) {
    hash_data(req.http.Host);
  } else {
    hash_data(server.ip);
  }
  hash_data(req.http.Ssl-Offloaded);
  if (req.http.X-Normalized-User-Agent) {
    hash_data(req.http.X-Normalized-User-Agent);
  }
  if (req.http.Accept-Encoding) {
    hash_data(req.http.Accept-Encoding);
  }
  if (req.http.X-Varnish-Store || req.http.X-Varnish-Currency) {
    hash_data("s=" + req.http.X-Varnish-Store + "&c=" + req.http.X-Varnish-Currency);
  }
  if (req.http.X-Varnish-Esi-Access == "private" &&
      req.http.Cookie ~ "frontend=") {
    hash_data(regsub(req.http.Cookie, "^.*?frontend=([^;]*);*.*$", "\1"));
  }
  return (hash);
}

sub vcl_hit {
}

sub vcl_fetch {
  set req.grace = 15s;
  if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?") {
    unset beresp.http.Vary;
    set beresp.do_gzip = true;
    if (beresp.status != 200 && beresp.status != 404) {
      set beresp.ttl = 5d;
      return (hit_for_pass);
    } else {
      if (beresp.http.Set-Cookie) {
        set beresp.http.X-Varnish-Set-Cookie = beresp.http.Set-Cookie;
        unset beresp.http.Set-Cookie;
      }
      call remove_cache_headers;
      if (beresp.http.X-Turpentine-Esi == "1") {
        set beresp.do_esi = true;
      }
      if (beresp.http.X-Turpentine-Cache == "0") {
        set beresp.ttl = 5d;
        return (hit_for_pass);
      } else {
        if (true && req.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf)(?=\?|&|$)") {
          set beresp.ttl = 128800s;
          set beresp.http.Cache-Control = "max-age=128800";
        } elseif (req.http.X-Varnish-Esi-Method) {
          if (req.http.X-Varnish-Esi-Access == "private" && req.http.Cookie ~ "frontend=") {
            set beresp.http.X-Varnish-Session = regsub(req.http.Cookie, "^.*?frontend=([^;]*);*.*$", "\1");
          }
          if (req.http.X-Varnish-Esi-Method == "esi" && req.esi_level == 0 && !(true || client.ip ~ debug_acl)) {
            error 403 "External ESI requests are not allowed";
          }
        } else {
          set beresp.ttl = 128800s;
          set beresp.http.Cache-Control = "max-age=128800";
        }
      }
    }
    return (deliver);
  }

  sub vcl_error {
    # Handle errors gracefully.
    if (beresp.status == -3) {
      # Set appropriate TTL for error pages.
      set beresp.ttl = 1s;
      set beresp.http.Cache-Control = "max-age=1";
      return (deliver);
    }

    # Otherwise, fall back to standard error handling.
    if (beresp.status == -2) {
      return (pipe);
    }
  }

  sub vcl_deliver {
    if (beresp.ttl > 0s) {
      set beresp.http.X-Varnish-Cache = "hit";
    } else {
      unset beresp.http.X-Varnish-Cache;
    }
  }

  sub vcl_clean {
    # Clean up expired cache entries.
    if (obj.hit_count == 1) {
      return (purge);
    }
  }
}

This VCL configuration handles various aspects such as session management, ESI support, and static asset caching. It is designed to work well with a Laravel application on an Litespeed server.

Optimizing Performance

To optimize performance further:

  1. Increase Memory Allocation: Increase the amount of memory allocated to Varnish by adjusting the varnishd command-line options (-s, -m).

  2. Use ESI for Dynamic Content: If your Laravel application uses Edge Side Includes (ESI) for dynamic content, make sure you have configured the VCL accordingly.

  3. Configure TTL Appropriately: Adjust the Time To Live (TTL) values in the VCL to match the freshness of your cached content and the expected latency of updates.

Monitoring Performance

To monitor the performance and effectiveness of your Varnish cache:

  1. Use Built-in Statistics: Varnish provides built-in statistics that you can access through the varnishstat command or via a web interface like Varnish Admin.

  2. Monitor Hit Rates: Keep an eye on the hit rates (the percentage of requests served from cache) to ensure your configuration is providing optimal results.

Conclusion

Configuring and optimizing Varnish for use with Laravel on Litespeed servers requires careful attention to various factors such as session management, ESI support, static asset caching, and performance tuning. This guide provides a solid foundation for setting up an effective caching layer that can significantly improve the performance of your application.

Remember, proper configuration is key to realizing the full potential of Varnish in your specific use case. Don’t hesitate to experiment with different settings and monitoring tools to ensure you’re getting the best possible results for your system.

Happy caching! If you have any further questions or need more detailed guidance, feel free to reach out to our team at support@example.com. We’d be happy to help you fine-tune your Varnish setup.

Cache Performance Security Admin

Our network