nginx uwsgi 502 Bad Gateway - python

My nginx and uwsgi configuration perfectly working for first one or two requests . Then nginx showing 502 Bad Gateway .When I restart the uwsgi service , same thing happens again. I am using Ubuntu 16.04. Here is my all conf and error log:
nginx conf:
upstream book {
server unix:///tmp/book.sock;
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
......
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
charset utf-8;
client_max_body_size 100M;
access_log /var/log/nginx/example.com_access.log;
error_log /var/log/nginx/example.com_error.log;
location /media {
alias /home/prism/prod/example.com/media;
}
location /static {
alias /home/prism/prod/example.com/static;
}
location / {
uwsgi_pass book;
include /etc/nginx/uwsgi_params;
}
}
/var/log/nginx/example.com_error.log
2016/05/25 17:44:26 [error] 5230#5230: *214 connect() to unix:///tmp/book.sock failed (111: Connection refused) while connecting to upstream, client: 27.*.*.*, server: example.com, request: "GET / HTTP/2.0", upstream: "uwsgi://unix:///tmp/book.sock:", host: "example.com"
hiren.ini
[uwsgi]
chdir=/home/prism/prod/example.com
home = /home/prism/prod/example.com/.env
module=hiren.wsgi
master=True
process = 5
pidfile=/tmp/book.pid
socket= /tmp/book.sock
vacuum=True
max-requests=5000
daemonize=/home/prism/prod/example.com/hiren.log
uid = www-data
gid = www-data
die-on-term = true
and service file:
[Unit]
Description=uWSGI instance to serve example.com
[Service]
User=prism
ExecStart=/bin/bash -c 'cd /home/prism/prod/example.com; source .env/bin/activate; uwsgi --ini hiren.ini'
[Install]
WantedBy=multi-user.target

Solved the problem by tweaking systemd script
[Unit]
Description=uWSGI instance to serve example.com
[Service]
ExecStart=/bin/bash -c 'su prism; cd /home/prism/prod/example.com; source .env/bin/activate; uwsgi --ini hiren.ini'
[Install]
WantedBy=multi-user.target

If you are able to run uwsgi and your .ini file within your virtual environment, and nginx is starting adequately, the issue is then with a systemd file.

Related

Nginx Permission Denied but has permission (nginx, gunicorn, django)

I have tried to get rid of the error message for over 6 hours, but I can't still figure it out. At First, I had my Django+React project directory at /home/pi/pidjango and I got a 403 Forbidden(css, js) Error message from Nginx, so I searched for a long time and I moved my project (pidjango) to /home/pi/.local/share and it seemed to be fine. Then I got a 502 Bad Gateway Error, and also fixed that (It was a problem from nginx) and I got 403 error again. It is the error about nginx not getting static files(css, js) and I gave chmod 755 (-rwxr-xr-x) and it still doesn't work. Can anybody solve this? Thank you.
I tried this tutorial(but except postgres)
$ sudo tail -F /var/log/nginx/error.log
*3 open() "/home/pi/.local/share/pidjango/static/css/main.js" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /static/frontend/main.js HTTP/1.1", host:"127.0.0.1", referrer: "http://127.0.0.1"
*3 open() "/home/pi/.local/share/pidjango/static/css/index.css" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /static/frontend/index.css HTTP/1.1", host:"127.0.0.1", referrer: "http://127.0.0.1"
*3 open() "/home/pi/.local/share/pidjango/static/css/main.js" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /static/frontend/main.js HTTP/1.1", host:"127.0.0.1", referrer: "http://127.0.0.1"
# nginx (sites-enabled)
server {
listen 80;
server_name localhost 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/pi/.local/share/pidjango;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/.local/share/pidjango
ExecStart=/home/pi/.local/share/pidango/djenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock diary.wsgi:application
[Install]
WantedBy=multi-user.target
gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Directory
/home/pi/.local/share/pidjango
-diary (folder that contains wsgi.py, settings.py, etc)
-djenv (virtual env)
-frontend (react)
-static (static folder)
-db.sqlite3
-manage.py
I have made sure I gave chmod 755 to all the static files in /static
Can anybody help?
*I have written all the scripts above by hand(not copy-pasting) so there could be typos

sock file is missing in running django app with nginx and gunicorn

I was following this tutorial and successfully ran the project manually. However, after setting up nginx and systemd, it says 502 Bad Gateway.
I have looked through other similar threads to no avail.
I see that my gunicorn workers are running by doing ps -ax | grep gunicorn.
my nginx configuration:
server {
listen 8000;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
}
}
and the systemd file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
The contents of /var/log/nginx/error.log:
2017/02/18 17:57:51 [crit] 1977#1977: *6 connect() to unix:/home/ubuntu/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.2.2, server: 172.30.1.5, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/myproject/myproject.sock:/", host: "localhost:8000"
Manually running /home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application is also working. That is, sock file is created.
I feel like I missing something very simple.
What could I be doing wrong?
There could be an ordering thing here - nginx is probably starting before gunicorn, so the socket is not yet there to connect. You should add gunicorn.service to the After directive in the nginx systemd file.

nginx + gunicorn + django not working

I am taking the help of following docs http://tutos.readthedocs.io/en/latest/source/ndg.html and trying to run the server. I have following file in sites-enabled :
upstream test_server {
server unix:/var/www/test/run/gunicorn.sock fail_timeout=10s;
}
# This is not neccessary - it's just commonly used
# it just redirects example.com -> www.example.com
# so it isn't treated as two separate websites
server {
listen 80;
server_name dehatiengineers.in;
return 301 $scheme://www.dehatiengineers.in$request_uri;
}
server {
listen 80;
server_name www.dehatiengineer.in;
client_max_body_size 4G;
access_log /var/www/test/logs/nginx-access.log;
error_log /var/www/test/logs/nginx-error.log warn;
location /static/ {
autoindex on;
alias /var/www/test/ourcase/static/;
}
location /media/ {
autoindex on;
alias /var/www/test/ourcase/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://test_server;
break;
}
}
#For favicon
location /favicon.ico {
alias /var/www/test/test/static/img/favicon.ico;
}
#For robots.txt
location /robots.txt {
alias /var/www/test/test/static/robots.txt ;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/test/ourcase/static/;
}
}
My server's domain name is dehatiengineers.in.
The gunicorn_start.sh file is:
#!/bin/bash
NAME="ourcase" #Name of the application (*)
DJANGODIR=/var/www/test/ourcase # Django project directory (*)
SOCKFILE=/var/www/test/run/gunicorn.sock # we will communicate using this unix socket (*)
USER=root # the user to run as (*)
GROUP=webdata # the group to run as (*)
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=ourcase.settings # which settings file should Django use (*)
DJANGO_WSGI_MODULE=ourcase.wsgi # WSGI module name (*)
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /var/www/test/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /var/www/test/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind=unix:$SOCKFILE
Now, I am running both of my nginx and gunicorn_start.sh file. nginx is running, as you can see at http://www.dehatiengineers.in/ but the django is not connected to nginx. Now on sudo sh gunicorn_start.sh, I am getting following output:
Starting ourcase as root
gunicorn_start.sh: 16: gunicorn_start.sh: source: not found
[2016-05-20 06:21:45 +0000] [10730] [INFO] Starting gunicorn 19.5.0
[2016-05-20 06:21:45 +0000] [10730] [INFO] Listening at: unix:/var/www/test/run/gunicorn.sock (10730)
[2016-05-20 06:21:45 +0000] [10730] [INFO] Using worker: sync
[2016-05-20 06:21:45 +0000] [10735] [INFO] Booting worker with pid: 10735
But, in browser, I am just getting nginx output, not the original one.
Apart from this, I am having issue in running gunicorn_ourcase.service file:
[Unit]
Description=Ourcase gunicorn daemon
[Service]
Type=simple
User=root
ExecStart=/var/www/test/gunicorn_start.sh
[Install]
WantedBy=multi-user.target
running issues:
$ systemctl enable gunicorn_ourcase
systemctl: command not found
Please help me to solve this.
Everything I was doing was fine, except the fact that nginx was using it's default config file. I had to delete/edit the file located in /etc/nginx/sites-enabled/ , with name default. After editing it, everything works fine.

django running with gunicorn and nginx: 400 bad request

I'm trying to deploy my django version 1.6 project using nginx and gunicorn. In my server, I added my project nginx file:
error_log /var/log/nginx/myproject-error.log;
access_log /var/log/nginx/myproject-access.log;
server {
listen 80;
server_name <domain_name>;
root <path_to_my_root_project>;
location /static/ {
root <path_to_my_root_project>;
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Schema $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8333/;
}
error_page 500 502 503 504 /static/50x.html;
}
In my django settings file, I set static paths:
# Static
STATIC_ROOT = BASE_DIR + '/static'
STATIC_URL = '/static/'
And the I run gunicorn as following:
$ gunicorn_django -b localhost:8333
!!!
!!! WARNING: This command is deprecated.
!!!
!!! You should now run your application with the WSGI interface
!!! installed with your project. Ex.:
!!!
!!! gunicorn myproject.wsgi:application
!!!
!!! See https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/gunicorn/
!!! for more info.
!!!
So when I run a curl on my server curl localhost, I get <h1>Bad Request (400)</h1> and the logs don't say much further than 127.0.0.1 - - [18/Oct/2014:14:38:57 +0200] "GET / HTTP/1.1" 400 37 "-" "curl/7.26.0"
And gunicorn stays still, as if no request is sent to it.
Any ideas?
UPDATE:
I run gunicor: gunicorn myproject.wsgi -b localhost:8333
The curl localhost returns my static/50x.html error page. And I got this in nginx logs:
2014/10/18 15:06:31 [error] 16037#0: *32 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: servername.local.fr, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8333/", host: "localhost"
This one tells you what to do.
!!! You should now run your application with the WSGI interface
!!! installed with your project. Ex.:
!!!
!!! gunicorn myproject.wsgi:application
NGINX is working fine, the gunicorn just isn't receiving your request.
Just to to your project dir and run gunicorn with proper parameters as described here: http://docs.gunicorn.org/en/latest/run.html#gunicorn-django

Serving django with nginx and uWSGI

I followed this post to serve my django project. The project runs well with manage.py runserver and I want to set it up for production. Here are my setting files:
nginx.conf:
upstream django {
server /tmp/vc.sock;
#server 10.9.1.137:8002;
}
server {
listen 8001;
server_name 10.9.1.137;
charset utf-8;
client_max_body_size 25M;
location /media {
alias /home/deploy/vc/media;
}
location /static {
alias /home/deploy/vc/static;
}
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
}
uwsgi.ini:
[uwsgi]
chdir = /home/deploy/vc
wsgi-file = vc/wsgi.py
master = true
processes = 2
#socket = :8002
socket = /tmp/vc.sock
chmod-socket = 666
vacuum = true
If I use TCP port socket (server 10.9.1.137:8002 and socket = :8002), it's going to be fine. However if I comment them out and use Unix sockets(server /tmp/vc.sock and socket = /tmp/vc.sock), the server will return 502 error. How should I fix it?
EDIT
Here's the nginx error log when I run /etc/init.d/nginx restart
nginx: [emerg] invalid host in upstream "/tmp/vc.sock" in /etc/nginx/conf.d/vc.conf:2
nginx: configuration file /etc/nginx/nginx.conf test failed
And this is the warning when I run uwsgi --ini vc/uwsgi.ini:
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Can't I run uWSGI as root?
roberto's comment should be an answer!
the syntax in nginx for unix socket path is wrong, you need to prefix
it with unix:
Check your nginx error log, very probably is telling you it does not have permissions to the socket. Unix sockets honour file system permissions, so nginx must have write privilege over the socket file. Short answer: 664 is not enough, you need 666

Categories

Resources