HTTP 502 Bad Gateway using the $PORT variable assigned by Railway + other service reference $PORT
alex-wirebee
PROOP

2 years ago

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}}"?

56 Replies

alex-wirebee
PROOP

2 years ago

Project ID bffee644-ec55-4122-9e19-fee98c401647


2 years ago

please see our docs page -


alex-wirebee
PROOP

2 years ago

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?


alex-wirebee
PROOP

2 years ago

Also, what about using the reference variables for the API_URL?


2 years ago

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 -


alex-wirebee
PROOP

2 years ago

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



2 years ago

show me a screenshot of your public domain please


alex-wirebee
PROOP

2 years ago

1287920122065063976


alex-wirebee
PROOP

2 years ago

or you mean the public domain config?


alex-wirebee
PROOP

2 years ago

1287920318874390611


2 years ago

how do you know that is the start command that railway is using?


alex-wirebee
PROOP

2 years ago

I see it in the deploy logs


alex-wirebee
PROOP

2 years ago

app:start: $ next start --port ${PORT-3000} app:start: ▲ Next.js 14.1.0 app:start: - Local: http://localhost:3000


alex-wirebee
PROOP

2 years ago

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 guessing


2 years ago

I see localhost, not 0.0.0.0


alex-wirebee
PROOP

2 years ago

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


alex-wirebee
PROOP

2 years ago

I'm adding --hostname 0.0.0.0 to the next start command and redeploying


alex-wirebee
PROOP

2 years ago

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


2 years ago

have you set the PORT variable?


alex-wirebee
PROOP

2 years ago

I have not, I was going to rely on the injected value from railway


alex-wirebee
PROOP

2 years ago

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


alex-wirebee
PROOP

2 years ago

But I'm still getting the 502 Bad gateway


2 years ago

it's injected into the runtime, not your own service variables


2 years ago

go ahead and set the port variable


alex-wirebee
PROOP

2 years ago

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


alex-wirebee
PROOP

2 years ago

set the frontend.PORT to 3000?


2 years ago

right, you can't reference a variable that doesn't exist


2 years ago

on this next service, set a PORT variable to 3000


alex-wirebee
PROOP

2 years ago

If I set the variable, it will work, I had it working with that config


2 years ago

awesome


alex-wirebee
PROOP

2 years ago

I was just trying to follow the docs and let the service use the injected $PORT 🙂


2 years ago

I know, but something is missconfigured in your app and it's not listening on it, so this is easier


alex-wirebee
PROOP

2 years ago

perfect, now the build is hanging haha


alex-wirebee
PROOP

2 years ago

build is stuck for the past 6 minutes at

#7 [stage-0 5/11] COPY . /app/.


alex-wirebee
PROOP

2 years ago

it usually build and deploys in 2-3 mins


2 years ago

feel free to cancel it


alex-wirebee
PROOP

2 years ago

it rebuilt in 1 minute, deployed and back to working, having specified $PORT as 3000 in the env vars


2 years ago

awsome


alex-wirebee
PROOP

2 years ago

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?


2 years ago

you need to use a volume with redis, what plan are you on


alex-wirebee
PROOP

2 years ago

Pro plan


2 years ago

then you can go up to 250GB


2 years ago

(you dont pay for unused storage)


alex-wirebee
PROOP

2 years ago

but I need to add a volume to the redis service? right now I don't have a volume backing it


2 years ago

ah so you currently dont care if your redis data persists?


alex-wirebee
PROOP

2 years ago

not for dev env, but it's so little data that it shouldn't matter


2 years ago

okay because i was about to say you have a volume


alex-wirebee
PROOP

2 years ago

for production project


alex-wirebee
PROOP

2 years ago

so that's showing that the redis volume is using 1gb of storage, but the redis cache is pretty much empty


2 years ago

yep so you will pay for 1GB of storage


alex-wirebee
PROOP

2 years ago

I'm just wondering why it's using 1gb of storage since there's no data on it


2 years ago

i honestly couldn't tell you, im not familiar with redis in depth


alex-wirebee
PROOP

2 years ago

ah okay


alex-wirebee
PROOP

2 years ago

that's all my questions for tonight, thank you very much!


2 years ago

no problem 🙂


Welcome!

Sign in to your Railway account to join the conversation.

Loading...