HTTP 502 Bad Gateway using the $PORT variable assigned by Railway + other service reference $PORT
I have a next.js front end deployed as a railway project, when I set the $PORT variable to 3000, the service starts and works fine. If I remove the $PORT variable and just rely on the $PORT provided by railway, the project builds fine, but when I try to access the public url, I get a 502 Bad Gateway error.
Also, my front end connects to a backend service in the same project, if that backend also uses the provided $PORT from railway, can the frontend access it by setting its API_URL env var to "http://${{backend.RAILWAY_PRIVATE_DOMAIN}}:${{backend.PORT}}"?
42 Replies
Project ID:
bffee644-ec55-4122-9e19-fee98c401647
Project ID bffee644-ec55-4122-9e19-fee98c401647
please see our docs page - https://docs.railway.app/guides/fixing-common-errors#application-failed-to-respond
I am not using target ports, but I must've missed the example for node/next. Do I need to set the address to listen on as "0.0.0.0" in next too?
Also, what about using the reference variables for the API_URL?
next by default should listen on the correct host, and how do you know you aren't using target ports?
as for crafting a API URL out of reference variables - https://docs.railway.app/guides/private-networking#use-internal-hostname-and-port
I just started the project with $ next start --port ${PORT-3000}, and I'm still getting the 502 bad gateway. I don't believe I'm using target ports since I'm not seeing that option in configuring the public network addresses
This is the url to the deployment: https://railway.app/project/bffee644-ec55-4122-9e19-fee98c401647/service/05cba2ee-b344-4910-b0de-626add3247f7?id=3f44da7f-8890-47f5-9017-07222547dd91#details
Railway
Railway
Railway is an infrastructure platform where you can provision infrastructure, develop with that infrastructure locally, and then deploy to the cloud.
show me a screenshot of your public domain please
or you mean the public domain config?
how do you know that is the start command that railway is using?
I see it in the deploy logs
app:start: $ next start --port ${PORT-3000}
app:start: ▲ Next.js 14.1.0
app:start: - Local: http://localhost:3000
it looks like it didn't use the $PORT injected by railway, and used port 3000 the fallback, but now the service is trying to connect to the $PORT and it's not working, but I'm just guessingI see localhost, not 0.0.0.0
that's the output from next start. Right, it's localhost, it's just the default I guess from next because I'm not setting the host anywhere in code
I'm adding --hostname 0.0.0.0 to the next start command and redeploying
While that's going, I used the ${{backend.PORT}} var reference, but it doesn't show the autocomplete for it, so I wasn't sure if it's possible to do that, even though I read the docs
have you set the PORT variable?
I have not, I was going to rely on the injected value from railway
app:start: $ next start --hostname 0.0.0.0 --port ${PORT-3000}
app:start: ▲ Next.js 14.1.0
app:start: - Local: http://localhost:3000
app:start: - Network: http://0.0.0.0:3000
But I'm still getting the 502 Bad gatewayit's injected into the runtime, not your own service variables
go ahead and set the port variable
ah ok, so if I want to reference ${{backend.PORT}} from frontend service, I have to set it on the backend service first, can't use the injected one
set the frontend.PORT to 3000?
right, you can't reference a variable that doesn't exist
on this next service, set a PORT variable to 3000
If I set the variable, it will work, I had it working with that config
awesome
I was just trying to follow the docs and let the service use the injected $PORT 🙂
I know, but something is missconfigured in your app and it's not listening on it, so this is easier
perfect, now the build is hanging haha
build is stuck for the past 6 minutes at
#7 [stage-0 5/11] COPY . /app/.
it usually build and deploys in 2-3 minsfeel free to cancel it
it rebuilt in 1 minute, deployed and back to working, having specified $PORT as 3000 in the env vars
awsome
can I ask a question about redis storage or should I open up another question? I was just curious if that's also expandable to 10gb without adding a persistent volume?
you need to use a volume with redis, what plan are you on
Pro plan
then you can go up to 250GB
(you dont pay for unused storage)
but I need to add a volume to the redis service? right now I don't have a volume backing it
ah so you currently dont care if your redis data persists?
not for dev env, but it's so little data that it shouldn't matter
okay because i was about to say you have a volume
for production project
so that's showing that the redis volume is using 1gb of storage, but the redis cache is pretty much empty
yep so you will pay for 1GB of storage
I'm just wondering why it's using 1gb of storage since there's no data on it
i honestly couldn't tell you, im not familiar with redis in depth
ah okay
that's all my questions for tonight, thank you very much!
no problem 🙂