Home > Net >  Varnish Docker image not working for node js On localhost
Varnish Docker image not working for node js On localhost

Time:07-13

I am trying to implement Varnish for a small small node js server

(index.js)
const port = 80;
require("http").createServer((req, res) => {
    res.write(new Date().toISOString());
    res.end();
}).listen(port, () => {
    console.log(`http://127.0.0.1:${port}/`);
})
(default.vcl)
vcl 4.1;

backend default {
    .host = "127.0.0.1";
    .port = "80";
}
(CMD)
//now I run docker  with following commands
docker run  --name varnish -p 8080:80   -e VARNISH_SIZE=2G  varnish:stable
docker cp default.vcl varnish:/etc/varnish 
(Followed By restart container)

But All i see is following error:

Error 503 Backend fetch failed Backend fetch failed

Guru Meditation: XID: 31

Varnish cache server

CodePudding user response:

You have a problem in your varnish configuration. You have set:

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

But 127.0.0.1 (or localhost) means "this container", and your backend is not running inside the same container as Varnish. If your node.js server is running on your host, you probably want to do something like this:

vcl 4.1;

backend default {
    .host = "host.docker.internal";
    .port = "80";
}

And then start the container like this:

docker run  --name varnish -p 8080:80 --add-host=host.docker.internal:host-gateway  -e VARNISH_SIZE=2G  varnish:stable

This maps the hostname host.docker.internal to mean "the host on which Docker is running".


If your node.js is running in another container, the solution is going to look a little different.

  • Related