Friday, October 2, 2015

How to setup nginx+uwsgi with CKAN

Recently I had to deploy CKAN website on web server. There is nothing difficult if just you follow official documentation and deploy CKAN with Apache. But my choice was uwsgi+nginx, because I always use this bundle.

After setting everything up like I usually do with django, I got below error in nginx log files for all static files:

[error] 1057#0: *70 upstream prematurely closed connection while reading response header from upstream
CKAN website was working but without static files. After quick research I found the reason. To solve this issue you need to pass extra parameter to uwsgi which is called SCRIPT_NAME

Basic configuration of Nginx+UWSGI

Below you can find my working basic configuration for nginx+uwsgi+CKAN.

1. files tree
/etc/ckan/default/
            production.ini  
            who.ini  
            wsgi.py #must have .py extension, because we reference it as "module" in uwsgi.ini
            ckan_conf/
              nginx.conf
              uwsgi.ini  
              uwsgi.log  
              uwsgi_params
              tmp/
                uwsgi.sock
                ckan.log #optional
Of course, you can move ckan_conf folder and all its content to any other location (but don't forget to specify correct path to these files).

2. nginx.conf
server {
    listen      80;

    server_name ckan-website.com;
    charset     utf-8;

    # max upload size
    client_max_body_size 1M;   # adjust to taste

    location / {
        uwsgi_pass  unix:/etc/ckan/default/ckan_conf/tmp/uwsgi.sock;
        include     /etc/ckan/default/ckan_conf/uwsgi_params;
        uwsgi_param SCRIPT_NAME '';
    }
}

3. uwsgi.ini
[uwsgi]
chdir           = /etc/ckan/default
module          = wsgi
home            = /usr/lib/ckan/default
logto = /etc/ckan/default/ckan_conf/uwsgi.log

master          = true
processes       = 1
socket          = /etc/ckan/default/ckan_conf/tmp/uwsgi.sock
chmod-socket    = 660
# clear environment on exit
vacuum          = true

Obviously you also need to give all necessary permissions to uwsgi.sock and log files, so that server could write to them. That's it. After you have these settings everything should work like a charm.

2 comments:

  1. It would be good to see the contents of /etc/ckan/default/wsgi.py

    ReplyDelete
  2. Dear,
    This configuration is incomplete.
    I am new in nginx and I need al steps do configure and run.
    Tks.

    ReplyDelete