Varnish Cache Setup for Liferay on Nginx

Objective:

  • Implement Varnish Cache as a reverse proxy for a Liferay instance running on Nginx to enhance performance.

Prerequisites:

  • Ubuntu-based server with Nginx and Liferay installed.

1. Install Nginx:

sudo apt update
sudo apt install nginx

2. Start and Enable Nginx:

sudo systemctl start nginx 
sudo systemctl enable nginx

3. Verify Nginx Status:

sudo systemctl status nginx

4. Update Nginx Configuration:

cd /etc/nginx/sites-available
vim default

Add the following proxy_pass configuration inside the location / block:

location / {
    proxy_pass http://your_IP:port;
}

5. Restart Nginx:

sudo systemctl restart nginx

 

Nginx Config

 

Installing and Configuring Varnish Cache

1. Install Varnish:

sudo apt install varnish

2. Start and Enable Varnish:

sudo systemctl start varnish
sudo systemctl enable varnish

3. Update Nginx Configuration for Varnish:

cd /etc/nginx/sites-available/
vim default

4. Change the default Nginx port from 80 to 8080:

server {
    listen 8080 default_server;
    listen [::]:8080 default_server;
    # Other configurations...
}

5. Reload Nginx:

sudo systemctl reload nginx

6. Allow Port 8080 (if firewall is enabled):

sudo ufw allow 8080

7. Configure Varnish Port:

vim /etc/default/varnish

8. Change DEAMON_OPTS to use port 80:

DEAMON_OPTS="-a :80"

9. Update Varnish Service Configuration:

vim /etc/systemd/system/varnish.service

10. Update ExecStart to use port 80 and 6081:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f
/etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

11. Reload Varnish Service:

systemctl daemon-reload
systemctl restart varnish

12. Verify Configuration:

netstat -plntu

13. Check that Varnish is running on port 80, and Nginx on port 8080:

sudo service nginx restart
netstat -plntu 

14. Verify Varnish Cache:

Access Liferay on the browser using the VM OS IP without specifying a port. Inspect the Liferay page network column to confirm Varnish caching

 

Varnish Cache

Blogs

Great blog, Sahir!

For Varnish configuration options, check my post: https://liferay.dev/blogs/-/blogs/increasing-capacity-and-decreasing-response-times-using-a-tool-you-re-probably-not-familiar-with

It likely could use some VCL file updates to deal with /o/api and other /o paths, but even now it's probably a great starting point for customizing your own VCL setup.