gunicorn, nginx and postgres - OperationalError: no connection to the server

290 views
Skip to first unread message

Radim Novotny

unread,
Mar 13, 2012, 4:06:25 PM3/13/12
to djang...@googlegroups.com
Hi,

I'm preparing a LFS shop 0.7b2 on Debian squeeze, Python 2.6, with PostgreSQL 8.4 as SQL backend, default gunicorn configuration and nginx 1.0.6 as a frontend. There is one strange issue though. If I run gunicorn (in both debug mode and production mode), some requests to the site fails with

File "env/lib/python2.7/site-packages/django/db/backends/__init__.py", line 50, in _rollback
return self.connection.rollback()
OperationalError: no connection to the server

Postgres is running on the same VPS (openvz) and is not used by any other application. I'm alone on the site, because it is not public so there are no concurrent requests (maybe only requests to static files) I've been googling and found this email thread http://lists.gunicorn.org/user/0027.html which describes the same issue (the traceback above is from this email thread).

I'd like to ask LFS users if anyone is running LFS in the same environment (Postgres, gunicorn) and if anyone experience similar behavior. It seems strange to me that Django + Postgres + gunicorn won't work properly by default.

Thank you,
Radim

Radim Novotny

unread,
Mar 16, 2012, 8:04:50 AM3/16/12
to django-lfs
FYI: I ended up using uWSGI instead of gunicorn.

Radim

On Mar 13, 9:06 pm, Radim Novotny <novotny.ra...@gmail.com> wrote:
> Hi,
>
> I'm preparing a LFS shop 0.7b2 on Debian squeeze, Python 2.6,  with PostgreSQL 8.4 as SQL backend, default gunicorn configuration and nginx 1.0.6 as a frontend. There is one strange issue though. If I run gunicorn (in both debug mode and production mode), some requests to the site fails with
>
>   File "env/lib/python2.7/site-packages/django/db/backends/__init__.py", line 50, in _rollback
>     return self.connection.rollback()
> OperationalError: no connection to the server
>
> Postgres is running on the same VPS (openvz) and is not used by any other application. I'm alone on the site, because it is not public so there are no concurrent requests (maybe only requests to static files) I've been googling and found this email threadhttp://lists.gunicorn.org/user/0027.htmlwhich describes the same issue (the traceback above is from this email thread).

sbmart

unread,
Mar 19, 2012, 9:23:22 AM3/19/12
to django-lfs


On Mar 16, 4:04 pm, Radim Novotny <novotny.ra...@gmail.com> wrote:
> FYI: I ended up using uWSGI instead of gunicorn.
>
Hello, can you show your uWSGI config that runs with the lfs?

Radim Novotny

unread,
Mar 19, 2012, 10:02:45 AM3/19/12
to djang...@googlegroups.com
Hi,

1) install uwsgi to your system (not to your LFS buildout, but in general, it should not matter)

2) set-up djangorecipe to generate wsgi script (wsgi=true)

3) create file uwsgi.xml in buildout root (next to buildout.cfg) and put this into it:

<uwsgi>
<pythonpath>FULL PATH TO YOUR LFS_PROJECT LIKE /home/django/lfs/lfs_project</pythonpath>
<app mountpoint="/">
<script>django_wsgi</script>
</app>
</uwsgi>

(lfs_project is the folder containing settings.py etc)

4) symlink (or copy) django.wsgi to lfs_project/django_wsgi.py

ln -s /home/django/lfs/bin/django.wsgi /home/django/lfs/lfs_project/django_wsgi

From my understanding, uWSGI tries to import python module specified in the XML file using python-importable manner, so that it should be present in folder with __init__.py and should end with .py extension. I may be wrong, but it works for me.

5) create for example /var/log/uwsgi/ /var/run/uwsgi/ /var/run/uwsgi/ and change owner/group to your django user if you want to start uSWGI as django user

6) start uwsgi using the following command (or similar). Please note -C option which chmods the socket so that it is readable by everyone.

uwsgi -d /var/log/uwsgi/lfs.log -C -L -M -p 2 -s /var/run/uwsgi/lfs.sock -x /home/django/lfs/uwsgi.xml --pidfile /var/run/uwsgi/lfs.pid --logdate

7) configure and restart nginx using this config, for example:

server {
listen 80;
server_name my.shop.com;

location /static/ {
root /home/django/lfs/lfs_project/;
}

location /favicon.ico {
root /home/django/lfs/lfs_project/mytheme/static/img;
}


access_log /var/log/nginx/lfs.access.log;
client_max_body_size 0;
keepalive_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
send_timeout 300;

location / {
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Scheme $scheme;
uwsgi_read_timeout 600; # you may need this for long running tasks
include uwsgi_params;
uwsgi_pass unix:/var/run/uwsgi/lfs.sock; # point this to your uWSGI .sock file
}
}


Hope this helps,
Radim

> --
> You received this message because you are subscribed to the Google Groups "django-lfs" group.
> To post to this group, send email to djang...@googlegroups.com.
> To unsubscribe from this group, send email to django-lfs+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-lfs?hl=en.
>

Reply all
Reply to author
Forward
0 new messages