Search code examples
djangonginxuwsgi

Unable to serve django static files using uwsgi server to the nginx reverse proxy


I'm using uwsgi as the server and nginx as the reverse proxy for running the django project.

Project structure is as follows(here i have listed only required folder/files):

war
├── katana
│   ├── wapps
│   │   ├── app1
│   │   └── app2
│   └── wui
│       ├── settings.py
│       └── wsgi.py
└── static
    ├── css
    │   └── test.css
    ├── img
    │   └── test.img
    └── js
        └── test.js

The static configuration in settings.py is as follows:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static')
    ]
DATA_UPLOAD_MAX_MEMORY_SIZE = 10242880
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

The wsgi.py is as follows:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "katana.wui.settings")

application = get_wsgi_application()

The uwsgi is server is started using: uwsgi -b 65535 --socket :4000 --workers 100 --cpu-affinity 1 --module katana.wui.wsgi --py-autoreload 1

The nginx conf is as follows:

events {
  worker_connections  1024;  ## Default: 1024
}

http {
    include     conf/mime.types;

    # the upstream component nginx needs to connect to
    upstream uwsgi {
        server backend:4000; # for a web port socket (we'll use this first)
    }

    # configuration of the server
    server {
        # the port your site will be served on
        listen      8443 ssl http2 default_server;

        # the domain name it will serve for
        server_name _; # substitute your machine's IP address or FQDN
        charset     utf-8;

        ssl_certificate     /secrets/server.crt;
        ssl_certificate_key /secrets/server.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        add_header Strict-Transport-Security "max-age=31536000" always;

        # Redirect HTTP to HTTPS
        error_page 497 https://$http_host$request_uri;

        # max upload size
        client_max_body_size 75M;   # adjust to taste
        uwsgi_read_timeout 600s;

        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  uwsgi;
            include     /config/uwsgi_params; # the uwsgi_params file you installed
        }
    }
}

The project deployment is successful but the static contents(css,js,img) are not loading. Error in the browser console:

GET https://<ip>/static/css/test.css net::ERR_ABORTED 404

Note: I want uwsgi server to serve the static files and nginx to just act as a reverse proxy. If nginx is configured to serve static files then it is able to do it but i want this functionality to be achived using uwsgi server.


Solution

  • Django does not serve static files in production you should add aditional nginx location for them

    location /static {
            alias   /path/to/your/static/;
        }
    

    I would advise not to do following as you are already proxying from nginx, there is no point of uwsgi handling them ( you get bit more load this way as they need to be proxied back)

    If you still want to go this path uwsgi staticfile docs