django、gunicorn 和 Nginx 的 502 错误
Posted
技术标签:
【中文标题】django、gunicorn 和 Nginx 的 502 错误【英文标题】:502 error with django, gunicorn and Nginx 【发布时间】:2017-10-01 13:12:16 【问题描述】:几天来一直在尝试设置 django 以与 nginx 和 gunicorn 一起工作,遵循digital ocean 设置指南,尝试了在这个论坛中找到的许多解决方案,但没有一个对我有用。
这是我的第一篇 *** 帖子,如果根据您喜欢阅读的帖子格式错误或写得不好,我想听听,以便我改进。
从文件夹启动 gunicorn
/home/django/virtualenv/sio/SiO
运行命令
gunicorn --bind 0.0.0.0:8000 SiO.wsgi:application
工作得很好,我尝试将其设置为 sock 文件夹。但是我最近的尝试是针对 wsgi.py 文件所在的 /home/django/virtualenv/sio/SiO/SiO 。
查看我发现的 nginx 错误日志时
2017/05/03 13:56:34 [emerg] 29248#29248: invalid number of arguments in "proxy_pass" directive in /etc/nginx/sites-enabled/sio:12
2017/05/03 13:58:54 [crit] 29275#29275: *1 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 14:03:53 [crit] 29275#29275: *4 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 14:06:02 [crit] 29422#29422: *1 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 14:11:51 [crit] 29422#29422: *5 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 14:13:10 [crit] 29422#29422: *9 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 14:25:34 [crit] 29422#29422: *11 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 14:51:30 [crit] 712#712: *1 connect() to unix:/home/django/virtualenv/sio/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO.sock:/", host: "67.207.75.163"
2017/05/03 15:00:41 [crit] 954#954: *1 connect() to unix:/home/django/virtualenv/sio/SiO/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO/SiO.sock:/", host: "67.207.75.163"
2017/05/03 15:02:00 [crit] 1069#1069: *1 connect() to unix:/home/django/virtualenv/sio/SiO/SiO.sock failed (2: No such file or directory) while connecting to upstream, client: 158.39.197.123, server: 67.207.75.163, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/virtualenv/sio/SiO/SiO.sock:/", host: "67.207.75.163"
我的项目中的任何地方都没有 .sock 文件。我试过this 修复。
项目结构树状图:
|-- SiO
| |-- calapp
| | |-- migrations
| | | |-- __pycache__
| | | | `-- __init__.cpython-35.pyc
| | | `-- __init__.py
| | |-- __pycache__
| | | |-- admin.cpython-35.pyc
| | | |-- __init__.cpython-35.pyc
| | | |-- models.cpython-35.pyc
| | | |-- urls.cpython-35.pyc
| | | `-- views.cpython-35.pyc
| | |-- static
| | | |-- css
| | | | |-- calendar.css
| | | | |-- responsive.css
| | | | `-- sidebar.css
| | | `-- js
| | | |-- calendar.js
| | | `-- jquery-1.12.4.min.js
| | |-- templates
| | | `-- calapp
| | | |-- calendar (copy).html
| | | `-- calendar.html
| | |-- admin.py
| | |-- apps.py
| | |-- forms.py
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- chart
| | |-- migrations
| | | |-- __pycache__
| | | | `-- __init__.cpython-35.pyc
| | | `-- __init__.py
| | |-- __pycache__
| | | |-- admin.cpython-35.pyc
| | | |-- __init__.cpython-35.pyc
| | | |-- models.cpython-35.pyc
| | | |-- urls.cpython-35.pyc
| | | `-- views.cpython-35.pyc
| | |-- static
| | | |-- css
| | | | `-- chart.css
| | | `-- js
| | | `-- chart.js
| | |-- templates
| | | `-- chart
| | | `-- ChartView.html
| | |-- admin.py
| | |-- apps.py
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- CoAdmin
| | |-- migrations
| | | |-- __pycache__
| | | | |-- 0001_initial.cpython-35.pyc
| | | | |-- 0002_auto_20170413_0316.cpython-35.pyc
| | | | |-- 0003_auto_20170413_1514.cpython-35.pyc
| | | | `-- __init__.cpython-35.pyc
| | | |-- 0001_initial.py
| | | |-- 0002_auto_20170413_0316.py
| | | |-- 0003_auto_20170413_1514.py
| | | `-- __init__.py
| | |-- __pycache__
| | | |-- admin.cpython-35.pyc
| | | |-- forms.cpython-35.pyc
| | | |-- __init__.cpython-35.pyc
| | | |-- models.cpython-35.pyc
| | | |-- serializers.cpython-35.pyc
| | | |-- urls.cpython-35.pyc
| | | `-- views.cpython-35.pyc
| | |-- static
| | | `-- css
| | | `-- signup.css
| | |-- templates
| | | `-- CoAdmin
| | | |-- admin_delete.html
| | | |-- admin_edit.html
| | | |-- admin_overview.html
| | | |-- InnsideSignup.html
| | | |-- password.html
| | | `-- signup.html
| | |-- admin.py
| | |-- apps.py
| | |-- forms.py
| | |-- __init__.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- core
| | |-- migrations
| | | |-- __pycache__
| | | | `-- __init__.cpython-35.pyc
| | | `-- __init__.py
| | |-- __pycache__
| | | |-- admin.cpython-35.pyc
| | | |-- forms.cpython-35.pyc
| | | |-- __init__.cpython-35.pyc
| | | |-- models.cpython-35.pyc
| | | |-- urls.cpython-35.pyc
| | | `-- views.cpython-35.pyc
| | |-- static
| | | |-- css
| | | | |-- animate.css
| | | | |-- cover.css
| | | | |-- dashboard.css
| | | | |-- network.css
| | | | `-- profile.css
| | | |-- img
| | | | |-- logo.svg
| | | | `-- SiOLogo.png
| | | `-- js
| | | `-- picture.js
| | |-- templates
| | | `-- core
| | | |-- cover.html
| | | |-- dashboard.html
| | | |-- partial_settings_menu.html
| | | |-- password.html
| | | |-- profile.html
| | | `-- settings.html
| | |-- admin.py
| | |-- apps.py
| | |-- forms.py
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- member
| | |-- migrations
| | | |-- __pycache__
| | | | |-- 0001_initial.cpython-35.pyc
| | | | `-- __init__.cpython-35.pyc
| | | |-- 0001_initial.py
| | | `-- __init__.py
| | |-- __pycache__
| | | |-- admin.cpython-35.pyc
| | | |-- forms.cpython-35.pyc
| | | |-- __init__.cpython-35.pyc
| | | |-- models.cpython-35.pyc
| | | |-- serializers.cpython-35.pyc
| | | |-- urls.cpython-35.pyc
| | | `-- views.cpython-35.pyc
| | |-- static
| | | `-- css
| | | |-- member_overview.css
| | | `-- member_signup.css
| | |-- templates
| | | `-- member
| | | |-- asoc_signup.html
| | | |-- member_confirm_delete.html
| | | |-- member_delete.html
| | | |-- member_edit.html
| | | |-- member_overview.html
| | | `-- member_signup.html
| | |-- admin.py
| | |-- apps.py
| | |-- forms.py
| | |-- __init__.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- post
| | |-- migrations
| | | |-- __pycache__
| | | | |-- 0001_initial.cpython-35.pyc
| | | | |-- 0002_auto_20170413_1514.cpython-35.pyc
| | | | |-- 0003_remove_email_sendstatus.cpython-35.pyc
| | | | `-- __init__.cpython-35.pyc
| | | |-- 0001_initial.py
| | | |-- 0002_auto_20170413_1514.py
| | | |-- 0003_remove_email_sendstatus.py
| | | `-- __init__.py
| | |-- __pycache__
| | | |-- admin.cpython-35.pyc
| | | |-- __init__.cpython-35.pyc
| | | |-- mail.cpython-35.pyc
| | | |-- models.cpython-35.pyc
| | | |-- urls.cpython-35.pyc
| | | `-- views.cpython-35.pyc
| | |-- static
| | | |-- css
| | | | `-- post.css
| | | `-- javascript
| | | `-- post.js
| | |-- templates
| | | `-- post
| | | `-- post.html
| | |-- admin.py
| | |-- apps.py
| | |-- __init__.py
| | |-- mail.py
| | |-- models.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- __pycache__
| | |-- __init__.cpython-35.pyc
| | |-- settings.cpython-35.pyc
| | |-- urls.cpython-35.pyc
| | `-- wsgi.cpython-35.pyc
| |-- static
| | |-- css
| | | |-- jquery.Jcrop.min.css
| | | `-- SiO.css
| | |-- img
| | | |-- sio-logo.jpg
| | | `-- user.png
| | `-- js
| | |-- date.js
| | |-- ga.js
| | |-- jquery.bullseye-1.0-min.js
| | |-- jquery.Jcrop.min.js
| | |-- jquery.selection.js
| | |-- jquery.typeahead.bundle.js
| | |-- SiO.js
| | `-- SiO.markdown.js
| |-- templates
| | |-- admin
| | | `-- chart_association.html
| | |-- 403.html
| | |-- 404.html
| | |-- 500.html
| | `-- base.html
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
|-- staticfiles
| |-- admin
| | |-- css
| | | |-- base.css
| | | |-- changelists.css
| | | |-- dashboard.css
| | | |-- fonts.css
| | | |-- forms.css
| | | |-- login.css
| | | |-- rtl.css
| | | `-- widgets.css
| | |-- fonts
| | | |-- LICENSE.txt
| | | |-- README.txt
| | | |-- Roboto-Bold-webfont.woff
| | | |-- Roboto-Light-webfont.woff
| | | `-- Roboto-Regular-webfont.woff
| | |-- img
| | | |-- gis
| | | | |-- move_vertex_off.svg
| | | | `-- move_vertex_on.svg
| | | |-- calendar-icons.svg
| | | |-- icon-addlink.svg
| | | |-- icon-alert.svg
| | | |-- icon-calendar.svg
| | | |-- icon-changelink.svg
| | | |-- icon-clock.svg
| | | |-- icon-deletelink.svg
| | | |-- icon-no.svg
| | | |-- icon-unknown-alt.svg
| | | |-- icon-unknown.svg
| | | |-- icon-yes.svg
| | | |-- inline-delete.svg
| | | |-- LICENSE
| | | |-- README.txt
| | | |-- search.svg
| | | |-- selector-icons.svg
| | | |-- sorting-icons.svg
| | | |-- tooltag-add.svg
| | | `-- tooltag-arrowright.svg
| | `-- js
| | |-- admin
| | | |-- DateTimeShortcuts.js
| | | `-- RelatedObjectLookups.js
| | |-- vendor
| | | |-- jquery
| | | | |-- jquery.js
| | | | |-- jquery.min.js
| | | | `-- LICENSE-JQUERY.txt
| | | `-- xregexp
| | | |-- LICENSE-XREGEXP.txt
| | | |-- xregexp.js
| | | `-- xregexp.min.js
| | |-- actions.js
| | |-- actions.min.js
| | |-- calendar.js
| | |-- cancel.js
| | |-- change_form.js
| | |-- collapse.js
| | |-- collapse.min.js
| | |-- core.js
| | |-- inlines.js
| | |-- inlines.min.js
| | |-- jquery.init.js
| | |-- popup_response.js
| | |-- prepopulate_init.js
| | |-- prepopulate.js
| | |-- prepopulate.min.js
| | |-- SelectBox.js
| | |-- SelectFilter2.js
| | |-- timeparse.js
| | `-- urlify.js
| |-- css
| | |-- animate.css
| | |-- bootstrap-datetimepicker.css
| | |-- bootstrap.min.css
| | |-- calendar.css
| | |-- chart.css
| | |-- cover.css
| | |-- dashboard.css
| | |-- datetimepicker.css
| | |-- jquery.Jcrop.min.css
| | |-- member_overview.css
| | |-- member_signup.css
| | |-- network.css
| | |-- post.css
| | |-- profile.css
| | |-- responsive.css
| | |-- sidebar.css
| | |-- signup.css
| | `-- SiO.css
| |-- fonts
| | |-- glyphicons-halflings-regular.eot
| | |-- glyphicons-halflings-regular.svg
| | |-- glyphicons-halflings-regular.ttf
| | |-- glyphicons-halflings-regular.woff
| | `-- glyphicons-halflings-regular.woff2
| |-- img
| | |-- logo.svg
| | |-- sio-logo.jpg
| | |-- SiOLogo.png
| | `-- user.png
| |-- javascript
| | `-- post.js
| |-- js
| | |-- locales
| | | |-- bootstrap-datetimepicker.ar.js
| | | |-- bootstrap-datetimepicker.bg.js
| | | |-- bootstrap-datetimepicker.ca.js
| | | |-- bootstrap-datetimepicker.cs.js
| | | |-- bootstrap-datetimepicker.da.js
| | | |-- bootstrap-datetimepicker.de.js
| | | |-- bootstrap-datetimepicker.ee.js
| | | |-- bootstrap-datetimepicker.el.js
| | | |-- bootstrap-datetimepicker.es.js
| | | |-- bootstrap-datetimepicker.fi.js
| | | |-- bootstrap-datetimepicker.fr.js
| | | |-- bootstrap-datetimepicker.he.js
| | | |-- bootstrap-datetimepicker.hr.js
| | | |-- bootstrap-datetimepicker.hu.js
| | | |-- bootstrap-datetimepicker.id.js
| | | |-- bootstrap-datetimepicker.is.js
| | | |-- bootstrap-datetimepicker.it.js
| | | |-- bootstrap-datetimepicker.ja.js
| | | |-- bootstrap-datetimepicker.ko.js
| | | |-- bootstrap-datetimepicker.kr.js
| | | |-- bootstrap-datetimepicker.lt.js
| | | |-- bootstrap-datetimepicker.lv.js
| | | |-- bootstrap-datetimepicker.ms.js
| | | |-- bootstrap-datetimepicker.nb.js
| | | |-- bootstrap-datetimepicker.nl.js
| | | |-- bootstrap-datetimepicker.no.js
| | | |-- bootstrap-datetimepicker.pl.js
| | | |-- bootstrap-datetimepicker.pt-BR.js
| | | |-- bootstrap-datetimepicker.pt.js
| | | |-- bootstrap-datetimepicker.ro.js
| | | |-- bootstrap-datetimepicker.rs.js
| | | |-- bootstrap-datetimepicker.rs-latin.js
| | | |-- bootstrap-datetimepicker.ru.js
| | | |-- bootstrap-datetimepicker.sk.js
| | | |-- bootstrap-datetimepicker.sl.js
| | | |-- bootstrap-datetimepicker.sv.js
| | | |-- bootstrap-datetimepicker.sw.js
| | | |-- bootstrap-datetimepicker.th.js
| | | |-- bootstrap-datetimepicker.tr.js
| | | |-- bootstrap-datetimepicker.ua.js
| | | |-- bootstrap-datetimepicker.uk.js
| | | |-- bootstrap-datetimepicker.zh-CN.js
| | | `-- bootstrap-datetimepicker.zh-TW.js
| | |-- bootstrap-datetimepicker.js
| | |-- calendar.js
| | |-- chart.js
| | |-- date.js
| | |-- ga.js
| | |-- jquery-1.12.4.min.js
| | |-- jquery.bullseye-1.0-min.js
| | |-- jquery.Jcrop.min.js
| | |-- jquery.selection.js
| | |-- jquery.typeahead.bundle.js
| | |-- picture.js
| | |-- SiO.js
| | `-- SiO.markdown.js
| `-- rest_framework
| |-- css
| | |-- bootstrap.min.css
| | |-- bootstrap-tweaks.css
| | |-- default.css
| | `-- prettify.css
| |-- docs
| | |-- css
| | | |-- base.css
| | | |-- bootstrap.min.css
| | | |-- bootstrap-theme.min.css
| | | |-- font-awesome-4.0.3.css
| | | |-- highlight.css
| | | `-- jquery.json-view.min.css
| | |-- fonts
| | | |-- fontawesome-webfont.eot
| | | |-- fontawesome-webfont.svg
| | | |-- fontawesome-webfont.ttf
| | | |-- fontawesome-webfont.woff
| | | |-- glyphicons-halflings-regular.eot
| | | |-- glyphicons-halflings-regular.svg
| | | |-- glyphicons-halflings-regular.ttf
| | | |-- glyphicons-halflings-regular.woff
| | | `-- glyphicons-halflings-regular.woff2
| | |-- img
| | | |-- favicon.ico
| | | `-- grid.png
| | `-- js
| | |-- api.js
| | |-- base.js
| | |-- bootstrap.min.js
| | |-- highlight.pack.js
| | |-- jquery-1.10.2.min.js
| | `-- jquery.json-view.min.js
| |-- fonts
| | |-- glyphicons-halflings-regular.eot
| | |-- glyphicons-halflings-regular.svg
| | |-- glyphicons-halflings-regular.ttf
| | |-- glyphicons-halflings-regular.woff
| | `-- glyphicons-halflings-regular.woff2
| |-- img
| | |-- glyphicons-halflings.png
| | |-- glyphicons-halflings-white.png
| | `-- grid.png
| `-- js
| |-- ajax-form.js
| |-- bootstrap.min.js
| |-- coreapi-0.1.0.js
| |-- csrf.js
| |-- default.js
| |-- jquery-1.12.4.min.js
| `-- prettify-min.js
|-- manage.py
`-- requirements.txt
我的 gunicorn 文件位于
/home/django/virtualenv/sio/bin/gunicorn
/etc/systemd/system/gunicorn.service 的内容
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=django
Group=www-data
WorkingDirectory=/home/django/virtualenv/sio/SiO
ExecStart=/home/django/virtualenv/sio/bin/gunicorn --workers 3 --bind unix:/home/django/virtualenv/sio/SiO/SiO SiO.wsgi:application
[Install]
WantedBy=multi-user.target
还有我的 /etc/nginx/sites-available/sio 文件
server
listen 80;
server_name 67.207.75.163;
location = /favicon.ico access_log off; log_not_found off;
location /static/
root /home/django/virtualenv/sio/SiO;
location /
include proxy_params;
proxy_pass http://unix:/home/django/virtualenv/sio/SiO/SiO.sock;
该站点在尝试仅使用 django 内置的 inn 网络服务器运行时工作。尝试使用 gunicorn 访问时出现 502 错误。
【问题讨论】:
nginx 和 gunicorn 作为不同的用户运行。 nginx 可以读取 /home/django 吗?如果 unix 套接字有问题,为什么不使用 tcp/ip 直到你解决这个问题? 【参考方案1】:进行您建议的更改并没有解决问题,尝试您在新 VM 中发送的指南确实可以解决所有问题。谢谢!非常好,通常数字海洋指南是我的首选,但你也说服我去其他地方看看 :)。
【讨论】:
【参考方案2】:您在这一行中缺少“.sock”:
ExecStart=/home/django/virtualenv/sio/bin/gunicorn --workers 3 --bind unix:/home/django/virtualenv/sio/SiO/SiO SiO.wsgi:application
您绑定到文件夹而不是 sock 文件,但您告诉 nginx 查找 /home/django/virtualenv/sio/SiO/SiO.sock
那么,不妨试试这个:
ExecStart=/home/django/virtualenv/sio/bin/gunicorn --workers 3 --bind unix:/home/django/virtualenv/sio/SiO/SiO.sock SiO.wsgi:application
编辑:顺便说一句,我是这个设置的忠实粉丝,不能高度推荐它 - http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
【讨论】:
以上是关于django、gunicorn 和 Nginx 的 502 错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥 nginx 不会用 django 和 gunicorn 显示静态内容?
Django、Nginx、Gunicorn 和 AngularJS 应用程序结构
使用 Django、Nginx、Gunicorn 和主管进行空日志记录
Gunicorn 和 Django 与 Upstart 和 Nginx