简体   繁体   English

Django nginx 静态文件 404

[英]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:这是我的 nginx 配置:

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.我已经运行python manage.py collectstatic并复制了所有静态文件。 I run my server with gunicorn_django --bind:my-ip:8001 and everything seems to be working except for static files.我用 gunicorn_django --bind:my-ip:8001 运行我的服务器,除了静态文件外,一切似乎都在工作。

EDIT: I've run编辑:我跑了

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

and there seems to be no errors of static files not found :/并且似乎没有找不到静态文件的错误:/

I encountered the same problem and was able to fix my nginx configuration by removing the trailing / from the /static/ location. 我遇到了同样的问题,并通过删除/static/ location中的尾随/来修复我的nginx配置。

location /static {  # "/static" NOT "/static/"
    # ...
}

Try adding the ^~ prefix modifier to your static location to skip checking regular expressions: 尝试将^~前缀修饰符添加到静态位置以跳过检查正则表达式:

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

Your problem is that the location / block is always used, even after the location /static/ one, so everything will be proxy_pass ed. 您的问题是始终使用location /块,即使在location /static/一个之后,所以一切都将是proxy_pass ed。
The solution here is to use some try_files magic. 这里的解决方案是使用一些try_files魔法。

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"';
    }
}

In your settings.py, put this: 在你的settings.py中,输入:

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 = ...

I think browser tries to find your static in: 我认为浏览器试图找到你的静态:

http://127.0.0.1:8001/static/

While nginx by default work on 80 port. 虽然nginx默认工作在80端口上。

You need to define 8001 port in nginx config or run django server on 80 port. 您需要在nginx配置中定义8001端口或在80端口上运行django服务器。

Check this things 检查一下这个

1 Whether the static older is accessible by nginx, I mean the folder permission . 1 nginx是否可以访问静态旧版本,我的意思是文件夹权限。

2 Or do this 2或者这样做

Replace this: 替换这个:

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

with this 有了这个

STATIC_ROOT = '' STATIC_ROOT =''

And add this in settings 并在设置中添加此项

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

Don't forget to reload the nginx server. 不要忘记重新加载nginx服务器。

Hope this works. 希望这有效。

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, 在nginx配置中,我的位置/ static /位于该位置之上/像这样,

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. 我不确定它是否可以提供帮助。

try this in settings.py: 在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): 和配置文件在nginx(或您的服务器设置):

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

settings.py: 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) 在nginx配置中(/ 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: 然后重启nginx服务器:

sudo service nginx restart

And run gunicorn: 并运行gunicorn:

gunicorn PFT.wsgi

Serves the application on localhost or the entire local network (on port 80). 在localhost或整个本地网络上(在端口80上)提供应用程序。

http://127.0.0.1/

I ran into this issue and solved it by adding these lines to project urls.py file:我遇到了这个问题并通过将这些行添加到项目urls.py文件来解决它:

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 .还有另一个原因导致这种问题,它位于/etc/nginx/nginx.conf nginx 主配置文件中。 Make sure this file contains these lines:确保此文件包含以下行:

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

Try to remove slash in nginx settings followed by static eg 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. 尝试删除nginx设置中的斜杠,然后是静态,例如它应该是“/ static”而不是“/ static /” ,如果您的设置很好,那么尝试在本地计算机上重新加载服务器并尝试在远程计算机上重新启动。 I faced similar but after rebooting the machine, fixed the issue. 我遇到了类似但重新启动机器后修复了问题。

Use STATIC_URL with domain. 将STATIC_URL与域一起使用。 It's important! 这一点很重要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM