Welcome to the first article in our Varnish series, where we’ll explore practical techniques for leveraging Varnish Cache (soon to be rebranded as Vinyl) beyond simple caching. While many developers view Varnish as just an HTTP accelerator, its VCL (Varnish Configuration Language) provides powerful capabilities for traffic control, security, and request processing—all before requests reach your backend application. Throughout this series, we’ll cover practical implementations including path protection, rate limiting, URL normalization, and advanced traffic filtering. This first article focuses on implementing HTTP Basic Authentication directly in VCL to protect specific paths like admin areas and staging environments.Documentation Index
Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
Use this file to discover all available pages before exploring further.
Why Protect Paths in Varnish?
Varnish Cache sits between your application and your users as a powerful HTTP accelerator. Beyond caching, it’s the perfect place to implement simple access controls for specific paths—blocking unauthorized requests before they consume backend resources. While Upsun offers environment-level HTTP access control, implementing authentication directly in VCL gives you more flexibility:- Path-specific protection: Secure only certain URLs (e.g.,
/admin,/staging) - Multiple credential sets: Use different passwords for different areas
- Custom logic: Combine authentication with other VCL conditions
- Edge enforcement: Block unauthorized traffic at the Varnish layer
Understanding HTTP Basic Authentication
HTTP Basic Authentication is a simple challenge-response mechanism where:- Client requests a protected resource
- Server responds with
401 Unauthorizedand aWWW-Authenticateheader - Browser displays a login prompt
- Client resends the request with an
Authorizationheader containing Base64-encoded credentials - Server validates credentials and either allows or denies access
Authorization header against known credentials and generating synthetic responses for authentication challenges.
Implementation
HTTP Basic Auth in VCL requires two parts: checking credentials invcl_recv and generating the authentication challenge in vcl_synth.
Part 1: Credential Verification in vcl_recv
Part 2: Authentication Challenge in vcl_synth
How It Works
-
Path Matching: The condition
if (req.url ~ "^/admin")identifies protected paths. The^anchor ensures only paths starting with/adminare restricted (e.g.,/admin,/admin/dashboard, but not/public/admin). -
Authorization Header Check:
if (!req.http.Authorization)verifies whether the client sent credentials. If not, we immediately return a401status, triggering the authentication challenge. -
Credential Validation: The second check validates the Base64-encoded credentials:
- HTTP Basic Auth sends credentials as
Authorization: Basic <base64-string> - The base64 string encodes
username:password - Example:
admin:adminbecomesYWRtaW46YWRtaW4= - Example:
username:passwordbecomesdXNlcm5hbWU6cGFzc3dvcmQ=
- HTTP Basic Auth sends credentials as
-
Multiple Allowed Credentials: The regex pattern
(YWRtaW46YWRtaW4=|dXNlcm5hbWU6cGFzc3dvcmQ=)allows multiple username/password combinations using alternation (OR logic). -
Synthetic Response: When authentication fails,
return (synth(401, "Authentication Required"))triggersvcl_synthwith a 401 status. -
WWW-Authenticate Header: In
vcl_synth, theWWW-Authenticateheader tells the browser to display the login prompt. Therealmparameter appears in the browser’s authentication dialog.
Generating Base64 Credentials
To create your own credential strings, use the DebugBear Basic Auth Header Generator or encode manually:Protecting Multiple Paths
You can protect different areas with different credentials:Security Considerations
Important: HTTP Basic Auth has several security limitations:- Credentials in VCL: The credentials are stored in plain text (Base64 is just encoding, not encryption) in your VCL configuration. Anyone with access to your VCL can decode them instantly.
- HTTPS Required: Always use HTTPS when deploying Basic Auth. Over HTTP, credentials are transmitted in the clear and can be intercepted.
-
Not for Primary Production User Auth: This approach is suitable for:
- Restricting admin areas with a shared password
- Adding a simple barrier to work-in-progress content
- Protecting staging environments from public access
- Securing internal APIs with simple tokens
- Production user authentication (use proper session-based auth in your application)
- Primary protection for sensitive customer data
- Any scenario requiring user-specific permissions or audit trails