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

a year 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 APIURL env var to "http://${{backend.RAILWAYPRIVATE_DOMAIN}}:${{backend.PORT}}"?

56 Replies

alex-wirebee
PROOP

a year ago

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


a year ago

please see our docs page -


alex-wirebee
PROOP

a year 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

a year ago

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


a year 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

a year 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



a year ago

show me a screenshot of your public domain please


alex-wirebee
PROOP

a year ago

1287920122065064000


alex-wirebee
PROOP

a year ago

or you mean the public domain config?


alex-wirebee
PROOP

a year ago

1287920318874390500


a year ago

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


alex-wirebee
PROOP

a year ago

I see it in the deploy logs


alex-wirebee
PROOP

a year ago

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


alex-wirebee
PROOP

a year 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


a year ago

I see localhost, not 0.0.0.0


alex-wirebee
PROOP

a year 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

a year ago

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


alex-wirebee
PROOP

a year 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


a year ago

have you set the PORT variable?


alex-wirebee
PROOP

a year ago

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


alex-wirebee
PROOP

a year 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

a year ago

But I'm still getting the 502 Bad gateway


a year ago

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


a year ago

go ahead and set the port variable


alex-wirebee
PROOP

a year 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

a year ago

set the frontend.PORT to 3000?


a year ago

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


a year ago

on this next service, set a PORT variable to 3000


alex-wirebee
PROOP

a year ago

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


a year ago

awesome


alex-wirebee
PROOP

a year ago

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


a year ago

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


alex-wirebee
PROOP

a year ago

perfect, now the build is hanging haha


alex-wirebee
PROOP

a year ago

build is stuck for the past 6 minutes at
#7 [stage-0 5/11] COPY . /app/.


alex-wirebee
PROOP

a year ago

it usually build and deploys in 2-3 mins


a year ago

feel free to cancel it


alex-wirebee
PROOP

a year ago

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


a year ago

awsome


alex-wirebee
PROOP

a year 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?


a year ago

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


alex-wirebee
PROOP

a year ago

Pro plan


a year ago

then you can go up to 250GB


a year ago

(you dont pay for unused storage)


alex-wirebee
PROOP

a year ago

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


a year ago

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


alex-wirebee
PROOP

a year ago

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


a year ago

okay because i was about to say you have a volume


alex-wirebee
PROOP

a year ago

for production project


alex-wirebee
PROOP

a year ago

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


a year ago

yep so you will pay for 1GB of storage


alex-wirebee
PROOP

a year ago

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


a year ago

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


alex-wirebee
PROOP

a year ago

ah okay


alex-wirebee
PROOP

a year ago

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


a year ago

no problem 🙂


Loading...