11

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.

Tundebabzy
  • 829
  • 2
  • 11
  • 24

2 Answers2

21

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.

Scott Woodall
  • 10,456
  • 6
  • 39
  • 37
  • Thanks @scott Due to a previous experience with Django somehow ignoring static files stored in a folder called "static", I decides to change the location and names for my static files. After nginx reboot, my config worked without having to add your suggested location block. – Tundebabzy Jun 30 '13 at 15:48
  • When you watch your supervisor log file, do you see requests come through for static (img, css, javascript) files? – Scott Woodall Jun 30 '13 at 15:50
  • my logfile.log ie stdout_logfile in supervisord.conf is completely empty – Tundebabzy Jun 30 '13 at 16:10
  • even after putting the new location block, every request shows up in my gunicorn logs.....nothing in nginx logs – Tundebabzy Jul 01 '13 at 10:23
  • @Tundebabzy If you don't add a `location` block in your nginx.conf like Scott suggested, it is impossible for the nginx to serve your static files. – rantanplan Jul 01 '13 at 10:33
  • @rantanplan I have added the location block just that I did that in /etc/nginx/sites-enabled/oga.conf since there's include /etc/nginx/sites-enabled/* in nginx.conf. Let me try to put it in nginx.conf instead and see what happens – Tundebabzy Jul 01 '13 at 10:53
  • @Tundebabzy There is no difference. In one way or another nginx.conf has to have this directive in it. If your nginx.conf has an `include` which makes this directive available from another file(oga.conf), then it's OK. – rantanplan Jul 01 '13 at 12:24
1
location / {
    try_files $uri @proxy_to_app;
}

Please change $uri -> $request_uri

location / {
    try_files $request_uri @proxy_to_app;
}
laaposto
  • 11,835
  • 15
  • 54
  • 71