#SOLVED: caddy reverse proxy error 502 (can't find host)

13 messages · Page 1 of 1 (latest)

rain scarab
#

Hi,
I am trying to setup a caddy reverse proxy.

caddyfile

{
    admin off
    persist_config off
    auto_https off
    log {
        format console
    }
    servers {
        trusted_proxies static private_ranges
    }
}

:{$PORT} {
    log {
        format console
    }

    reverse_proxy {$FRONTEND_HOST}
    reverse_proxy {$BACKEND_HOST}
}

proxy env vars:

FRONTEND_HOST=http://dashboard.railway.internal:3000
BACKEND_HOST=http://api.railway.internal:3000

My main focus now is the API
the API is listening to :::3000 (this is working)

When I go to my domain /api
I get error 502

foggy lynxBOT
#

Project ID: 1aa4380a-1423-4392-a56a-2d49595fe43e

weak brookBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

rain scarab
#

1aa4380a-1423-4392-a56a-2d49595fe43e

#

2023/10/04 09:34:05.748 ERROR http.log.error.log0 dial tcp [my ipv6]:3000: connect: connection refused

rain scarab
#

with Nginx i get this

2023/10/04 10:22:50 [emerg] 1#1: host not found in upstream "api.railway.internal" in /etc/nginx/nginx.conf:13

nginx: [emerg] host not found in upstream "api.railway.internal" in /etc/nginx/nginx.conf:13

next tiger
#

what type of app is the backend/api ?

rain scarab
#

It's expressjs (and react)
However, I managed to get it working with this caddy config:

{ # global options
    admin off # theres no need for the admin api in railway's environment
    persist_config off # storage isn't persistent anyway
    auto_https off # railway handles https for us, this would cause issues if left enabled
    log { # runtime logs
        format console # set runtime log format to console mode 
    }
    servers { # server options
        trusted_proxies static private_ranges # trust railway's proxy
    }
}

:{$PORT} { # site block, listens on the $PORT environment variable, automatically assigned by railway
    log { # access logs
        format console # set access log format to console mode
    }

    reverse_proxy {$FRONTEND_HOST} # proxy all requests for /* to the frontend, configure this variable in the service settings

    # Handle /api/* without stripping the prefix
    reverse_proxy /api/* {$BACKEND_HOST} # proxy all requests for /api/* to the backend, configure this variable in the service settings
}

With these env vars:

FRONTEND_HOST=dashboard:3000
BACKEND_HOST=api:3000

For other people with a similar issue ^

#

SOLVED: caddy reverse proxy error 502 (can't find host)

next tiger
#

haha glad you could solve, now its closer to the original caddyfile i wrote

rain scarab
next tiger
#

it would have, you just left out the /api/* part

rain scarab
#

I tried redirecting all traffic to the api instead, which also only would work without railway.internal. Don't ask me why 😦