How to fix problem to change port for varnish cache

Although you changed the port in the varnish configuration file:

sudo nano /etc/default/varnish
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"


sudo nano /etc/varnish/default.vcl
backend default {
    .host = "";
    .port = "8080";
    .first_byte_timeout = 600s;

However the varnish still doesn’t change the port and if you check, you will see it like:

ps aux | grep varnish


/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secred -s malloc,256m

It still listen on the port 6081 and 6082. Why it happens? Because the varnish cache┬ádidn’t take our new configuration. It still run with old configuration.┬áNormally, the varnish cache /etc/init.d/varnish script will read the file /etc/default/varnish. However the the systemd init script (/lib/systemd/system/varnish.service) doesn’t read this configuration.

The solution: You have to do is override the systemd init script of varnish:

sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/
sudo nano /etc/systemd/system/varnish.service

And change from

ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m


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

Now, you can restart varinish cache to check it again:

systemctl daemon-reload
service varnish restart


Add Comment

Required fields are marked *. Your email address will not be published.