+-
django nginx静态文件404

这是我的设置:

STATIC_URL = '/static/'

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

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

这是我的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"';
    }
}

我运行python manage.py collectstatic并且它已经复制了所有静态文件。我使用gunicorn_django运行我的服务器--bind:my-ip:8001除了静态文件外,一切似乎都在工作。

编辑:我跑了

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

似乎没有找到静态文件的错误:/

18
投票

我遇到了同样的问题,并能够通过从/位置删除尾随的/static/来修复我的nginx配置。

location /static {  # "/static" NOT "/static/"
    # ...
}
-1
投票

尝试删除nginx设置中的斜杠,然后静态,例如它应该是“/ static”而不是“/ static /”,如果您的设置很好,那么尝试在本地计算机上重新加载服务器并尝试在远程计算机上重新启动。我遇到了类似但重新启动机器后修复了问题。

-2
投票

将STATIC_URL与域一起使用。这一点很重要!

3
投票

尝试将^~前缀修饰符添加到静态位置以跳过检查正则表达式:

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

你的问题是,总是使用location /块,即使在location /static/之后,所以一切都将是proxy_passed。 这里的解决方案是使用一些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"';
    }
}
1
投票

在你的settings.py中,输入:

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

你不需要这个:

STATICFILES_DIRS = ...
0
投票

我认为浏览器试图找到你的静态:

http://127.0.0.1:8001/static/

虽然nginx默认工作在80端口上。

您需要在nginx配置中定义8001端口或在80端口上运行django服务器。

0
投票

检查一下这个

1 nginx是否可以访问静态旧版本,我的意思是文件夹权限。

2或者这样做

替换这个:

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

有了这个

STATIC_ROOT =''

并在设置中添加此项

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

不要忘记重新加载nginx服务器。

希望这有效。

0
投票

我看起来像这样,它有效:

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

在nginx配置中,我的位置/ static /位于该位置之上/像这样,

location /static {
    //
}

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

还有一件事,我不知道这是否重要,但我确实在服务器{}中有'监听'。我不确定它是否可以提供帮助。

0
投票

在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'),]

和配置文件在nginx(或您的服务器设置):

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

settings.朋友:

ALLOWED_HOSTS = ['*']

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

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

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

然后重启nginx服务器:

sudo service nginx restart

并运行gunicorn:

gunicorn PFT.wsgi

在localhost或整个本地网络上(在端口80上)提供应用程序。

http://127.0.0.1/