Varnish cache, also known as a reverse caching proxy, is a web application accelerator designed for content-heavy dynamic websites. It is used to cache content in front of the web server and optimize web pages for faster loading times. It handles all inbound requests before they land on your web server’s backend.

In this post, we will show you how to set up the Varnish cache with Nginx on Arch Linux.

Step 1 – Configure Repository

By default, the default repository is outdated in Arch Linux, so you will need to modify the default mirror list if you have not done so already. You can do it by editing the mirrorlist configuration file:

nano  /etc/pacman.d/mirrorlist

Remove all lines and add the following lines:

## Score: 0.7, United States
Server = http://mirror.us.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.8, United States
Server = http://lug.mtu.edu/archlinux/$repo/os/$arch
Server = http://mirror.nl.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.9, United Kingdom
Server = http://mirror.bytemark.co.uk/archlinux/$repo/os/$arch
## Score: 1.5, United Kingdom
Server = http://mirrors.manchester.m247.com/arch-linux/$repo/os/$arch
Server = http://archlinux.dcc.fc.up.pt/$repo/os/$arch
## Score: 6.6, United States
Server = http://mirror.cs.pitt.edu/archlinux/$repo/os/$arch
## Score: 6.7, United States
Server = http://mirrors.acm.wpi.edu/archlinux/$repo/os/$arch
## Score: 6.8, United States
Server = http://ftp.osuosl.org/pub/archlinux/$repo/os/$arch
## Score: 7.1, India
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
## Score: 10.1, United States
Server = http://mirrors.xmission.com/archlinux/$repo/os/$arch

Save and close the file, then update all the package indexes with the following command:

pacman -Syu

Step 2 – Install Varnish Cache

By default, the Varnish package is included in the Arch Linux default repository. You can install it by running the following command.

pacman -S varnish

After the successful installation, start the Varnish service and enable it to start after the system reboot.

systemctl start varnish
systemctl enable varnish

By default, Varnish listens on port 6081. You can check it using the following command.

ss -antpl | grep 6081

You will get the following output.

LISTEN 0      0            0.0.0.0:6081       0.0.0.0:*    users:(("cache-main",pid=46376,fd=3),("varnishd",pid=46281,fd=3))
LISTEN 0      0                  *:6081             *:*    users:(("cache-main",pid=46376,fd=5),("varnishd",pid=46281,fd=5))

Step 3 – Install Nginx

Next, you will need to install the Nginx as a backend server. You can install it using the following command.

pacman -S nginx-mainline

Once installed, start and enable the Nginx service using the following command.

systemctl start nginx
systemctl enable nginx

By default, Nginx listens on port 80, so you will need to change the Nginx default port to 8080. You can change it by editing the Nginx main configuration file.

nano /etc/nginx/nginx.conf

Change the following line:

        listen       8080;

Save and close the file, then restart the Nginx to apply the changes.

systemctl restart nginx

Step 4 – Configure Varnish to Work With Nginx

Next, you will need to edit the Varnish service file and change port 6081 to 80.

nano /usr/lib/systemd/system/varnish.service

Find the following lines:

ExecStart=/usr/sbin/varnishd \
          -a :6081 \
          -a localhost:8443,PROXY \
          -p feature=+http2 \
          -f /etc/varnish/default.vcl \
          -s malloc,256m

Replace them with the following lines:

ExecStart=/usr/sbin/varnishd \
          -a :80 \
          -a localhost:8443,PROXY \
          -p feature=+http2 \
          -f /etc/varnish/default.vcl \
          -s malloc,256m

Save and close the file, then reload the systemd daemon to apply the changes.

systemctl daemon-reload

Next, restart the Varnish service using the following command.

systemctl restart varnish

Next, edit the Varnish configuration file and define your backend web server.

nano /etc/varnish/default.vcl

Change the following lines as per your Nginx backend server IP and port.

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Save and close the file when you are done.

Step 5 – Verify Varnish Cache

At this point, the Varnish cache is installed and configured as a front end for the Nginx web server. You can now check it using the curl command.

curl -I http://localhost

If everything is fine, you will get the following output.

HTTP/1.1 200 OK
Server: nginx/1.23.3
Date: Thu, 26 Jan 2023 14:41:04 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 13 Dec 2022 17:30:37 GMT
ETag: "6398b6bd-267"
X-Varnish: 5 3
Age: 3
Via: 1.1 archlinux (Varnish/7.2)
Accept-Ranges: bytes
Connection: keep-alive

You can also check the Varnish log to see the caching-related information.

varnishlog

You should see the following output.

-   RespHeader     Age: 13
-   RespHeader     Via: 1.1 archlinux (Varnish/7.2)
-   RespHeader     Accept-Ranges: bytes
-   VCL_call       DELIVER
-   VCL_return     deliver
-   Timestamp      Process: 1674744107.428200 0.000149 0.000149
-   RespProtocol   HTTP/1.1
-   RespStatus     304
-   RespReason     Not Modified
-   Filters        
-   RespUnset      Content-Length: 615
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1674744107.428263 0.000212 0.000063
-   ReqAcct        538 0 538 287 0 287
-   End            

*   << Session >> 11        
-   Begin          sess 0 HTTP/1
-   SessOpen       49.34.251.188 44732 a0 104.245.35.248 80 1674744107.418188 25
-   Link           req 12 rxreq
-   SessClose      RX_CLOSE_IDLE 5.016
-   End            

*   << Session >> 32771     
-   Begin          sess 0 HTTP/1
-   SessOpen       49.34.251.188 44730 a0 104.245.35.248 80 1674744107.512357 31
-   SessClose      RX_CLOSE_IDLE 5.005
-   End            

*   << Session >> 13        
-   Begin          sess 0 HTTP/1
-   SessOpen       49.34.251.188 44734 a0 104.245.35.248 80 1674744107.844468 32
-   SessClose      RX_CLOSE_IDLE 5.001
-   End            

Conclusion

Congratulations! You have successfully installed and configured the Varnish cache with Nginx on Arch Linux. You can now use Varnish cache as a front-end proxy server to speed up your web server performance. You can test the Varnish cache on dedicated server hosting from Atlantic.Net!