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?
I am new to django/gunicorn too but here is how I configured my app:
settings in settings.py
I'm not using lighttpd but nginx and here is the configuration:
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