R
Railwayβ€’14mo ago
Rem

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

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}
}
{
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
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
9 Replies
Percy
Percyβ€’14mo ago
Project ID: 1aa4380a-1423-4392-a56a-2d49595fe43e
Rem
RemOPβ€’14mo ago
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
2023/10/04 09:34:05.748 ERROR http.log.error.log0 dial tcp [my ipv6]:3000: connect: connection refused
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
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
Brody
Brodyβ€’14mo ago
what type of app is the backend/api ?
Rem
RemOPβ€’14mo ago
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
}
{ # 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
FRONTEND_HOST=dashboard:3000
BACKEND_HOST=api:3000
For other people with a similar issue ^
Brody
Brodyβ€’14mo ago
haha glad you could solve, now its closer to the original caddyfile i wrote
Rem
RemOPβ€’14mo ago
Yes, though I think the main issue were the ENV vars, as I tried countless caddyfile variations πŸ˜„ It wouldn't work with http(s):// and/or railway.internal in there
Brody
Brodyβ€’14mo ago
it would have, you just left out the /api/* part
Rem
RemOPβ€’14mo ago
I tried redirecting all traffic to the api instead, which also only would work without railway.internal. Don't ask me why 😦
Brody
Brodyβ€’14mo ago
sounds good
Want results from more Discord servers?
Add your server