Nginx找不到一些静态文件

时间:2016-10-21 14:10:10

标签: python django nginx

我无法弄清楚为什么nginx在数字海洋上部署后无法找到一些静态文件。我认为我已经正确设置了一切。 collectstatic工作正常,它创建了1目录,其中包含所有静态文件。

可能/project/static出现了问题:

settings.py

的nginx /位点可用/ django的

STATIC_URL = '/static/'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATICFILES_DIRS = (
    # os.path.join(PROJECT_ROOT, 'static'),
    ('dolava_app', os.path.join(PROJECT_ROOT, 'dolava_app', 'static')),
    ('reservations_app', os.path.join(PROJECT_ROOT, 'reservations_app', 'static')),
    ('admin_stuff', os.path.join(PROJECT_ROOT, 'admin_stuff', 'static')),
    ('ajax_stuff', os.path.join(PROJECT_ROOT, 'ajax_stuff', 'static')),
)

我也重新启动了nginx和gunicorn。但是仍然在upstream app_server { server 127.0.0.1:9000 fail_timeout=0; } server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; client_max_body_size 4G; server_name _; keepalive_timeout 5; # Your Django project's media files - amend as required location /media { alias /home/django/project/media; } # your Django project's static files - amend as required location /static { alias /home/django/project/static; } # Proxy the static assests for the Django Admin panel location /static/admin { alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } } 目录中找不到一些静态文件。你知道我该怎么办吗?

1 个答案:

答案 0 :(得分:1)

你不应该在那里使用alias。在nginx中,它的工作方式与apache不同。

http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

您应该使用root代替。

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        root /home/django/project;
    }

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

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       root /usr/lib/python2.7/dist-packages/django/contrib/admin;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}