Django API internal routing error

dheijbroek
PRO

3 months ago

Hi there,

This question may have come by before. However, I still have not managed to access my Django API (fetching data from React service) through internal routing.

Django backend: settings.py
I am sure that my environment variables are received from the railway environment variables.

I have included the following:
ALLOWED_HOSTS = [

os.getenv('BACKEND_PRIVATE_URL', 'localhost'),

f"{os.getenv('BACKEND_PRIVATE_URL', 'localhost')}:8000'",

os.getenv('BACKEND_PUBLIC_URL', 'localhost'),

]

CORS_ALLOWED_ORIGINS = [

f"https://{os.getenv('FRONTEND_PUBLIC_URL')}",

f"http://{os.getenv('BACKEND_PRIVATE_URL')}:{os.getenv('PORT', '8000')}", # Allow Railway internal URLs with port

]
And this as well for CSRF_TRUSTED_ORIGINS. Also without the port for the private URL.

React frontend: apiClient.ts
Fetches from https://backend.railway.internal/api/path (also tried with port).

Railway
In railway, both services are in the same project and same environment. Both services indicate that the internal connection should be possible.

Thanks in advance!

Solved

3 Replies

dheijbroek
PRO

3 months ago

Btw, I still need to try binding my Django to ipv6 as explained in the caveats of Railway internal networking docs.
In the mean time I am curious if someone found an alternative solution :)


3 months ago

Hello,

You cannot access the API via the private network from a web browser, it would not be a very private network if anyone's browser could call the private domain.

You need to use the API's public URL.

Please see our docs that explain why -

https://docs.railway.com/guides/private-networking#private-network-context


Best,
Brody


Status changed to Awaiting User Response Railway 3 months ago


brody

Hello,You cannot access the API via the private network from a web browser, it would not be a very private network if anyone's browser could call the private domain.You need to use the API's public URL.Please see our docs that explain why -https://docs.railway.com/guides/private-networking#private-network-contextBest,Brody

dheijbroek
PRO

3 months ago

Thanks for letting me know!


Status changed to Awaiting Railway Response Railway 3 months ago


Status changed to Solved ray-chen 3 months ago


Django API internal routing error - Railway Help Station