Milano Slesarik Milano Slesarik - 1 month ago 16
Python Question

Nginx can't find some static files

I can't figure out why nginx can't find some static files after deploying on a Digital Ocean. I think that I've set everything correctly. The collectstatic worked ok, it created

/project/static
directory with all static files.

Maybe there is something wrong with
settings.py
:

STATIC_URL = '/static/'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATICFILES_DIRS = (
# os.path.join(PROJECT_ROOT, 'static'),
('dolava_app', os.path.join(PROJECT_ROOT, 'dolava_app', 'static')),
('reservations_app', os.path.join(PROJECT_ROOT, 'reservations_app', 'static')),
('admin_stuff', os.path.join(PROJECT_ROOT, 'admin_stuff', 'static')),
('ajax_stuff', os.path.join(PROJECT_ROOT, 'ajax_stuff', 'static')),
)


nginx/sites-available/django

upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

# Your Django project's media files - amend as required
location /media {
alias /home/django/project/media;
}

# your Django project's static files - amend as required
location /static {
alias /home/django/project/static;
}

# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}


I've restarted nginx and gunicorn too. But still some static files can't be found inside
django/project/static/
dir. Do you know what should I do?

Answer

You should not use alias there. In nginx it works absolutle different from apache.

http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

You should just use root instead.

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        root /home/django/project;
    }

    # your Django project's static files - amend as required
    location /static {
        root /home/django/project; 
    }

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       root /usr/lib/python2.7/dist-packages/django/contrib/admin;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}
Comments