首页 > 解决方案 > 在 ubuntu 16.04 上使用 nginx 和 uwsgi 部署 django 项目时,nginx 无法正确提供文件

问题描述

当我部署 django 项目时,请遵循本教程:[ http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html?highlight=django 当我完成步骤Basic nginx test后,我​​输入 taopinpin。 cn/media/media.png,页面响应被拒绝是这样的: 在此处输入图片描述

我的项目是这样的: 在此处输入图像描述 mysite_nginx.conf 文件是:

upstream django {
    server 127.0.0.1:8001;
}
server {
    listen      8000;
    server_name  taopinpin.cn;

    charset     utf-8;

    client_max_body_size   75M;


    location /media  {
        alias /root/mysite/media;
    }
    location /static {
        alias /root/mysite/static;
    }
    location / {
        uwsgi_pass    django;
        inlcude       /root/mysite/mysite/uwsgi_params;
    }
}

我不知道哪里出错了,你能帮我调试一下吗?十分感谢。

标签: pythondjangonginxuwsgi

解决方案


首先,你有一个错字inlcudewhile it should be include

根据您的屏幕截图,您正在以用户身份运行应用程序root,并且您的应用程序安装在目录~/mysite中,这将/root/mysite用于用户root,但是当 nginx 使用用户名运行时www-data/root/mysite除非您使用sudo. 使用root或运行应用程序sudo不是一个好习惯。

uwsgi_params默认情况下安装在 中,/etc/nginx/通常您不需要指定目录,除非您的目录不在etc/nginx/.

在您的 nginx 配置中没有定义 web 根目录的位置,如果您定义了一个 web 根目录root /root/mysite,那么位置指令location /medialocation /static似乎是相当多余和必要的。

有了我上面提到的所有内容,这里是它应该适用于用户的配置,root并假设您还将用户设置更改为/etc/nginx/nginx.conffrom user www-datato user root再次不推荐。

upstream django {
  server 127.0.0.1:8001;
}
server {
  listen 8000;
  server_name taopinpin.cn;    #check your /etc/hostname setting
  root /root/mystie;    #this need to be change if it is not running by root
  charset utf-8;

  client_max_body_size   75M;

  location / {
    include uwsgi_params;    #make sure uwsgi_params exist at /etc/nginx/
    uwsgi_pass    django;
  }
}

推荐阅读