ubuntu - 来自服务器的空回复,在单独的缓存服务器上带有 Varnish 和挂接

标签 ubuntu networking server varnish virtualmin

在 ubuntu 20.04 服务器上安装 Varnish 和挂接后,出现以下错误:

curl: (52) Empty reply from server


我正在关注的教程:
[https://docs.varnish-software.com/tutorials/hitch-letsencrypt/][1]
就我而言,我有两台服务器 s1 和 s2 都安装了 ubuntu 20.04 服务器。
s1的本地静态ip:

192.168.1.105


s2的本地静态ip:

192.168.1.106


在 s1 virtualmin 面板上已安装并且一切正常。所以我
决定制作一个单独的缓存服务器并安装 Varnish 和 Hook :
sudo apt install varnish -y 

sudo apt install hitch -y
default.vcl 文件中的更改:
    backend default {
    .host = "192.168.1.105";
    .port = "80";

   }
hitch.conf:
# Run 'man hitch.conf' for a description of all options.
    frontend = {
        host = "*"
        port = "443"
    }
    backend = "[127.0.0.1]:8443"    # 6086 is the default Varnish PROXY port.
#workers = 4                     # number of CPU cores

#daemon = on

# We strongly recommend you create a separate non-privileged hitch
# user and group
#user = "hitch"
#group = "hitch"

# Enable to let clients negotiate HTTP/2 with ALPN. (default off)
# alpn-protos = "h2, http/1.1"

# run Varnish as backend over PROXY; varnishd -a :80 -a localhost:6086,PROXY ..
#write-proxy-v2 = on             # Write PROXY header

#pem files

    pem-file = "/etc/letsencrypt/live/example.com/hitch-bundle.pem"

unit file:

    [Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/6.1/ man:varnishd

    [Service]
    Type=simple
    LimitNOFILE=131072
    LimitMEMLOCK=infinity
    ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -a localhost:8443,proxy -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,3G
    ExecReload=/usr/share/varnish/varnishreload
    ProtectSystem=full
    ProtectHome=true
    PrivateTmp=true
    PrivateDevices=true

    [Install]
    WantedBy=multi-user.target
注:我对网络一无所知,所以主路由器与第二个路由器相连,而 s1 和 s2 都直接与第二个路由器相连。
dmz 主机 ip 也从 s1 ip 更改为 s2 ip。
结果:
curl -i https://example.com
curl: (52) Empty reply from server.
根据这个命令
varnishlog -g request -q "ReqUrl eq '/'"
VSM:无法获取 varnishd,它正在运行吗?
请指导谢谢。

最佳答案

通过启用 PROXY 协议(protocol)修复空回复
在您的 hitch.conf 中取消注释以下行:

#write-proxy-v2 = on
这将允许使用 PROXY 协议(protocol)与 Varnish 进行通信。该协议(protocol)通过 TLV 属性将有关原始客户端(与 Hitch 连接的客户端)的更多信息传递给 Varnish。
您正在连接到端口 8443来自 Hitch,它是 Varnish 的 PROXY 协议(protocol)端口,如下文所述 varnishd运行时参数:
-a localhost:8443,proxy
现在 Varnish 期望代理信息出现在 TCP 数据包中,但找不到该信息。因此,空的答复。
让 Varnish 协议(protocol)感知
如果你的应用程序强制 HTTPS 重定向,重要的是让 Varnish 知道所使用的协议(protocol)。它将是 HTTP 或 HTTPS。
如果您通过 PROXY 协议(protocol)使用 Hitch,我们可以使用 vmod_proxy识别这些连接并创建适当的缓存变体。
这是VCL代码:
vcl 4.1;

import proxy;

sub vcl_recv {
    if (proxy.is_ssl()) {
        set req.http.X-Forwarded-Proto = "https";
    } else {
        set req.http.X-Forwarded-Proto = "http";
    }
}

sub vcl_backend_response {
    if(beresp.http.Vary) {
        if(beresp.http.Vary !~ "X-Forwarded-Proto") {
            set beresp.http.Vary = set beresp.http.Vary + ", X-Forwarded-Proto";
        }
    } else {
        set beresp.http.Vary = "X-Forwarded-Proto";
    }
}
此代码将存储每个响应的 HTTP 版本和 HTTPS 版本。
奖励:启用 HTTP/2
当您使用它时,您也可以通过取消注释 hitch.conf 中的以下行来启用 HTTP/2。文件:
# alpn-protos = "h2, http/1.1"
为确保在 Varnish 中启用 HTTP/2,请将以下运行时参数添加到 varnishd在您的 systemd 配置中:
-p feature=+http2
重新启动 Hitch & Varnish 和 Hitch 将能够终止 HTTP/2 请求的 TLS。

关于ubuntu - 来自服务器的空回复,在单独的缓存服务器上带有 Varnish 和挂接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70656894/

相关文章:

java - 在java TCP连接中绑定(bind)一个接口(interface)

php - Debian 8 多个 PHP 是这些安全的方法吗?

php - file_get_contents(https ://www. google.com/recaptcha/api/siteverify):无法打开流:连接超时

linux - 为什么 LXC 由于 "Too many levels of symbolic links"而无法启动,但在第二次或第三次尝试时启动?

ubuntu - 在 Vagrant VM 中意外修改 authorised_keys 后无法登录

linux - 如何在 Linux 16.04 上更新 Sublime Text

PHP Session 随机死亡

haskell - 如何让 Haskell 的 Network.Browser 进行 gzip 压缩?

linux - 如何捕获每个 PID 的网络数据包?

javascript - Node.js 在错误的文件路径上崩溃