skip to Main Content

I have been trying to install Varnish on ubuntu 18.04. I have used this article https://hostadvice.com/how-to/how-to-setup-varnish-http-cache-on-an-ubuntu-18-04-vps-or-dedicated-server/,

after I run the command curl -I http://localhost but I get the error curl: (7) Failed to connect to localhost port 80: Connection refused.

/etc/default/varnish

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

/etc/varnish/default.vcl

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

Results of curl -I http://localhost:8080

curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: Apache/2.4.25 (Debian)
ETag: "29cd-56dff9168052e"
Accept-Ranges: bytes
Content-Length: 10701
Vary: Accept-Encoding
Content-Type: text/html

the output of sudo service varnich restart is Failed to restart varnich.service: Unit varnich.service not found.

how can I resolve this problem ?

2

Answers


  1. Update

    Based on the new info the problem is the Varnish VCL config. It’s missing alot of information.

    Here is an example VCL modified according to your setup

    vcl 4.0;
    
    backend default {
        .host = "127.0.0.1";
        .port = "8080";
    }
    
    sub vcl_recv {
        if (req.method == "PRI") {
        /* We do not support SPDY or HTTP/2.0 */
        return (synth(405));
        }
        if (req.method != "GET" &&
          req.method != "HEAD" &&
          req.method != "PUT" &&
          req.method != "POST" &&
          req.method != "TRACE" &&
          req.method != "OPTIONS" &&
          req.method != "DELETE") {
            /* Non-RFC2616 or CONNECT which is weird. */
            return (pipe);
        }
    
        if (req.method != "GET" && req.method != "HEAD") {
            /* We only deal with GET and HEAD by default */
            return (pass);
        }
        if (req.http.Authorization || req.http.Cookie) {
            /* Not cacheable by default */
            return (pass);
        }
        return (hash);
    }
    
    sub vcl_pipe {
        # By default Connection: close is set on all piped requests, to stop
        # connection reuse from sending future requests directly to the
        # (potentially) wrong backend. If you do want this to happen, you can undo
        # it here.
        # unset bereq.http.connection;
        return (pipe);
    }
    
    sub vcl_pass {
        return (fetch);
    }
    
    sub vcl_hash {
        hash_data(req.url);
        if (req.http.host) {
            hash_data(req.http.host);
        } else {
            hash_data(server.ip);
        }
        return (lookup);
    }
    
    sub vcl_purge {
        return (synth(200, "Purged"));
    }
    
    sub vcl_hit {
        if (obj.ttl >= 0s) {
            // A pure unadultered hit, deliver it
            return (deliver);
        }
        if (obj.ttl + obj.grace > 0s) {
            // Object is in grace, deliver it
            // Automatically triggers a background fetch
            return (deliver);
        }
        // fetch & deliver once we get the result
        return (miss);
    }
    
    sub vcl_miss {
        return (fetch);
    }
    
    sub vcl_deliver {
        return (deliver);
    }
    
    /*
     * We can come here "invisibly" with the following errors: 413, 417 & 503
     */
    sub vcl_synth {
        set resp.http.Content-Type = "text/html; charset=utf-8";
        set resp.http.Retry-After = "5";
        synthetic( {"<!DOCTYPE html>
    <html>
      <head>
        <title>"} + resp.status + " " + resp.reason + {"</title>
      </head>
      <body>
        <h1>Error "} + resp.status + " " + resp.reason + {"</h1>
        <p>"} + resp.reason + {"</p>
        <h3>Guru Meditation:</h3>
        <p>XID: "} + req.xid + {"</p>
        <hr>
        <p>Varnish cache server</p>
      </body>
    </html>
    "} );
        return (deliver);
    }
    
    #######################################################################
    # Backend Fetch
    
    sub vcl_backend_fetch {
        return (fetch);
    }
    
    sub vcl_backend_response {
        if (beresp.ttl <= 0s ||
          beresp.http.Set-Cookie ||
          beresp.http.Surrogate-control ~ "no-store" ||
          (!beresp.http.Surrogate-Control &&
            beresp.http.Cache-Control ~ "no-cache|no-store|private") ||
          beresp.http.Vary == "*") {
            /*
            * Mark as "Hit-For-Pass" for the next 2 minutes
            */
            set beresp.ttl = 120s;
            set beresp.uncacheable = true;
        }
        return (deliver);
    }
    
    sub vcl_backend_error {
        set beresp.http.Content-Type = "text/html; charset=utf-8";
        set beresp.http.Retry-After = "5";
        synthetic( {"<!DOCTYPE html>
    <html>
      <head>
        <title>"} + beresp.status + " " + beresp.reason + {"</title>
      </head>
      <body>
        <h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
        <p>"} + beresp.reason + {"</p>
        <h3>Guru Meditation:</h3>
        <p>XID: "} + bereq.xid + {"</p>
        <hr>
        <p>Varnish cache server</p>
      </body>
    </html>
    "} );
        return (deliver);
    }
    
    #######################################################################
    # Housekeeping
    
    sub vcl_init {
    }
    
    sub vcl_fini {
        return (ok);
    }
    

    Replace the contents in your default.vcl with this and then restart Varnish.


    There isn’t any service listening on localhost:80

    Double check that Varnish is running

    $ ps aux | grep varnish
    

    If Varnish is running then you have to check the Varnish config

    $ sudo cat /etc/default/varnish
    

    Should print something like

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

    Where the key is -a :80

    Also make sure that your Varnish service have the same port config

    $ sudo nano /lib/systemd/system/varnish.service
    
    [Unit]
    Description=Varnish HTTP accelerator
    Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd
    [Service]
    Type=simple
    LimitNOFILE=131072
    LimitMEMLOCK=82000
    ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f$
    ExecReload=/usr/share/varnish/varnishreload
    ProtectSystem=full
    ProtectHome=true
    PrivateTmp=true
    PrivateDevices=true
    [Install]
    WantedBy=multi-user.target
    

    Then try to restart Varnish

    $ sudo systemctl restart varnish
    

    Tip

    You can verify that Apache is working by curling on port 8080

    $ curl -I http://localhost:8080
    
    Login or Signup to reply.
  2. Your configuration is faulty to begin with.

    In the DAEMON_OPTS you’ve set varnish to listen on port 80, while in the default.vcl you’re configuring a backend for varnish which should listen on the same 80 port.

    This will lead into a neverending loop.

    In your case, you want to set apache as a backend for varnish. As you’ve configured apache to run on port 8080, you’ll need to reflect this in the default.vcl

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

    Do a sudo service varnish restart afterwards.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search