Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static files with Nginx + Gunicorn + Django

Tags:

nginx

django

This is my nginx config:

    server {
        listen 80;
        server_name localhost;

        keepalive_timeout 5;

        access_log /home/tunde/django-projects/mumu/nginx/access.log;
        error_log /home/tunde/django-projects/mumu/nginx/error.log;

        root /home/tunde/django-projects/mumu;

        location / {
            try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://127.0.0.1:8000;
        }
    }

My settings.py looks like:

    import os
    settings_dir = os.path.dirname(__file__)
    PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

    STATIC_ROOT = '/home/tunde/django-projects/mumu/STATIC/'
    STATIC_URL = '/static/'

    STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static/'),
    )

My supervisord.conf file looks like:

[program: mumu]
command = /home/tunde/ENV1/bin/gunicorn -w 1 --bind=127.0.0.1:8000 mumu.wsgi:application
directory = /home/tunde/django-projects/mumu/
stdout_logfile= /home/tunde/django-projects/mumu/supervisor/logfile.log
stderr_logfile= /home/tunde/django-projects/mumu/supervisor/error.log
user = tunde

The problem is that static files don't get served and I have no idea what I'm doing wrong. A url like /static/css/styles.css returns a 404. Help will be greatly appreciated.

like image 710
Tundebabzy Avatar asked Jun 30 '13 14:06

Tundebabzy


People also ask

Can Gunicorn server static files?

Gunicorn is meant to serve dynamic content, it should not be used to serve static files.

Can Django serve static files?

staticfiles in INSTALLED_APPS , you can still manually serve static files using the django. views. static.

Can nginx serve static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

How do you serve static files in Django in production?

Serving static files in production. The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory ( STATIC_ROOT ) to be moved to the static file server and served.


2 Answers

You need to create an nginx location block that matches the location of your django STATIC_URL. As you have it configured, nginx is also forwarding requests for static assets to your django application. I'm guessing you are seeing the 404 errors in your django application log? You want nginx to handle these requests, not django.

location /static {
    alias /home/tunde/django-projects/mumu/STATIC/; 
}

Be sure to run the django admin command collectstatic to make sure all the static assets get copied to the STATIC_ROOT directory.

like image 94
Scott Woodall Avatar answered Oct 08 '22 08:10

Scott Woodall


location / {
    try_files $uri @proxy_to_app;
}

Please change $uri -> $request_uri

location / {
    try_files $request_uri @proxy_to_app;
}
like image 33
user3559826 Avatar answered Oct 08 '22 10:10

user3559826