我的机器上运行着两个 Web 服务器。一个监听端口 8080,另一个监听端口 8081。
$ curl http://localhost:8080
I am the API
$ curl http://localhost:8081
<html> <head><title>Flat file</title></head>
<body><h1>I am the flat flile</h1></body> </body> </html>
我想设置一个中继,根据 HTTP 请求的请求路径将流量转发到其中一个或另一个。这是我的/etc/relayd.conf...
$ cat /etc/relayd.conf
api_host="127.0.0.1"
api_port="8080"
table <apihosts> {$api_host}
flat_host="127.0.0.1"
flat_ports="8081"
table <flathosts> {$flat_host}
log state changes
log connection
http protocol "chatty" {
pass request path "/api/*" forward to <apihosts>
pass request path "/*" forward to <flathosts>
}
relay "forum.cwal.net" {
listen on 0.0.0.0 port 80
protocol "chatty"
forward to <apihosts> port $api_port
forward to <flathosts> port $flat_ports
}
但是,似乎所有请求都发往端口 8081 上的服务器。
$ curl http://localhost
<html>
<head><title>Flat file</title></head>
<body><h1>I am the flat flile</h1></body>
</body>
$ curl http://localhost/api
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>404 Not Found</title>
<style type="text/css"><!--
body { background-color: white; color: black; font-family: 'Comic Sans MS', 'Chalkboard SE', 'Comic Neue', sans-serif; }
hr { border: 0; border-bottom: 1px dashed; }
--></style>
</head>
<body>
<h1>404 Not Found</h1>
<hr>
<address>OpenBSD httpd</address>
</body>
</html>
在最后一个请求中,我希望看到“我是 API”请求,但没有看到。
答案1
IRC 上有人为我解释了这一点。由于中继规则以“最后获胜”为基础,所以我需要将最具体的匹配结果放在最后。以下方法对我有用。
table <apihosts> { 127.0.0.1 }
table <flathosts> { 127.0.0.1 }
http protocol "PROTOX" {
match request path "/*" forward to <flathosts>
match request path "/api/*" forward to <apihosts>
}
relay "RELAYX" {
listen on 0.0.0.0 port 80
protocol "PROTOX"
forward to <apihosts> port 8080
forward to <flathosts> port 8081
}