3

I have two web servers running on my machine. One is listening on port 8080 and the other on 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>

I want to setup a relay that forwards traffic to one or the other based on the request path of an HTTP request. Here is my /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
}

However, it seems like all requests are going to the server on port 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>

On that last request, I expect to see the "I am the API" request but I'm not.

1
  • I don't know relayd, but: What do you get with curl http://localhost/api/ and curl http://localhost:8080/api/? Jul 14, 2020 at 7:15

1 Answer 1

3

Somebody on IRC explained it for me. Because relayd rules operate on a "last wins" basis, I need my most specific match to appear last. The following works for me.

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
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .