客户端 443/HTTPS -> 虚拟 IP -> HA 代理 80/HTTP 和 443/HTTPS SSL 终端 -> Varnish 81/HTTP -> 82/HTTPS 和 442/HTTPS Apache

问题描述 投票:0回答:1

在家里,我有 1 台托管服务器,其中包含一些网站,使用 Virtualmin(很棒的工具!),一切都可以正常使用 HTTPS、HTTP...甚至在我的互联网路由器后面,使用 NAT 将端口 80 和 443 转发到我的 Web 服务器.

客户端 443/HTTPS -> 我的互联网路由器 NAT -> 80/HTTP 或 443/HTTPS Apache

我正在通过在 Apache 前面添加负载均衡器 HA 代理来进行一些改进:工作正常。

客户端 443/HTTPS -> 我的互联网路由器 NAT -> HA 代理 443/HTTPS SSL 终端 -> 82/HTTP 或 442/HTTPS Apache (192.168.0.40)

这意味着我的网站仍然可以通过 HTTPS 协议访问,但端口为 442

我什至添加了 keepalived 并且工作正常:

客户端 443/HTTPS -> 我的互联网路由器 NAT -> 保持活动 IP 地址 -> HA 代理 443/HTTPS SSL 终端 -> 82/HTTP 或 442/HTTPS Apache

现在,我想添加 Web Caching Varnish。如您所知,Varnish 的免费版本不支持 HTTPS。这对我来说不是问题,因为我有处理 SSL 终止的 HA 代理。 我想做的是将 Varnish 设置在 HA Proxy 后面、Apache 前面。

客户端 443/HTTPS -> 我的互联网路由器 NAT -> 保持活动 IP 地址 -> HA 代理 443/HTTPS SSL 终止 -> Varnish 81/HTTP -> 82/HTTP 或 442/HTTPS Apache

但是这不起作用... 当我尝试使用 HTTPS 访问我的网站时,例如 https://myexemple.com,主页已加载,但所有其他静态文件未加载... 查看页面网络检查器(从浏览器中按 F12),我可以看到这些静态文件是使用 HTTP 提供的(请参阅随附的屏幕截图)。

enter image description here

刚刚为此网站安装了wordpress,没有其他。

是否有人有类似的问题,或者是否成功实现了像我这样的架构模式:

客户端 443/HTTPS -> 保持活动 IP 地址 -> HA 代理 443/HTTPS SSL 终止 -> Varnish 81/HTTP -> 82/HTTP 或 442/HTTPS Apache

一般来说,使用 Varnish 时如何处理 HTTPS? 特别是对于 Wordpress:在后端,我们必须指定网站的 URL(即在我的例子中为 https://kmx.ovh)? 谢谢你

好吧,我尝试在 HA 代理后面和我的 Apache 网站前面设置 Varnish,请参阅下面的配置:

这是我的 HA 代理的配置(非常简单):

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
        tune.ssl.default-dh-param 4096

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        option http-server-close
        timeout connect 5s
        timeout client  50s
        timeout server  50s
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http


frontend https
        description "HA Proxy"
        bind *:80
        bind *:443 ssl crt /etc/haproxy/cert/
        mode http
        option httplog
        option forwardfor except 127.0.0.1
        http-request set-header X-Forwarded-Proto https
        http-response set-header MON-LOADBALANCER "HTTPS HA Proxy 40"
        default_backend varnish_pool

backend varnish_pool
        description 'Varnish backends pool'
        balance roundrobin
        mode http
        http-response set-header X-Frame-Options DENY
        http-response set-header X-XSS-Protection 1;mode=block
        http-response set-header X-Content-Type-Options nosniff
        http-response set-header Referrer-Policy no-referrer-when-downgrade
        default-server inter 15s fall 3 rise 2
        option httpchk GET / HTTP/1.1
        http-check expect status 200
        http-request set-header X-Forwarded-Port %[dst_port]
        server 40-80 192.168.0.40:81

这是我的 Varnish 配置文件(非常基本......):

vcl 4.1;

backend default {
    .host = "192.168.0.40";
    .port = "82";
}

sub vcl_recv {
    set req.backend_hint = default;
    set req.http.Host = req.http.host;
}

sub vcl_backend_response {
    set beresp.http.MonX-Cache = "via Varnish cache 40";
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
        set resp.http.X-Cache-Hits = obj.hits;
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

还有清漆服务:

[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/ man:varnishd

[Service]
Type=simple

# Maximum number of open files (for ulimit -n)
LimitNOFILE=131072

# Locked shared memory - should suffice to lock the shared memory log
# (varnishd -l argument)
# Default log size is 80MB vsl + 1M vsm + header -> 82MB
# unit is bytes
LimitMEMLOCK=85983232
ExecStart=/usr/sbin/varnishd \
          -j unix,user=vcache \
          -F \
          -a :81 \
          -T localhost:6082 \
          -f /etc/varnish/default.vcl \
          -S /etc/varnish/secret \
          -s malloc,256m
ExecReload=/usr/share/varnish/varnishreload
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true

[Install]
WantedBy=multi-user.target

haproxy varnish
1个回答
0
投票

我建议您将以下行添加到您的 HAProxy 后端配置中:

http-request add-header X-Forwarded-Proto https if { ssl_fc }

虽然您已经添加了

X-Forwarded-Port
标头,但
X-Forwarded-Proto
标头是 URL 构建更受支持的标头。

直接向最终用户公开的 Web 应用程序知道客户端 URL 方案(HTTP 或 HTTPS) 是什么,并相应地构建 URL。

当坐在缓存代理后面时,现代 Web 应用程序还支持

X-Forwarded-Proto
来了解它们需要使用哪种 URL 方案。尽管 Varnish 只向您的应用程序发送纯 HTTP 请求,但初始连接可能是通过 HTTPS 进行的。

这就是您需要

X-Forwarded-Proto
的原因。为了确保 Varnish 分别缓存页面的 HTTP 和 HTTPS 版本,您必须将
Vary: X-Forwarded-Proto
响应标头添加到应用程序返回的 HTTP 响应中。

© www.soinside.com 2019 - 2024. All rights reserved.