Running Django on Hetzner Managed Server (FastCGI): Static files not found

I managed to run my Django projects on a Hetzner Managed Server with FastCGI following this tutorial, see also this post).

However, static files do not work. For easier testing, I created a brand new django project and added the following to the settings.py as described in the docs:


STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_URL = "static/"

I already ran python manage.py collectstatic and files like staticfiles/admin/css/base.css were created. However, when I access these static files via browser, I get a 404 error.

It makes no difference if DJANGO is set to False or True. After each edit of settings.py, I killed all running fcgi processes to force a reload.

I am aware that Django’s build in FastCGI support has dropped long time ago, but as far as I understand, it should still work when properly configured by hand. And for small projects, it’s still OK to use it, right?

So I would be very thankful for any help.

Currently, I am using Python 3.11.2 and Django 5.1.7.

Meanwhile, I found out by myself how this works: the static files have to be served by the web server itself, not by the FastCGI application. Therefore, the staticfiles folder has to be located in the public_html folder.

So, in the settings.py file, I changed the STATIC_ROOT to /usr/home/myaccount/public_html/mydjangoproject/staticfiles. Then I ran collectstatic again.

In the .htaccess file, I added one line with a new RewriteRule :

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^static/(.*)$ staticfiles/$1 [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ mydjangoproject.fcgi/$1 [QSA,L]
</IfModule>

Don’t forget to kill running fcgi processes if there are any.

1 Like