python - 在 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;
}
}
我不知道哪里出错了,你能帮我调试一下吗?十分感谢。
解决方案
首先,你有一个错字inlcude
while 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 /media
和location /static
似乎是相当多余和必要的。
有了我上面提到的所有内容,这里是它应该适用于用户的配置,root
并假设您还将用户设置更改为/etc/nginx/nginx.conf
from user www-data
to 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;
}
}
推荐阅读
- python - “int”对象不是可迭代的错误?怎么修?
- json - jq 加入公共密钥
- java - 在 PDFbox 中动态创建页面 - Java for Android
- r - 如何通过 r 中的 AWS CloudFront 访问 Amazon S3 数据?
- python - 在 python 3.7 上安装 PyQt4。出错
- python - 为什么需要检查二维数组是高于 float(inf) 还是低于 float(-inf)
- nestjs - 如何将路由添加到 Nestjs 应用程序中不存在的 Swagger
- python - 使用另一个文件中的函数进行计算
- bash - Unix bash:在exel文件中获取正确的行数
- xml - PowerShell XML 函数问题