Confusion around $PORT and custom domains
matt-hall-work
PROOP

2 months ago

Hi,

I'm quite confused around how the PORT environmental variable interacts with custom domains. The documentation is clear that I am to read the PORT environmental variable and listen on that port. Got it. That's what I'm doing successfully (it happened to be in 8080 in one of my services).

However, when I go to add a custom domain, it does not allow me to leave the port number blank - it forces me to enter a number (I'm running ngnix and the Railway "magic" doesn't see it for some reason?). So, I entered 8080, because that's what Railway happened to set my PORT to in that running instance.

This seems like it's wrong, though? The documentation has suggested I use PORT in code, since the actual port might change, but the UI is forcing me to enter an explicit port number when adding the custom domain. What if it changes?

If I enter "8080" (or, 9999 or 7777 or whatever) in the custom domain, is Railway setting PORT to that? If it's not, why won't Railway let me leave the port number on the custom domain blank so that it can just use PORT itself? Please advise.

An additional question I have, is that my code (as written) expects an environmental variable that points to itself (i.e. "http://localhost:8080"). In the variables UI I tried to configure this as "http://localhost:${{PORT}}" but that doesn't work and it's being set in the container to "http://localhost:". I realize I can fix this by changing the application code to concatenate the two, but I'd rather not (I just want the one env variable) and I'm trying to understand how PORT actually works and gets set by Railway.

Thanks!

$20 Bounty

5 Replies

ilyass012
FREE

2 months ago

the port you enter in the custom domain ui is just telling railway's proxy where to forward incoming traffic to inside your container. entering 8080 is correct, you're not doing anything wrong there. railway uses 8080 by default on the v2 runtime so it won't randomly change on you


ilyass012
FREE

2 months ago

for the ${{port}} issue , port is only injected at runtime and is not available in the dashboard. that's why http://localhost:${{port}} resolves to http://localhost: with nothing after it. the ${{}} syntax resolves at config time, but port doesn't exist yet at that stage.

the fix is to manually add port=8080 as a service variable in your variables tab. once it's a real declared variable, ${{port}} will resolve correctly and your http://localhost:${{port}} will work exactly as you want without changing any application code .


masthan-8
PRO

2 months ago

PORT is the runtime port your app should listen on. The custom domain port is just the internal target port Railway should route that domain to. They are related, but not the same setting.

So if your app is listening on PORT=8080, set the custom domain target port to 8080 too. Railway is not using that custom-domain field to dynamically set PORT for you.

On the env var part: http://localhost:${{PORT}} does not seem to resolve from Railway’s injected runtime PORT. If you need that, the clean fix is to define PORT yourself as a normal variable, then reference it, or build the URL in code.


matt-hall-work
PROOP

2 months ago

Ah - this is all helpful. I think the bit that I was missing is that I can just set PORT explicitly myself to whatever I want and Railway will honor that, while it also giving me a fixed value to set in the custom domain port. Thanks!


matt-hall-work

Ah - this is all helpful. I think the bit that I was missing is that I can just set PORT explicitly myself to whatever I want and Railway will honor that, while it also giving me a fixed value to set in the custom domain port. Thanks!

ilyass012
FREE

2 months ago

yeah exactly, and since you're setting port yourself now, that ${{port}} reference in your variable will also work perfectly too

good luck with the rest of the project!


Welcome!

Sign in to your Railway account to join the conversation.

Loading...