Home Ask Login Register

Developers Planet

Your answer is one click away!

Shazia Nusrat February 2016

Can't serve statics with Nginx on DigitalOcean VPS for Django

Hi I am trying to serve statics using Django and Nginx on a VPS to get my project live. My settings.py includes following.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')

My project folder includes:


In Nginx I got following:

upstream gunicorn_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server fail_timeout=0;

server {

listen   80;
server_name example.com;
keepalive_timeout 5;
client_max_body_size 4G;

access_log /home/projectuser/logs/nginx-access.log;
error_log /home/projectuser/logs/nginx-error.log;

location /static/ {
    alias   /venv/project/static/static;

location /media/ {
    alias   /venv/project/static/media;
location / {

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
        proxy_pass http://gunicorn_server;


I can't serve static no matter what and admin panel css is not showing up.

Please advise asap.


Nikita February 2016

Try changing static and media blocks this way:

location /static/ {
    root   /venv/project/static;

location /media/ {
    root   /venv/project/static;

This way, the http://example.com/static/admin/css/dashboard.css will be searched as /venv/project/static/static/admin/css/dashboard.css on the filesystem.

P.S. Also instead of if (!-f $request_filename) it's better to use try_files: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

Post Status

Asked in February 2016
Viewed 1,607 times
Voted 14
Answered 1 times


Leave an answer

Quote of the day: live life