Retrieving Django database sessions work locally behind a proxy but fail on Railway?

Does anyone know if or why reading Django (DRF) sessions might fail behind a reverse proxy on Railway such as Caddy or Nginx but work locally? I am using django.contrib.sessions.backends.db in my case.
37 Replies
Percy
Percy9mo ago
Project ID: N/A
JeffPeterson
JeffPetersonOP9mo ago
N/A
Brody
Brody9mo ago
are you having gunicorn trust the proxy headers?
JeffPeterson
JeffPetersonOP9mo ago
I will be honest, I don't think I have done that. Do you know where I can find documentation for this?
Brody
Brody9mo ago
https://docs.gunicorn.org/en/stable/settings.html?highlight=forwarded-allow-ips#forwarded-allow-ips add the flag --forwarded-allow-ips * to your gunicorn start command
JeffPeterson
JeffPetersonOP9mo ago
Thanks! I will try that out and see if it works So when I run this I get an error:
Failed to find attribute 'application' in 'Core'.
Failed to find attribute 'application' in 'Core'.
I seen online that it may have something to do with the wsgi.py file but I am not quite sure. Here is my wsgi.py file:
"""
WSGI config for django_project project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import os
from dotenv import load_dotenv, find_dotenv

load_dotenv(dotenv_path=find_dotenv(filename='.env'))

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', f'settings.{os.getenv("DJANGO_SETTINGS_FILE")}')

application = get_wsgi_application()
"""
WSGI config for django_project project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import os
from dotenv import load_dotenv, find_dotenv

load_dotenv(dotenv_path=find_dotenv(filename='.env'))

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', f'settings.{os.getenv("DJANGO_SETTINGS_FILE")}')

application = get_wsgi_application()
Brody
Brody9mo ago
what's your start command
JeffPeterson
JeffPetersonOP9mo ago
exec gunicorn --forwarded-allow-ips * django_project.wsgi:application --bind [::]:${BACKEND_DJANGO_PORT} --timeout 0
exec gunicorn --forwarded-allow-ips * django_project.wsgi:application --bind [::]:${BACKEND_DJANGO_PORT} --timeout 0
Brody
Brody9mo ago
and where are you defining that start command
JeffPeterson
JeffPetersonOP9mo ago
Oh that is in a set_up.sh file that is run by a Dockerfile
Brody
Brody9mo ago
do you need to talk to your django app over the private network?
JeffPeterson
JeffPetersonOP9mo ago
That will be the endgame plan
Brody
Brody9mo ago
use this then -
exec gunicorn django_project.wsgi --bind [::]:${PORT} --timeout 0 --forwarded-allow-ips *
exec gunicorn django_project.wsgi --bind [::]:${PORT} --timeout 0 --forwarded-allow-ips *
side note, always best to use the standard names for environment variables such as PORT, then since you will need a fixed port simply define PORT as a service variable
JeffPeterson
JeffPetersonOP9mo ago
Ahh thanks for the information. I will reploy now Unfortunately I get the same error:
Failed to find attribute 'application' in 'Core'.
Failed to find attribute 'application' in 'Core'.
I must be doing something wrong:
gunicorn --forwarded-allow-ips * django_project.wsgi --bind [::]:${PORT} --timeout 0
gunicorn --forwarded-allow-ips * django_project.wsgi --bind [::]:${PORT} --timeout 0
Brody
Brody9mo ago
do you have the start command set elsewhere, like in the service settings?
JeffPeterson
JeffPetersonOP9mo ago
Sorry, where would I find the service settings? I am not using a procfile or a gunicorn config file
Brody
Brody9mo ago
you can find the settings tab after opening your service
JeffPeterson
JeffPetersonOP9mo ago
Oh you mean in railway Actually it fails locally also And there is no additional start command in my railway settings
Brody
Brody9mo ago
share your dockerfile?
JeffPeterson
JeffPetersonOP9mo ago
FROM python:3.11.6

WORKDIR /app/django_project

COPY ./backend-django/requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY ./backend-django /app

WORKDIR /app/django_project

EXPOSE 8000
EXPOSE 5672
EXPOSE 15672

# RUN python manage.py makemigrations && python manage.py migrate

CMD ["sh", "set_up.sh"]
FROM python:3.11.6

WORKDIR /app/django_project

COPY ./backend-django/requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY ./backend-django /app

WORKDIR /app/django_project

EXPOSE 8000
EXPOSE 5672
EXPOSE 15672

# RUN python manage.py makemigrations && python manage.py migrate

CMD ["sh", "set_up.sh"]
And set_up.sh:
exec gunicorn --forwarded-allow-ips * django_project.wsgi --bind [::]:${PORT} --timeout 0
exec gunicorn --forwarded-allow-ips * django_project.wsgi --bind [::]:${PORT} --timeout 0
Brody
Brody9mo ago
no railway.json either?
JeffPeterson
JeffPetersonOP9mo ago
No sir
Brody
Brody9mo ago
is django_project the correct folder name?
JeffPeterson
JeffPetersonOP9mo ago
Maybe it is how my project structure is structured?
Brody
Brody9mo ago
share your repo?
JeffPeterson
JeffPetersonOP9mo ago
Here is my repository structure:
backend-django
django_project
AppTwo
...
Core
API
v1
__init__.py
admin.py
apps.py
helpers.py
permisssions.py
serializers.py
services.py
views.py
viewsets.py
v2
(similar to v1)
migrations
...
django_project
__init__.py
asgi.py
router.py
urls.py
wsgi.py
settings
__init__.py
base.py
settings_local.py
settings_production.py
set_up.sh
backend-django
django_project
AppTwo
...
Core
API
v1
__init__.py
admin.py
apps.py
helpers.py
permisssions.py
serializers.py
services.py
views.py
viewsets.py
v2
(similar to v1)
migrations
...
django_project
__init__.py
asgi.py
router.py
urls.py
wsgi.py
settings
__init__.py
base.py
settings_local.py
settings_production.py
set_up.sh
Brody
Brody9mo ago
quite hard to get a good grasp of what's going on like that, if sharing a link to your repo not a option, could you add me to it?
JeffPeterson
JeffPetersonOP9mo ago
Unfortunately I had signed an NDA so it is not possible at the moment. But if you would like to look at a specific file I can do my best to provide it
Brody
Brody9mo ago
ah okay no worries I'm kinda out of ideas tbh you said running the same set_up.sh locally gives you the same error?
JeffPeterson
JeffPetersonOP9mo ago
That is correct
Brody
Brody9mo ago
are you running that script while in the same folder that the script resides in?
JeffPeterson
JeffPetersonOP9mo ago
This is also correct
Brody
Brody9mo ago
back to having no ideas
JeffPeterson
JeffPetersonOP9mo ago
Ah okay I fixed it. It was actually a syntax error where I did not put "*" in:
exec gunicorn --forwarded-allow-ips * django_project.wsgi --bind [::]:${PORT} --timeout 0
exec gunicorn --forwarded-allow-ips * django_project.wsgi --bind [::]:${PORT} --timeout 0
Basically it was a string
Brody
Brody9mo ago
what a fun little bug
Brody
Brody9mo ago
it did give an example, yet i gave you the wrong start command, my bad
No description
JeffPeterson
JeffPetersonOP9mo ago
No problem! I am going to figure out why the Django application is not using the django_session table
Want results from more Discord servers?
Add your server