django nginx static files 404
Asked Answered
E

13

24

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

Eightieth answered 22/4, 2014 at 17:29 Comment(4)
At first, change your gunicorn_django command to gunicorn_django --bind=127.0.0.1:8001, because when you run it with your external ip, it will accept connections from the outside.Sheritasherj
@OmidRaha now it doesn't work at all :/Eightieth
Have you set debug=False in your setting.py file?Sherrill
That IP is being served by an Apache server; do you have an Apache reverse proxy in front of your nginx? If not I'd expect an "Address already in use" error when starting nginx.Tearjerker
G
30

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/"
    # ...
}
Giule answered 4/8, 2014 at 17:0 Comment(0)
B
7

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

location ^~ /static/ {
    alias /home/django-projects/tshirtnation/staticfiles/;
}
Bobettebobina answered 1/5, 2014 at 18:4 Comment(1)
Two days later, this solution just saved my case. Thanks Cole ;)Naples
L
1

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 = ...
Lacteous answered 29/4, 2014 at 5:5 Comment(0)
T
1

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/
Testimonial answered 18/8, 2018 at 2:26 Comment(1)
Can you add some explanation as well?Cattish
K
0

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.

Kampmeier answered 25/4, 2014 at 7:17 Comment(0)
P
0

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.

Patagium answered 25/4, 2014 at 7:39 Comment(0)
B
0

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"';
    }
}
Bert answered 26/4, 2014 at 12:22 Comment(1)
Nginx matches the most specific location so this is not necessary.Simeon
T
0

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.

Teakwood answered 28/4, 2014 at 6:32 Comment(0)
P
0

try this in settings.py:

import os
ROOT_PATH = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(ROOT_PATH,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]

and config file in nginx(or your server setting):

location /static {
    alias /home/djangohome/static; # your Django project's static files - amend as required
}
Plop answered 8/8, 2015 at 7:15 Comment(0)
S
0

I ran into this issue and solved it by adding these lines to project urls.py file:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = [
   ...
] += staticfiles_urlpatterns()

Also there is another reason this kinda issues, and it's in nginx main configuration file at /etc/nginx/nginx.conf. Make sure this file contains these lines:

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
Scaife answered 28/10, 2021 at 18:59 Comment(0)
E
0

Another gotcha to watch for - be sure to match the trailing slashes. In the example below both the location and the alias have trailing slashes. It would also work if neither did, but they must match.

location /static/ {
  alias /home/you/django/gtd/staticfiles/;
}

The location has a trailing slash, and the full path is an alias for that, so they both need trailing slashes to stay equivalent. Or you could remove the trailing slash from both.

Embitter answered 16/6, 2023 at 6:14 Comment(0)
B
-1

Try to remove slash in nginx settings followed by static e.g. It should be "/static" not "/static/", and if your settings were fine then try to reload the server on local machine and try rebooting on remote machine. I faced similar but after rebooting the machine, fixed the issue.

Burnight answered 22/6, 2016 at 7:31 Comment(0)
L
-2

Use STATIC_URL with domain. It's important!

Lithology answered 30/4, 2014 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.