На моей машине работают два веб-сервера. Один прослушивает порт 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», но это не так.
Кто-то из 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
}