1

Right now I'm working on Django project and this is my first time with running Django app on server (Lighttpd) via gunicorn. The problem is - I run application with gunicorn like this:

$PYTHON $MANAGEPATH run_gunicorn -D -b 127.0.0.1:18002 -p $GUNICORN_PIDPATH --log-file $GUNICORN_LOGPATH

Part of Django settings.py:

from __future__ import absolute_import

import os
from socket import gethostname

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
HOSTNAME = gethostname()

def make_path(path):
    return os.path.join(BASE_DIR, path)

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = make_path('static_root')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

In /etc/lighttpd/vhost.d/my_conf.conf I have nice redirect for non-static files:

$SERVER["socket"] == ":82" {
    server.document-root = "/home/mefioo/public_html/generator/static"
    $HTTP["url"] !~ "\.(js|css|gif|jpg|png|ico|txt|swf|html|htm|svg|xlsx)$" {
        proxy.server = ("" => ( "localhost" => (
        "host" => "127.0.0.1",
        "port" => 18002,
        "fix-redirects" => 1
        ) ) )
    }
}

Idea - static files like .js or .css are in

/home/mefioo/public_html/generator/static

(which is proxy to static_root dir in Django app) and should be avaiable from url like

my_domain.com:82/my_file.js

Rest of the app, with all the urls like this:

my_domain.com:82/url

should look for django app like that:

127.0.0.1:18002/url

because of lighttpd conf.

The problem is - when I try to reach my static files, I got 404, because app is looking for

localhost:8080/my_file.js

instead of

my_domain.com:82/my_file.js

I don't understand why, especially when I have two instances of that app, one on my own server (second PC, doesn't work) and one on external VPS (works as intended). Am I running gunicorn wrong way, or missing something in lighttpd settings?

1 Answer 1

-1

I am new to django/gunicorn too but here is how I configured my app:

settings in settings.py

STATIC_ROOT = "/path to static files"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
   os.path.join(BASE_DIR, 'static'),
)

I'm not using lighttpd but nginx and here is the configuration:

server{
        server_name example.com www.example.com;

        access_log off;

location /static {
        autoindex on;
        alias /path_to_static_files;
    }
        location / {
                proxy_pass http://127.0.0.1:8000;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        }
    }

After that we have we have to collect the static files in the configured directory. This is done with: python3 manage.py collectstatic

All files should be copied in the correct directory and lightppd or nginx can serve them whereas gunicorn do the django/python - part

1
  • I of course did python manage.py collectstatic. They were all copied to proper dirs. If i manually paste my_domain.com:82/my_file.js the file IS there, the problem is, my app is trying to find it elsewhere... Nov 18, 2014 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.