502 Awaken Internal Sleepy Apps w/ Caddy
davidachris
FREEOP

6 months ago

Hello!

If you’re using Railway to host your backend and Caddy as a reverse proxy, you might have run into 502 errors when your backend is asleep.

This happens because Railway’s serverless backends can take a few seconds to wake up after a period of inactivity.

By default, Caddy will try your backend once and return a 502 if it doesn’t respond quickly enough.

To fix this, you can configure Caddy to retry the backend for a set period, giving Railway time to wake up your service. Just add the lb_try_duration and lb_try_interval options to your reverse proxy configuration:

{
	admin off # there's 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
	# runtime logs
	log {
		format json # set runtime log format to json mode
	}
	# server options
	servers {
		trusted_proxies static private_ranges 100.0.0.0/8 # trust railway's proxy
	}
}

:{$PORT} {
	@app host www.domain.com
	handle @app {
		redir https://domain.com{uri} 308
	}

	@api host api.domain.com
	handle @api {
		reverse_proxy internal-api:3000 {
			lb_try_duration 30s
			lb_try_interval 1s
		}
	}
}

This tells Caddy to keep retrying for up to 30 seconds, checking every 1 second. Adjust the values as needed for your use case.

Happy Building!

P.S. While you’re here, check out RailQ, an in-memory, SQS like queueing service written in Go for minimal memory footprint.

Solved

1 Replies

Railway
BOT

6 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


Status changed to Solved davidachris 6 months ago


Loading...