django nginx static files 404

2020-08-09 08:48发布

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 :/

11条回答
祖国的老花朵
2楼-- · 2020-08-09 09:08

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/
查看更多
对你真心纯属浪费
3楼-- · 2020-08-09 09:08

Mine looks like this, and it worked:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), '..', 'static'),
)

And in the nginx configuration, my location /static/ is above the location / like this,

location /static {
    //
}

# Finally, send all non-media requests to the Django server.
location / {
    //
}

And one more thing, I don't know if that matters, but I do have a 'listen' in the server{}. I'm not sure if it can help.

查看更多
Juvenile、少年°
4楼-- · 2020-08-09 09:10

Check this things

1 Whether the static older is accessible by nginx, I mean the folder permission .

2 Or do this

Replace this:

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

with this

STATIC_ROOT = ''

And add this in settings

STATICFILES_DIRS = (
     '/home/django-projects/tshirtnation/staticfiles/',
)

Don't forget to reload the nginx server.

Hope this works.

查看更多
Explosion°爆炸
5楼-- · 2020-08-09 09:11

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 = ...
查看更多
兄弟一词,经得起流年.
6楼-- · 2020-08-09 09:13

Your problem is that the location / block is always used, even after the location /static/ one, so everything will be proxy_passed.
The solution here is to use some try_files magic.

server {
    server_name 77.241.197.95;

    access_log off;

    location / {
        try_files $uri @django;
    }

    location /static/ {
        alias /home/django-projects/tshirtnation/staticfiles/;
        try_files $uri =404;
        # here we use =404 because there is no need to pass it to gunicorn.
    }

    location @djago {
        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"';
    }
}
查看更多
孤傲高冷的网名
7楼-- · 2020-08-09 09:14

I think browser tries to find your static in:

http://127.0.0.1:8001/static/

While nginx by default work on 80 port.

You need to define 8001 port in nginx config or run django server on 80 port.

查看更多
登录 后发表回答