My Droplet One-Click Django Server respponse 502 Bad GateWay Error.
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$
Here is gunicorn.conf
description "Gunicorn application server handling django_project"
start on runlevel [2345] stop on runlevel [!2345]
respawn setuid user setgid www-data chdir /home/user/django_project
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
Check the nginx configuration at $ ls /etc/nginx/conf.d/
if found any *.conf
file edit it if not, 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.