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
Percy
Percy2mo ago
Project ID: bffee644-ec55-4122-9e19-fee98c401647
Alexandru
AlexandruOP2mo ago
Project ID bffee644-ec55-4122-9e19-fee98c401647
Alexandru
AlexandruOP2mo 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? Also, what about using the reference variables for the API_URL?
Brody
Brody2mo 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 - https://docs.railway.app/guides/private-networking#use-internal-hostname-and-port
Alexandru
AlexandruOP2mo 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
Alexandru
AlexandruOP2mo ago
Railway
Railway
Railway is an infrastructure platform where you can provision infrastructure, develop with that infrastructure locally, and then deploy to the cloud.
Brody
Brody2mo ago
show me a screenshot of your public domain please
Alexandru
AlexandruOP2mo ago
No description
Alexandru
AlexandruOP2mo ago
or you mean the public domain config?
Alexandru
AlexandruOP2mo ago
No description
Brody
Brody2mo ago
how do you know that is the start command that railway is using?
Alexandru
AlexandruOP2mo ago
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 guessing
Brody
Brody2mo ago
I see localhost, not 0.0.0.0
Alexandru
AlexandruOP2mo 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 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
Brody
Brody2mo ago
have you set the PORT variable?
Alexandru
AlexandruOP2mo ago
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 gateway
Brody
Brody2mo ago
it's injected into the runtime, not your own service variables go ahead and set the port variable
Alexandru
AlexandruOP2mo 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 set the frontend.PORT to 3000?
Brody
Brody2mo ago
right, you can't reference a variable that doesn't exist on this next service, set a PORT variable to 3000
Alexandru
AlexandruOP2mo ago
If I set the variable, it will work, I had it working with that config
Brody
Brody2mo ago
awesome
Alexandru
AlexandruOP2mo ago
I was just trying to follow the docs and let the service use the injected $PORT 🙂
Brody
Brody2mo ago
I know, but something is missconfigured in your app and it's not listening on it, so this is easier
Alexandru
AlexandruOP2mo ago
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 mins
Brody
Brody2mo ago
feel free to cancel it
Alexandru
AlexandruOP2mo ago
it rebuilt in 1 minute, deployed and back to working, having specified $PORT as 3000 in the env vars
Brody
Brody2mo ago
awsome
Alexandru
AlexandruOP2mo 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?
Brody
Brody2mo ago
you need to use a volume with redis, what plan are you on
Alexandru
AlexandruOP2mo ago
Pro plan
Brody
Brody2mo ago
then you can go up to 250GB (you dont pay for unused storage)
Alexandru
AlexandruOP2mo ago
but I need to add a volume to the redis service? right now I don't have a volume backing it
Brody
Brody2mo ago
ah so you currently dont care if your redis data persists?
Alexandru
AlexandruOP2mo ago
not for dev env, but it's so little data that it shouldn't matter
Brody
Brody2mo ago
okay because i was about to say you have a volume
Alexandru
AlexandruOP2mo ago
for production project so that's showing that the redis volume is using 1gb of storage, but the redis cache is pretty much empty
Brody
Brody2mo ago
yep so you will pay for 1GB of storage
Alexandru
AlexandruOP2mo ago
I'm just wondering why it's using 1gb of storage since there's no data on it
Brody
Brody2mo ago
i honestly couldn't tell you, im not familiar with redis in depth
Alexandru
AlexandruOP2mo ago
ah okay that's all my questions for tonight, thank you very much!
Brody
Brody2mo ago
no problem 🙂
Want results from more Discord servers?
Add your server