简体   繁体   English

502 Gate Way-nginx / 1.10.3(Ubuntu)Dropplet一键式Django服务器

[英]502 Gate Way - nginx/1.10.3 (Ubuntu) Dropplet One-Click Django Server

My Droplet One-Click Django Server respponse 502 Bad GateWay Error. 我的Droplet一键式Django服务器响应502 Bad GateWay错误。

Here is nginx error.log 2018/02/14 06:09:09 [error] 1486#1486: *3960 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 60.191.38.77, server: _, re$ 2018/02/14 06:12:09 [error] 1493#1493: *1 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 31.223.26.233, server: _, requ$ 这是nginx error.log 2018/02/14 06:09:09 [错误] 1486#1486:* 3960 connect()到unix:/home/django/gunicorn.socket在连接到上游时失败(111:连接被拒绝) ,客户端:60.191.38.77,服务器:_,re $ 2018/02/14 06:12:09 [错误] 1493#1493:* 1对unix的connect():/ home / django / gunicorn.socket失败(111:连接到上游时拒绝连接),客户端:31.223.26.233,服务器:_,requ $

Here is gunicorn.conf 这是gunicorn.conf

description "Gunicorn application server handling django_project" 说明“ Gunicorn应用程序服务器处理django_project”

start on runlevel [2345] stop on runlevel [!2345] 在运行级别[2345]上启动在运行级别[!2345]上停止

respawn setuid user setgid www-data chdir /home/user/django_project 重新生成setuid用户setgid www-data chdir / home / user / django_project

exec myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/user/myproject/myproject.sock myproject.wsgi:application exec myprojectenv / bin / gunicorn --workers 3 --bind unix:/home/user/myproject/myproject.sock myproject.wsgi:application

First you need to edit all these configuration files to work and you need to give permissions for read and write or execute to the files 首先,您需要编辑所有这些配置文件才能工作,并且需要授予读写和执行这些文件的权限

$ touch gunicorn_start

$ sudo touch /etc/systemd/system/gunicorn.service

$ nano gunicorn_start

Here we will put our configuration file which will be used at our service later 在这里,我们将放置我们的配置文件,该文件将在以后的服务中使用

#!/bin/bash

NAME="djproject"
DJANGODIR=/path/to
USER=root
GROUP=root or whatever the user used
WORKERS=3
BIND=unix:/path/to/gunicorn.sock
DJANGO_SETTINGS_MODULE=mailqueue.settings
DJANGO_WSGI_MODULE=mailqueue.wsgi
LOGLEVEL=error

cd $DJANGODIR
source /path/to/venv/bin/activate
cd djangoproject
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$PROJECTDIR:$PYTHONPATH

exec /path/to/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $WORKERS \
  --user=$USER \
  --group=$GROUP \
  --bind=$BIND \
  --log-level=$LOGLEVEL \
  --log-file=-

Make the file executable 使文件可执行

$ chmod u+x gunicorn_start

Now we need to edit the service file to restart gracefully 现在我们需要编辑服务文件以正常重启

$ sudo echo "

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root # or your user
Group=root # or your grp     
WorkingDirectory=/your-project-path/
ExecStart=/path/to/gunicorn_start

[Install]
WantedBy=multi-user.target

" >  /etc/systemd/system/gunicorn.service

Finally 最后

Check the nginx configuration at $ ls /etc/nginx/conf.d/ if found any *.conf file edit it if not, touch proj.conf $ ls /etc/nginx/conf.d/检查nginx配置,如果发现任何*.conf文件,则编辑它,如果没有,请touch proj.conf

$ sudo echo "
upstream app_server {
    server unix:/path/to/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name 10.100.200.10;  # <- insert here the ip address/domain name

    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /var/logs/nginx-access.log;
    error_log /var/logs/nginx-error.log;

    location /static/ {
        alias /path/to/static/;
    }

    location /media/ {
        alias /path/to/media/;
    }

    location / {
        try_files \$uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
      proxy_set_header Host \$http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}
" >  /etc/nginx/conf.d/proj.conf;

Finally, make this your reference 最后,将此作为您的参考

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

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