【发布时间】:2023-01-09 13:20:06
【问题描述】:
我很难接受这个设置。我有一个 node.js 框在 3000 上提供 HTTP,在 3001 上提供 websockets,在 3002 上提供安全的 websockets。在它前面,我在自己的服务器上有一个远程 Hitch/Varnish 缓存代理,它正在侦听 443/80 并连接第一个服务器通过 3000 作为其默认后端。访问网站 URL https://foo.tld 的用户点击 varnish 代理并看到该网站,网站上的一些 javascript 告诉他们的浏览器连接到 wss://foo.tld:3002 以确保安全网络套接字。
我的问题是让 websockets 透明地传递到后端。在 VCL 我有标准
if (req.http.upgrade ~ "(?i)websocket") {
return (pipe);
}
和
sub vcl_pipe {
#Declare pipe handler for websockets
if (req.http.upgrade) {
set bereq.http.upgrade = req.http.upgrade;
set bereq.http.connection = req.http.connection;
}
}
在这种情况下这是行不通的。列出我到目前为止没有成功的尝试:
1:在名为“websockets”的 VCL 中创建第二个后端,它是相同的后端 IP,但在端口 3001 或 3002 上,并添加“set req.backend_hint = websockets;”在上面第一个 sn-p 中的管道召唤之前。
2:关闭 HTTPS 并尝试通过纯 HTTP 连接它。
3:修改varnish.service 来尝试让varnish 监听除-a :80 和-a :8443,proxy 之外的端口,在这种情况下varnish 只是拒绝启动。一种尝试是仅使用 HTTP 并尝试在 3001 上运行 varnish 以使 ws:// 在没有 SSL 的情况下工作,但 varnish 拒绝启动。
4:最近我在 VCL 中尝试了以下操作来尝试获取来自 3001 的客户端连接:
if (std.port(server.ip) == 3001) {
set req.backend_hint = websockets;
}
我的目标是让 Varnish box 在 3002 上获取安全的 websocket 流量(wss://)(通过使用普通安全 websocket 连接协议的 443 连接)并将其透明地传递到后端 websocket 服务器,无论 SSL 是否加密连接的腿或不。我之前已经设置了其他像这样的小型服务器,如果 Varnish 和后端服务在同一台机器上或在像 Cloudflare 这样的监管 CDN 后面,让 websockets 工作是微不足道的,所以试图弄清楚这是什么特别令人沮丧远程代理设置需要。我觉得部分解决方案是让 Varnish 或 Hitch(不确定)监听 3002 以接受连接,此时正常的 req.http.upgrade 和管道功能将发挥作用,但软件拒绝合作。
我当前的 hitch.conf:
frontend = "[*]:443"
frontend = "[*]:3001"
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
syslog = on
log-level = 1
# Add pem files to this directory
# pem-dir = "/etc/pki/tls/private"
pem-file = "/redacted/hitch-bundle.pem"
当前默认.vcl:
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "remote.server.ip";
.port = "8080";
}
backend websockets {
.host = "remote.server.ip";
.port = "6081";
}
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
#Allow websockets to pass through the cache (summons pipe handler below)
if (req.http.Upgrade ~ "(?i)websocket") {
set req.backend_hint = websockets;
return (pipe);
} else {
set req.backend_hint = default;
}
}
sub vcl_pipe {
if (req.http.upgrade) {
set bereq.http.upgrade = req.http.upgrade;
set bereq.http.connection = req.http.connection;
}
return (pipe);
}
【问题讨论】:
标签: ssl caching websocket proxy varnish