简体   繁体   English

nginx 在 18.04 LTS 上运行 django 时 uwsgi 出现 502 错误

[英]nginx gives 502 error with uwsgi for running django on ubuntu 18.04 LTS

Note: I am new to django and its deployment.注意:我是 django 及其部署的新手。

Deployed django through uwsgi and nginx according to the steps mentioned in this guide - except the emperor-vassal configuration and without any virtual environment.根据本指南中提到的步骤,通过 uwsgi 和 nginx 部署 django - 除了帝王附庸配置并且没有任何虚拟环境。

Side note: The site comes up using python3 manage.py 0.0.0.0:8800旁注:该站点使用python3 manage.py 0.0.0.0:8800

But, it seems that nginx is facing permission issues in the socket and giving a 502 bad gateway error in the browser.但是,nginx 似乎在套接字中面临权限问题,并在浏览器中出现 502 bad gateway 错误。

The nginx error log shows the following error: nginx 错误日志显示以下错误:

2020/07/08 21:05:40 [crit] 3943#3943: *3 connect() to unix:///home/ubuntu/deploymenttst/MySite/MySite.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.12.12, server: 192.168.12.12, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///home/ubuntu/deploymenttst/MySite/MySite.sock:", host: "192.168.12.12:8400" 2020/07/08 21:05:40 [crit] 3943#3943: *3 connect() 到 unix:///home/ubuntu/deploymenttst/MySite/MySite.sock 在连接到上游时失败(13:权限被拒绝) ,客户端:192.168.12.12,服务器:192.168.12.12,请求:“GET / HTTP/1.1”,上游:“uwsgi://unix:///home/ubuntu/deploymenttst/MySite/MySite.sock:”,主机:“192.168.12.12:8400”

The configuration are as follows:配置如下:

  1. In settings.py file of the project, the configuration are set as (apart from the default wsgi):在项目的 settings.py 文件中,配置设置为(除了默认的 wsgi):

     DEBUG = False ALLOWED_HOSTS = [ "192.168.12.12" ] STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  2. The MySite_nginx.conf file inside /etc/nginx/sites-available/MySite_nginx.conf has the following configuration entries: /etc/nginx/sites-available/MySite_nginx.conf 中的/etc/nginx/sites-available/MySite_nginx.conf文件具有以下配置条目:

     # MySite_nginx.conf # the upstream component nginx needs to connect to upstream django { server unix:///home/ubuntu/deploymenttst/MySite/MySite.sock; # for a file socket #server 127.0.0.1:8008; } # configuration of the server server { # the port your site will be served on listen 8400; # the domain name it will serve for server_name 192.168.12.12; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # # Django media location /media { alias /home/ubuntu/deploymenttst/MySite/media; # your Django project's media files - amend as required } location /static { #alias /home/ubuntu/deploymenttst/MySite/main/static; # your Django project's static files - amend as required #alias /home/ubuntu/deploymenttst/MySite/register/static; # your Django project's static files - amend as required alias /home/ubuntu/deploymenttst/MySite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /home/ubuntu/deploymenttst/MySite/uwsgi_params; # the uwsgi_params file you installed } }

    It has been sym linked to /etc/nginx/sites-enabled/MySite_nginx.conf.它已被符号链接到 /etc/nginx/sites-enabled/MySite_nginx.conf。

  3. The uwsgi_params file is made inside the project directory containing the following entries: 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 REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
  4. The content inside the MySite_uwsgi.ini file is as follows: MySite_uwsgi.ini 文件里面的内容如下:

     [uwsgi] # Django-related settings # the base directory (full path) chdir = /home/ubuntu/deploymenttst/MySite # Django's wsgi file module = MySite.wsgi # the virtualenv (full path) #home = /home/ubuntu/deploymenttst/MySite/ # process-related settings # master master = true # maximum number of worker processes processes = 20 # the socket (use the full path to be safe socket = /home/ubuntu/deploymenttst/MySite/MySite.sock #... with appropriate permissions - may be needed chmod-socket = 666 uid = www-data gid = www-data # clear environment on exit vacuum = true
  5. Static files have been collected inside a static directory inside the project directory using python3 manage.py collectstatic Static 文件已使用python3 manage.py collectstatic到项目目录内的 static 目录中

  6. In one of the terminal windows, the uwsgi process is started successfully using: uwsgi --ini MySite_uwsgi.ini在其中一个终端 windows 中,使用以下命令成功启动了 uwsgi 进程: uwsgi --ini MySite_uwsgi.ini

  7. nginx has been stopped and started and restarted multiple time upon each configuration change and otherwise as well. nginx 已在每次配置更改时停止并启动并重新启动多次,否则也是如此。

  8. The uid:gid of the MySite project directory has been set to www-data:www:data using sudo chown -R www-data:www:data * MySite 项目目录的 uid:gid 已设置为 www-data:www:data 使用sudo chown -R www-data:www:data *

Why am I still getting that bad gateway 502 error wherein upstream django application cannot be contacted due to permission issues?为什么我仍然收到错误的网关 502 错误,其中由于权限问题无法联系上游 django 应用程序?

Solved the issue by placing the project to /tmp directory.通过将项目放置到 /tmp 目录解决了这个问题。

nginx being run from www-data user was not able to access the internal directory MySite and thus the socket or the files placed there, despite being assigned to the user www-data.从 www-data 用户运行的 nginx 无法访问内部目录 MySite,因此尽管已分配给用户 www-data,但套接字或放置在那里的文件。

Now, my other question is regarding the cause of permission issue for nginx, despite providing the the uid and gid of the directory to www-data, what could have been the issue?现在,我的另一个问题是关于 nginx 权限问题的原因,尽管向 www-data 提供了目录的 uid 和 gid,但可能是什么问题?

Note: My user named ubuntu is a sudoer.注意:我的用户名为ubuntu是 sudoer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM