Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django nginx static files 404

Here are my settings :

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_ROOT = '/home/django-projects/tshirtnation/staticfiles'

Here's my nginx configuration:

server {
    server_name 77.241.197.95;

    access_log off;

    location /static/ {
        alias /home/django-projects/tshirtnation/staticfiles/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

I've run python manage.py collectstatic and it has copied all static files. I run my server with gunicorn_django --bind:my-ip:8001 and everything seems to be working except for static files.

EDIT: I've run

sudo tail /var/log/nginx/error.log

and there seems to be no errors of static files not found :/

like image 289
Marijus Avatar asked Apr 22 '14 17:04

Marijus


4 Answers

I encountered the same problem and was able to fix my nginx configuration by removing the trailing / from the /static/ location.

location /static {  # "/static" NOT "/static/"
    # ...
}
like image 89
pztrick Avatar answered Nov 15 '22 17:11

pztrick


Try adding the ^~ prefix modifier to your static location to skip checking regular expressions:

location ^~ /static/ {
    alias /home/django-projects/tshirtnation/staticfiles/;
}
like image 41
Cole Tierney Avatar answered Nov 15 '22 17:11

Cole Tierney


In your settings.py, put this:

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
STATIC_ROOT = "/home/django-projects/tshirtnation/staticfiles/"
STATIC_URL = '/static/'

You don't need this:

STATICFILES_DIRS = ...
like image 24
rafaels88 Avatar answered Nov 15 '22 17:11

rafaels88


settings.py:

ALLOWED_HOSTS = ['*']

STATIC_URL = '/static/'
STATIC_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/static/'

MEDIA_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/media/'
MEDIA_URL = '/media/'

In the nginx configurations(/etc/nginx/sites-enabled/default)

server {
    server_name localhost;

    access_log off;

    location /static/ {
        alias /home/calosh/PycharmProjects/Proyecto_AES/static/;
    }

    location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

Then restart the nginx server:

sudo service nginx restart

And run gunicorn:

gunicorn PFT.wsgi

Serves the application on localhost or the entire local network (on port 80).

http://127.0.0.1/
like image 1
calosh Avatar answered Nov 15 '22 17:11

calosh