Nginx、uwsgi、django、ubuntu 16 带有静态文件的问题

Posted

技术标签:

【中文标题】Nginx、uwsgi、django、ubuntu 16 带有静态文件的问题【英文标题】:Nginx, uwsgi, django, ubuntu 16 issue w/ static files 【发布时间】:2016-11-24 09:29:13 【问题描述】:

我正在使用 nginx、uwsgi、django 和 Ubuntu 16.04,并在我的所有静态文件上获得 404。当我查看 nginx 的 error.log 时,它在实际文件位置前面添加了一个额外的 /static:

404 未找到: /home/jsmith/firstproj/static_cdn/static/css/bootstrap.css

实际位置: /home/jsmith/firstproj/static_cdn/css/bootstrap.css

我不确定为什么在 /css/bootstrap.css 前面添加 /static。这是我的配置文件:

/var/log/nginx/error.log

2016/07/20 17:34:25 [error] 4424#4424: *22 open() "/home/jsmith/firstproj/static_cdn/static/css/bootstrap.css" failed (2: No such file or directory), client: , server: , request: "GET /static/css/bootstrap.css HTTP/1.1", host: 

/home/jsmith/firstproj/src/blogs/settings.py

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

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

/etc/nginx/sites-available/firstproj

server 
    listen 80;

    location = /favicon.ico  access_log off; log_not_found off; 
    location /static 
        root /home/jsmith/firstproj/static_cdn;
    

    location / 
        include         uwsgi_params;
        uwsgi_pass      unix:/run/uwsgi/firstproj.sock;
    

/etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown jsmith:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

/etc/uwsgi/sites/firstproj.ini

[uwsgi]
project = firstproj
base = /home/jsmith

chdir = %(base)/%(project)/src
home = %(base)/.virtualenvs/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chmod-socket = 666
vacuum = true
logto = /var/log/uwsgi/%n.log

【问题讨论】:

【参考方案1】:

问题出在 nginx 的“静态”配置中。这段代码解决了问题

location /static 
    alias /home/jsmith/firstproj/static_cdn;

查看此帖子Nginx -- static file serving confusion with root & alias

【讨论】:

以上是关于Nginx、uwsgi、django、ubuntu 16 带有静态文件的问题的主要内容,如果未能解决你的问题,请参考以下文章

Django + uWSGI 通过 Ubuntu 11.10 上的 NGINX

django+nginx+uwsgi的生产环境部署(Ubuntu16.04)

[部署开发环境]部署django的生成环境nginx+uwsgi+django

ubuntu 配置 nginx + uwsgi + django 记录

Ubuntu???Django+uWSGI+nginx??????

Ubuntu环境下部署Django+uwsgi+nginx总结