R
Railway•3mo ago
tjh

Frontend accessing backend - internal URL?

Hi folks I have a seperate FE and BE service - two different projects. The FE has an env var to locate the backend. Should I set this to the internal URL of the BE service that railway exposes? If so, should I prefix with http/https?
37 Replies
Percy
Percy•3mo ago
Project ID: c18400df-5dee-4fe7-bc56-a98db8d475bd
tjh
tjhOP•3mo ago
c18400df-5dee-4fe7-bc56-a98db8d475bd
Brody
Brody•3mo ago
two different projects, or two different services? please read this page so we can get our terminology correct before going forward - https://docs.railway.app/overview/the-basics
tjh
tjhOP•3mo ago
two different services within the same project
Brody
Brody•3mo ago
thank you is this a CSR frontend?
tjh
tjhOP•3mo ago
NextJS frontend, blend of SSR/CSR some of my build steps hit the backend, when I've changed to the internal URL (with https:// prefix) I get this:
#12 22.31 TypeError: fetch failed

#12 22.31 at node:internal/deps/undici/undici:12502:13

#12 22.31 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

#12 22.31 at async a (/app/.next/server/.../page.js:1:11099)

#12 22.31 at async u (/app/.next/server/.../page.js:1:10740) {

#12 22.31 digest: '4154157414',

#12 22.31 [cause]: Error: getaddrinfo ENOTFOUND <URL>

#12 22.31 at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26)

#12 22.31 at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {

#12 22.31 errno: -3008,

#12 22.31 code: 'ENOTFOUND',

#12 22.31 syscall: 'getaddrinfo',

#12 22.31 hostname: '<NAME>.railway.internal'

#12 22.31 }
#12 22.31 TypeError: fetch failed

#12 22.31 at node:internal/deps/undici/undici:12502:13

#12 22.31 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

#12 22.31 at async a (/app/.next/server/.../page.js:1:11099)

#12 22.31 at async u (/app/.next/server/.../page.js:1:10740) {

#12 22.31 digest: '4154157414',

#12 22.31 [cause]: Error: getaddrinfo ENOTFOUND <URL>

#12 22.31 at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26)

#12 22.31 at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {

#12 22.31 errno: -3008,

#12 22.31 code: 'ENOTFOUND',

#12 22.31 syscall: 'getaddrinfo',

#12 22.31 hostname: '<NAME>.railway.internal'

#12 22.31 }
Brody
Brody•3mo ago
the private network is not available during build time. the internal domains may use domains instead of IP addresses, but they need to be treated like a local IP, meaning you use http and a port in the URL. if your frontend calls the backend via SSR during runtime use the private network. if a client's browser calls the backend, use the public url.
tjh
tjhOP•3mo ago
Are the IPs static on a hobby plan?
Brody
Brody•3mo ago
that is a pro only feature, and it doesnt have anything to do with private networking.
tjh
tjhOP•3mo ago
all makes sense, thanks Brody
Brody
Brody•3mo ago
let me know if you have any more questions
tjh
tjhOP•3mo ago
sorry, one more, when addressing the internal url in SSR, do I use http or https prefix?
Brody
Brody•3mo ago
http treat it like your local network, but instead of a ip, it uses a domain
tjh
tjhOP•3mo ago
thanks man. Is that something that's obvious to everyone but me, or should it be in docs? (Or, is it already?)
Brody
Brody•3mo ago
its not obvious to everyone, i can for sure see the logic in thinking you would use https with a domain name, but it is in the docs! - https://docs.railway.app/guides/private-networking#use-internal-hostname-and-port
tjh
tjhOP•3mo ago
thanks again - should have read the manual
Brody
Brody•3mo ago
its not always a first thought to dig into the docs, no worries!
tjh
tjhOP•3mo ago
ok - have been trying this, on an API call that is definitely SSR I'm calling <my internal url>:8080/<api> from my frontend. My backend (python) has the internal url in ALLOWED_HOSTS I'm getting a
code: 'ECONNREFUSED',

syscall: 'connect',
code: 'ECONNREFUSED',

syscall: 'connect',
on the frontend
Brody
Brody•3mo ago
please provide the actual url you are using
tjh
tjhOP•3mo ago
DM'd
Brody
Brody•3mo ago
#🛂|readme #5
tjh
tjhOP•3mo ago
you need a way people can pass you variables they don't want exposed would rather not post in public
Brody
Brody•3mo ago
I assure you it's not sensitive in the slightest
Brody
Brody•3mo ago
it wouldn't be a very private network otherwise haha does your backend listen on 8080?
tjh
tjhOP•3mo ago
yes
Brody
Brody•3mo ago
gunicorn? unicorn?
tjh
tjhOP•3mo ago
verified with a print on launch of my backend - THE PORT IS 8080 gunicorn - started with web: python manage.py migrate && gunicorn <name>.asgi:application -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT
Brody
Brody•3mo ago
remove the bind flag (and it's accompanying host:port)
tjh
tjhOP•3mo ago
doing so no joy
Brody
Brody•3mo ago
show me your new start command please
tjh
tjhOP•3mo ago
web: python manage.py migrate && gunicorn share_a_podcast_be.asgi:application -k uvicorn.workers.UvicornWorker
Brody
Brody•3mo ago
what error are you getting now
tjh
tjhOP•3mo ago
Same as above Connection refused
Brody
Brody•3mo ago
very sorry, i was mistaken, your start command needs to be -
web: python manage.py migrate && gunicorn <name>.asgi:application -k uvicorn.workers.UvicornWorker --bind [::]:$PORT
web: python manage.py migrate && gunicorn <name>.asgi:application -k uvicorn.workers.UvicornWorker --bind [::]:$PORT
tjh
tjhOP•3mo ago
works! Next is now giving me warnings about mixed protocols being used (https vs http) Which I’ll look at
Brody
Brody•3mo ago
sounds good
Want results from more Discord servers?
Add your server