Django和Nginx设置不提供静态文件

时间:2019-03-09 00:27:23

标签: django nginx

重新启动后,我的Django-Nginx设置神秘地停止了向我的站点提供静态文件。该网站似乎托管良好,但是当我的网站尝试提供CSS文件和其他静态内容时,出现“ 404”错误。

“ CoolBlog”是项目的名称,“ Blog”是应用程序的名称。

目录树:

site.mywebsite.com
|
├── database
│   └── db.sqlite3
├── source
│   ├── blog
│   ├── CoolBlog
│   ├── functional_tests.py
│   ├── manage.py
│   ├── requirements.txt
│   └── static
├── static
│   ├── admin
│   ├── bootstrap-4.3.1-dist
│   ├── css
│   └── styles.css
└── virtualenv
    ├── bin
    └── lib

CoolBlog / settings.py中的相关部分:

STATIC_ROOT = os.path.join(BASE_DIR, '../static')
STATIC_URL = '/static/'

可用站点/site.mywebsite.com:

server{
    listen 80;
    server_name site.mywebsite.com;

    location /source/static {
        alias /home/elspeth/sites/site.mywebsite.com/static;
        autoindex on;
    }

    location / {
        proxy_pass http://unix:/tmp/site.mywebsite.com.socket;
        proxy_set_header Host $host;
    }
}

软件/操作系统版本:

  • Ubuntu 16.04.6 LTS

  • Django 2.1.7

  • nginx / 1.10.3

  • Gunicorn 19

我非常感谢有关如何进行的建议。

1 个答案:

答案 0 :(得分:0)

  1. 运行./manage.py collectstatic
  2. django + gunicorn + nginx配置的示例:https://docs.gunicorn.org/en/latest/deploy.html

    server {
        server_name www.site.com;
        client_max_body_size 5M;
        location = /favicon.ico { access_log off; log_not_found off; }
    
        location /static {
            alias /home/caocao/coding/caocao/static;
        }
    
        location /media {
            alias /home/caocao/coding/caocao/media;
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
            proxy_pass http://unix:/home/caocao/coding/caocao/gunicorn.sock;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/www.site.com-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.site.com-0001/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }