uWSGI + Nginx, uWSGI can't receive request - django

I'm using uWSGI + Nginx to deploy a Django app. But uWSGI can't receive any request from Nginx.
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8e7250 pid: 20440 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 20440)
spawned uWSGI worker 1 (pid: 20445, cores: 1)
spawned uWSGI worker 2 (pid: 20446, cores: 1)
spawned uWSGI worker 3 (pid: 20447, cores: 1)
... no requests seen
Here's the my config files:
uwsgi.ini
[uwsgi]
chdir = /home/laike9m/Envs/blog/My_Blog
module = my_blog.wsgi:application
home = /home/laike9m/Envs/blog/
master = true
processes = 3
socket = /tmp/uwsgi.sock
chmod-socket = 664
vacuum = true
nginx.conf
server {
listen 80;
location /media {
alias /home/laike9m/media;
}
location /static {
alias /home/laike9m/static;
}
location / {
uwsgi_pass unix:/tmp/uwsgi.sock;
include uwsgi_params;
}
}
If you visit http://107.170.253.157/, it will give you 502 error.
In Nginx's log:
"GET / HTTP/1.1" 502 545 "-"
What am I doing wrong? Thank you!

A few things to try:
1. make uwsgi_pass the last directive in the location block
2. just for sanity testing purposes, chmod the socket 777 instead of 664. If that resolves it, look into who owns the socket and who nginx is running as.
3. change your uwsgi config to open an HTTP socket on an alternate port and connect to it with your browser, again just for sanity checking purposes.
You also note that the nginx log seems to have no information, but that looks like the nginx access log, not the error log. Does the error log show anything different?

Related

".sock" does not exist error; connect() error

I'm trying to set up my Mezzanine/Django project on a virtual Ubuntu 14.04 box on Linode, but get a "502 Bad Gateway error" when trying to navigate to my site in my browser.
I'm following the instructions here: https://linode.com/docs/web-servers/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04.
I ran git clone in /home/django/, so everything's in a folder named FOLDER.
/home/django/ has these directories: Env/ and FOLDER/.
This should give some idea of what FOLDER/ tree looks like:
FOLDER
- product_blog
-- product_blog
--- settings.py
--- wsgi.py
-- manage.py
In my settings.py, I have:
ALLOWED_HOSTS = ["PUBLIC IP OF MY LINODE UBUNTU INSTANCE HERE"]
wsgi.py has this:
"""
WSGI config for product_blog project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
from mezzanine.utils.conf import real_project_name
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"%s.settings" % real_project_name("product_blog"))
application = get_wsgi_application()
/etc/uwsgi/sites/product_blog.ini has this:
[uwsgi]
project = product_blog
base = /home/django
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 2
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true
/etc/init/uwsgi.conf has this:
description "uWSGI"
start on runlevel [2345]
stop on runlevel [06]
respawn
env UWSGI=/usr/local/bin/uwsgi
env LOGTO=/var/log/uwsgi.log
exec $UWSGI --master --emperor /etc/uwsgi/sites --die-on-term --uid django --gid www-data --logto $LOGTO
/etc/nginx/sites-available/product_blog has this:
server {
listen 80;
server_name mydomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/FOLDER;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/django/FOLDER/product_blog/product_blog.sock;
}
}
When I go to the IP address for the Linode box, I see a 502 Bad Gateway error.
/var/log/uwsgi.log has this...
*** has_emperor mode detected (fd: 6) ***
[uWSGI] getting INI configuration from product_blog.ini
*** Starting uWSGI 2.0.17 (64bit) on [Sun Apr 22 17:17:56 2018] ***
compiled with version: 4.8.4 on 22 April 2018 19:53:45
os: Linux-4.15.12-x86_64-linode105 #1 SMP Thu Mar 22 02:13:40 UTC 2018
nodename: ubuntu-linode
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/sites
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/django/product_blog
chdir(): No such file or directory [core/uwsgi.c line 2629]
chdir(): No such file or directory [core/uwsgi.c line 1644]
Sun Apr 22 17:17:56 2018 - [emperor] curse the uwsgi instance product_blog.ini (pid: 4238)
Sun Apr 22 17:17:59 2018 - [emperor] removed uwsgi instance product_blog.ini
And /var/log/nginx/error.log has this (identifying info removed):
2018/04/22 17:18:14 [crit] 3953#0: *9 connect() to unix:/home/django/FOLDER/product_blog/product_blog.sock failed (2: No such file or directory) while connecting to upstream, client: 73.49.35.42, server: mydomain.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/django/FOLDER/product_blog/product_blog.sock:", host: "ADDRESS TO LINODE BOX"
It seems that product_blog.sock is supposed to be somewhere in my directory, but it is not there. How do I fix the 502 error so that I can navigate my browser to the Linode box's address and see a working website?
The socket file is supposed to be created by uWSGI. But the uWSGI log is telling you that it can't start up because it can't cd to the directory you have specified in product_blog.ini, /home/django/product_blog.
I can't tell if FOLDER is a placeholder or not, but in any case you don't seem to have included it in that path. I suppose it should be:
chdir = %(base)/FOLDER/%(project)
...
socket = %(base)/FOLDER/%(project)/%(project).sock

Django + uWSGI + nginx requests hang

I'm running a Django web application using Nginx and uWSGI. I'm having problems with the requests hanging for no apparent reason.
I have added a bunch of logging in the application, and this snippet is where it seems to hang. There are two log lines at the start of the try block, and the first one gets printed, but not he second one, so it would seem that it hangs in the middle of the code. This code is from a middleware class that I added in the Django configuration.
def process_request(self, request):
if 'auth' not in request.session:
try:
log.info("Auth not found") # this line is logged
log.info("another log line") # this line is never logged
if request.is_ajax():
return HttpResponse(status=401)
...
I managed to get a backtrace from the uWSGI thread and this is where it's stuck:
*** backtrace of 76 ***
/usr/bin/uwsgi(uwsgi_backtrace+0x2e) [0x45121e]
/usr/bin/uwsgi(what_i_am_doing+0x30) [0x451350]
/lib/x86_64-linux-gnu/libc.so.6(+0x36c30) [0x7f8a4b2b8c30]
/lib/x86_64-linux-gnu/libc.so.6(epoll_wait+0x33) [0x7f8a4b37d653]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/gevent/core.so(+0x27625) [0x7f8a44092625]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/gevent/core.so(ev_run+0x29b) [0x7f8a4409d11b]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/gevent/core.so(+0x32bc0) [0x7f8a4409dbc0]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x4bd4) [0x7f8a4a0c30d4]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x80d) [0x7f8a4a0c517d]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(+0x162310) [0x7f8a4a0c5310]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyObject_Call+0x43) [0x7f8a4a08ce23]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(+0x7d30d) [0x7f8a49fe030d]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyObject_Call+0x43) [0x7f8a4a08ce23]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_CallObjectWithKeywords+0x47) [0x7f8a4a04b837]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/greenlet.so(+0x375c) [0x7f8a49b1c75c]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/greenlet.so(+0x30a6) [0x7f8a49b1c0a6]
[0x7f8a42f26f38]
*** end of backtrace ***
SIGUSR2: --- uWSGI worker 3 (pid: 76) is managing request /login?next=/&token=45092ca6-c1a0-4c23-9d44-4d171fc561b8 since Wed Dec 2 09:52:44 2015 ---
The Nginx error log prints out [error] 619#0: *55 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.17.0.1, server: vdr
There are no errors in the printouts from uWSGI, so I'm a bit at a loss. Has anyone seen anything similar? All this is running within a Docker container if that makes any difference.
Nginx conf:
upstream uwsgi {
server unix:///tmp/vdr.sock;
}
server {
listen 80;
charset utf-8;
client_max_body_size 500M;
server_name localhost 172.17.0.2;
location /static {
alias /home/vdr/vdr-ui/static;
}
location / {
include uwsgi_params;
uwsgi_pass uwsgi;
uwsgi_read_timeout 200s;
}
}
uWSGI conf:
[uwsgi]
chdir = %d
module = alft_ui.wsgi:application
uid=1000
master=true
pidfile=/tmp/vdr.pid
vacuum=true
max-requests=5000
processes=4
env=DJANGO_SETTINGS_MODULE=alft_ui.settings.prod-live
home=/home/vdr/vdr-ui/env
socket=/tmp/vdr.sock
chmod-socket=666
So I finally found the cause for this. It turns out that my setup script added some logstash settings to the Django configuration. These settings pointed to the IP 10.8.0.1 which wasn't reachable from this environment. This would explain why the app got stuck on a logging line. Removing these settings made everything work again.
Always good to know that it was your own fault all along :)

cant run uwsgi with ini file in emperror mode - Permission denied

I cant run uwsgi in emperror mode with my ini file. I have installed uwsgi with sudo pip install uwsgi.
When I try to start uwsgi, I get error:
sudo /etc/init.d/uwsgi start
/etc/init.d/uwsgi: line 72: /usr/share/uwsgi/init/snippets: No such file or directory
/etc/init.d/uwsgi: line 73: /usr/share/uwsgi/init/do_command: No such file or directory
[....] Starting app server(s): uwsgi/etc/init.d/uwsgi: line 80: do_command: command not found
failed!
I'm using Debian. Firstly I tried to follow django and nginx docs, but when it did not work I googled a lot.
This is my ebook_uwsgi.ini file:
# ebook_uwsgi.ini file
[uwsgi]
emperor = /etc/uwsgi/vassals
#plugins = python #if uwsgi installed with pip, dont have to use this line
# Django-related settings
# the base directory (full path)
chdir = /var/www/django/ebook/ebook/wsgi/ebook/
# Django's wsgi file
module = controller.wsgi:application
# the virtualenv (full path)
home = /var/www/django/ebook
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /var/uwsgi/ebook.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
uid = www-data
gid = www-data
# clear environment on exit
vacuum = true
no-site = True
wsgi-file = /var/www/django/ebook/ebook/wsgi/ebook/controller/wsgi.py
env = DJANGO_SETTINGS_MODULE=controller.settings # set an environment variable
And here is my ebook_nginx.conf:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///var/uwsgi/ebook.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8001;
# the domain name it will serve for
server_name IP #.bookdownloading.com; # 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 /var/www/django/ebook/ebook/data; # your Django project's media files - amend as required
}
location /static {
alias /var/www/django/ebook/ebook/wsgi/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
include /var/www/django/ebook/ebook/uwsgi_params; # the uwsgi_params file you installed
}
}
Where IP stands for servers real IP.
When I start uwgi with my ini file I get Permission denied:
uwsgi --ini ebook_uwsgi.ini
[uWSGI] getting INI configuration from ebook_uwsgi.ini
*** Starting uWSGI 2.0.10 (64bit) on [Tue Apr 14 17:11:32 2015] ***
compiled with version: 4.7.2 on 14 April 2015 16:47:40
os: Linux-2.6.32-042stab104.1 #1 SMP Thu Jan 29 12:58:41 MSK 2015
nodename: zoltan
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /var/www/django/ebook/ebook
detected binary path: /usr/local/bin/uwsgi
chdir() to /var/www/django/ebook/ebook/wsgi/ebook/
your processes number limit is 2062113
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
*** starting uWSGI Emperor ***
bind(): Permission denied [core/socket.c line 230]
I am using socked in /var/uwsgi/ebook.sock where I have set www-data as owner of uwsgi directory:
ls -la /var
total 128
drwxr-xr-x 13 root root 15 apr 14 16:29 .
drwxr-xr-x 23 root root 23 apr 12 19:46 ..
drwxr-xr-x 2 root root 19 apr 13 06:25 backups
...
drwxr-xr-x 2 www-data www-data 2 apr 14 16:36 uwsgi
Why do I get Permission denied?
//EDIT:
I was missing some config settings. So here is my new ini file:
# ebook_uwsgi.ini file
[uwsgi]
#emperor = /etc/uwsgi/vassals
#plugins = python #if uwsgi installed with pip, dont have to use this line
# Django-related settings
# the base directory (full path)
chdir = /var/www/django/ebook/ebook/wsgi/ebook/
# Django's wsgi file
module = controller.wsgi:application
# the virtualenv (full path)
home = /var/www/django/ebook
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 1
# the socket (use the full path to be safe
socket = /var/uwsgi/ebook.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
uid = www-data
gid = www-data
# clear environment on exit
vacuum = true
no-site = True
wsgi-file = /var/www/django/ebook/ebook/wsgi/ebook/controller/wsgi.py
#env = DJANGO_SETTINGS_MODULE=controller.settings # set an environment variable
and nginx settings:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream ebook {
server unix:///var/uwsgi/ebook.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8001 default_server;
# the domain name it will serve for
server_name IP; #.bookdownloading.com; # 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 /var/www/django/ebook/ebook/data; # your Django project's media files - amend as required
}
location /static {
alias /var/www/django/ebook/ebook/wsgi/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass ebook;
include /var/www/django/ebook/ebook/uwsgi_params; # the uwsgi_params file you installed
}
}
No matter if I run uwsgi or not, I get 502 Bad Gateway. Error nginx log:
2015/05/12 16:22:08 [crit] 3002#0: *1 connect() to unix:///var/uwsgi/ebook.sock failed (13: Permission denied) while connecting to upstream, client: 78.45.37.119, server: IP, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///var/uwsgi/ebook.sock:", host: "IP:8001"
Hm, even though I have set owner of stocket file to www-data, it was created as my user and nginx could not read it.
I've read your post and you're getting the permission issue because maybe www-data don't have write permissions on the folder uwsgi, you have two ways of testing it:
Log in with the www-data user and try to create a file inside /var/uwsgi/
To log in with www-data user su - www-data
Change the folder permissions to 775 (chmod 775 /var/uwsgi/)
Another option could be try to change the path of the sockets to the tmp folder. I tried your configuration in a VM using tmp folder and it works perfectly
ebook_uwsgi.ini:
socket = /tmp/ebook.sock
ebook_nginx.conf:
upstream django {
server unix:///tmp/ebook.sock;
}
I too faced the same.
/usr/share/uwsgi/init/snippets and /usr/share/uwsgi/init/do_commands
Above two files are missing with pip install uwsgi
But apt-get install uwsgi adds above two files and resolves issue
Somehow, when I run uwsgi from my linux user, uwsgi ignored my ini file and created ebook.sock with owner and group of my user, not specified www-data. When I changed owner and group manually to www-data, web loaded.
Problem was with permissions for file uwsgi_params. After using nginx file it worked all right.

uwsgi ImportError: No module named os

I'm teaching myself how to setup an Ubuntu Server to run my Django application. I want to use Nginx + uwsgi. I know that this question can be very easy for experts but I've spent 6 days looking for it over the internet without getting it (in any case, forgive me if there is any link with the answer). I've followed a lot of tutorials and posts but I didn't found a solution.
I describe my file structure below:
My django project is located in /usr/local/projects/myproject
My virtualenv is in /root/.virtualenvs/myproject
My uwsgi config file myproject.ini is in /etc/uwsgi/apps-available/ and correctly symbolic linked in /etc/uwsgi/apps-enabled/
[uwsgi]
plugins = python
socket = /tmp/myproject.sock
chmod-socket = 644
uid = www-data
gid = www-data
master = true
enable-threads = true
processes = 2
no-site=true
virtualenv = /root/.virtualenvs/myproject
chdir = /usr/local/projects/myproject
module = myproject.wsgi:application
pidfile = /usr/local/projects/myproject/myproject.pid
logto = /var/log/uwsgi/myproject_uwsgi.log
vacuum = true
My nginx config file myproject.conf is in /etc/nginx/sites-available/ and correctly symbolic linked in /etc/nginx/sites-enabled/
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/myproject.sock; # for a file socket
}
server {
listen 80;
server_name dev.myproject.com www.dev.myproject.com;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
location / {
uwsgi_pass unix:///tmp/myproject.sock;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_SCRIPT myproject.wsgi;
}
location /media/ {
alias /usr/local/projects/myproject/media/;
}
location /static/ {
alias /usr/local/projects/myproject/static/;
}
}
When I try to access to dev.myproject.com I get an Internal Server Error. Then I take a look to my uwsgi log:
Traceback (most recent call last):
File "./myproject/wsgi.py", line 9, in <module>
import os
ImportError: No module named os
Sat Jul 26 17:39:16 2014 - unable to load app 0 (mountpoint='') (callable not found or import error)
Sat Jul 26 17:39:16 2014 - --- no python application found, check your startup logs for errors ---
[pid: 8559|app: -1|req: -1/8] 79.148.138.10 () {40 vars in 685 bytes} [Sat Jul 26 17:39:16 2014] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 1 headers in 57 bytes (0 switches on core 0)
I need your help because I'm not able to find a solution despite the posibility of being very simple.
If you need to know something else let me know and I will update my question as soon as possible.
I finally found a solution.
I followed kchan's suggestion about not putting any of the contents in the /root/ directory. Basically, I did some small changes in my myproject.conf file and my myproject.ini file. I created a user and structured everything like below:
uwsgi config file myproject.ini in /etc/uwsgi/apps-available/ and correctly symbolic linked in /etc/uwsgi/apps-enabled/
[uwsgi]
plugins = python
socket = /tmp/myproject.sock
chmod-socket = 644
uid = www-data
gid = www-data
master = true
enable-threads = true
processes = 2
virtualenv = /home/user/.virtualenvs/myproject
chdir = /home/user/projects/myproject
module = myproject.wsgi:application
pidfile = /home/user/projects/myproject/myproject.pid
daemonize = /var/log/uwsgi/myproject_uwsgi.log
vacuum = true
nginx config file myproject.conf in /etc/nginx/sites-available/ and correctly symbolic linked in /etc/nginx/sites-enabled/
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/myproject.sock; # for a file socket
}
server {
listen 80;
server_name dev.myproject.com www.dev.myproject.com;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
location /media/ {
alias /home/user/projects/myproject/media/;
}
location /static/ {
alias /home/user/projects/myproject/static/;
}
}
I must say that I think the real problem was to try to setup my DB configuration in the postactivate file of my virtualenv. Hope to help someone else.

Nginx + uWSGI basic configuration

I'm new to both, I got to run 2 Django skeleton apps (just shows the "It works!" page) using Emperor, but I want to try it without Emperor. (to better understand how it works)
My nginx.conf:
# snipped...
server {
listen 92;
server_name example.com;
access_log /home/john/www/example.com/logs/access.log;
error_log /home/john/www/example.com/logs/error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
}
}
# snipped...
And I start uWSGI by:
$ uwsgi --ini /home/john/www/example.com/uwsgi.ini
With uwsgi.ini being:
[uwsgi]
http = :8001
chdir = /home/john/www/example.com/example
module = example.wsgi
master = True
home = /home/john/Envs/example.com
Once uwsgi and nginx are running, I can access localhost:8001, but not localhost:92.
What am I missing?
Thanks in advance.
You are telling the uwsgi process to serve applications using the http protocol. This feature is meant mainly for developer convenience. You should instead tell it to use the uwsgi protocol:
[uwsgi]
protocol = uwsgi
socket = 127.0.0.1:8001
chdir = /home/john/www/example.com/example
module = example.wsgi
master = True
home = /home/john/Envs/example.com