20

这是我的设置:

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

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

4

12 回答 12

25

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

location /static {  # "/static" NOT "/static/"
    # ...
}
于 2014-08-04T17:00:01.950 回答
6

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

location ^~ /static/ {
    alias /home/django-projects/tshirtnation/staticfiles/;
}
于 2014-05-01T18:04:44.897 回答
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 = ...
于 2014-04-29T05:05:36.980 回答
1

设置.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/'

在 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/
于 2018-08-18T02:26:23.527 回答
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 / {
    //
}

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

于 2014-04-28T06:32:28.573 回答
0

你的问题是该location /块总是被使用,即使在location /static/一个之后,所以一切都会被proxy_pass编辑。
这里的解决方案是使用一些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"';
    }
}
于 2014-04-26T12:22:11.023 回答
0

我认为浏览器会尝试在以下位置找到您的静态:

http://127.0.0.1:8001/static/

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

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

于 2014-04-25T07:17:36.737 回答
0

检查这些东西

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

2 或者这样做

替换这个:

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

有了这个

STATIC_ROOT = ''

并将其添加到设置中

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

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

希望这有效。

于 2014-04-25T07:39:57.437 回答
0

我遇到了这个问题并通过将这些行添加到项目urls.py文件来解决它:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns


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

还有另一个原因会出现这种问题,它位于 nginx 主配置文件中/etc/nginx/nginx.conf。确保此文件包含以下行:

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
于 2021-10-28T18:59:01.037 回答
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
}
于 2015-08-08T07:15:54.757 回答
-1

尝试在 nginx 设置中删除斜线,然后是静态,例如它应该是"/static"而不是"/static/",如果您的设置很好,那么尝试在本地机器上重新加载服务器并尝试在远程机器上重新启动。我遇到了类似的情况,但在重新启动机器后,解决了这个问题。

于 2016-06-22T07:31:12.000 回答
-2

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

于 2014-04-30T13:05:14.953 回答