在Ubuntu 18.04上使用apache配置清漆

时间:2018-12-06 19:25:24

标签: apache ubuntu caching varnish

我一直试图在ubuntu 18.04上安装Varnish。我已经使用过这篇文章https://hostadvice.com/how-to/how-to-setup-varnish-http-cache-on-an-ubuntu-18-04-vps-or-dedicated-server/

运行命令snapshot_event_entry后,但出现错误noeviction

/ etc / default / varnish

curl -I http://localhost

/etc/varnish/default.vcl

curl: (7) Failed to connect to localhost port 80: Connection refused

curl的结果-I http://localhost:8080

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

backend default { .host = "127.0.0.1"; .port = "80"; 的输出为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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

更新

基于新信息,问题是Varnish VCL配置。它缺少大量信息。

以下是根据您的设置修改的VCL示例

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);
}

以此替换 default.vcl 中的内容,然后重新启动Varnish。


没有任何服务正在监听localhost:80

再次检查Varnish是否正在运行

$ ps aux | grep varnish

如果Varnish正在运行,则必须检查Varnish配置

$ sudo cat /etc/default/varnish

应该打印类似

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

密钥是-a :80

还要确保您的Varnish service具有相同的端口配置

$ 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

然后尝试重新启动Varnish

$ sudo systemctl restart varnish

提示

您可以通过卷曲端口8080来验证Apache是​​否正常工作

$ curl -I http://localhost:8080

答案 1 :(得分:0)

您的配置一开始是错误的。

DAEMON_OPTS 中,您已将varnish设置为在端口80上侦听,而在default.vcl中,您正在配置varnish的后端,该后端应在相同的80端口上侦听。

这将导致一个无休止的循环。

在您的情况下,您想将apache设置为清漆的后端。将apache配置为在端口8080上运行后,您需要在 default.vcl

中反映出来
backend default {
.host = "127.0.0.1";
.port = "8080";
}

随后执行sudo service varnish restart