【问题标题】:nginx cant't serve files correctly when deploy a django project with nginx and uwsgi on ubuntu 16.04在 ubuntu 16.04 上使用 nginx 和 uwsgi 部署 django 项目时,nginx 无法正确提供文件
【发布时间】:2023-03-17 19:55:02
【问题描述】:

当我部署 django 项目时,请遵循本教程:[http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html?highlight=django 当我完成 Basic nginx test 步骤时,在我输入 taopinpin.cn/media/media.png 后,页面响应被拒绝,如下所示: enter image description here

我的项目是这样的: enter image description here 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 /root/mysite/mysite/uwsgi_params;有错别字

标签: python django nginx uwsgi


【解决方案1】:

在您的 mysite_nginx.conf 中尝试以下操作:

upstream cluster {
    server 127.0.0.1:8001;
} 

server {
    listen      8000;
    server_name  taopinpin.cn;
    charset     utf-8;
    client_max_body_size   75M;

    location / {
         uwsgi_pass cluster;
         proxy_set_header X-Forwarded-Protocol $scheme;
         include /root/mysite/mysite/uwsgi_params;
         uwsgi_modifier1 30;
         proxy_set_header HTTP_AUTHORIZATION $http_authorization;
         proxy_set_header  X-Real-IP  $remote_addr;
         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header  Host $http_host;
         proxy_redirect  off;
         uwsgi_read_timeout 900;
     }

     location /media  {
         alias /root/mysite/media;
     }

     location /static {
         alias /root/mysite/static;
     }

     location ~* ^.+\.(js|css)$ {
         access_log off; log_not_found off;
     }

}

然后在您的/root/mysite/mysite/uwsgi_params 中(验证该文件确实存在):

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

运行nginx -t 以验证您的语法是否有效。

运行service nginx reload/etc/init.d/nginx reload

创建目录/root/mysite/logs 并使其可写 sudo mkdir /root/mysite/logs

然后重新启动 uwsgi - 调整 youruseryourproject 以适应您的规格:

   uwsgi --socket :8001 --module yourproject.wsgi --emperor /etc/uwsgi/vassals --uid youruser --gid youruser  --master --processes 6 --threads 3 --stats 127.0.0.1:9191 --daemonize=/root/mysite/logs/uwsgi.log

根据您的图像,您的 nginx 服务器无法在其中提供您的媒体文件 /root/mysite/media - 授予 nginx 用户对其的所有权 如果您在 /etc/nginx/nginx.conf 中的用户是

user    www-data;

然后做:

sudo chown www-data:www-data /root/mysite/media

- nginx 用户(可能不是 root 用户)必须有权访问此目录。

【讨论】:

  • 我可以将 www-data 更改为 root 吗?非常感谢。
  • 它必须与您的nginx.conf 中的用户相同 - 您可以,但您可能会造成某些漏洞。欢迎使用。
  • 我已经按照你说的做了:sudo chown www-data:www-data /root/mysite/media 但它也行不通。我很困惑。
  • 在您的 nginx.conf 中必须有一行 error_log - 打开该文件并查看那里有什么错误消息。如果没有配置 - 配置它,以便您可以在日志中看到错误,然后重新启动 nginx 和 uwsgi
【解决方案2】:

这里有一些错误。 rootalias 不是一回事,实际上在这里完全没有必要。您应该在 location 指令之外指定一次 root 。无论如何,它们都会从服务器指令继承它,而您基本上在做同样的事情。

upstream django {
    server 127.0.0.1:8001;
}
server {
    listen      8000;
    server_name  taopinpin.cn;
    root /root/mysite;       # you should really use a standard location, like /var/www
    charset     utf-8;

client_max_body_size   75M;

location / {
    try_files $uri $uri/ @proxy;
}
location @proxy {
    inlcude       /root/mysite/mysite/uwsgi_params; # is this correct? Mine is in /etc/nginx
    uwsgi_pass    django;
}
}

【讨论】:

    【解决方案3】:

    首先,您在inlcude 中有错字,而它应该是include

    基于您的screen capture,您正在以用户root 的身份运行您的应用程序,并且您的应用程序安装在目录~/mysite,对于用户root,这将是/root/mysite,但是当运行nginx 时使用用户名www-data,除非您使用sudo 运行所有内容,否则无法访问/root/mysite使用root 或使用sudo 运行应用程序不是一个好习惯。

    uwsgi_params默认安装在/etc/nginx/,一般不需要指定目录,除非你的目录不在etc/nginx/

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

    有了上面提到的所有内容,这里是它应该适用于用户root 的配置,并假设您还将/etc/nginx/nginx.conf 的用户设置从user www-data 更改为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;
      }
    }
    

    【讨论】:

    • 谢谢!但是如何编辑媒体和静态的位置?
    • 如果你将你的 web 根 root/mysite 指向 /,那么 root/mysite/mdidia 已经是 /media 的意思,你不需要那个别名设置。阅读nginx alias了解更多详情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2016-08-12
    • 2013-12-08
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多