Serving statics while on FORCE_SCRIPT_NAME

Viewed 73

I'm struggling with serving static files while using apache2 proxy. I want to keep my django server on foo path like below.

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass /foo/ http://0.0.0.0:8000/
    ProxyPassReverse /foo/ http://0.0.0.0:8000/
</VirtualHost>

With this settings my application is not getting static files e.g. admin site. My app doesn't throw any errors but in my browser i got:

Failed to load resource: the server responded with a status of 404 (Not Found)

I tried to proxy static

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass /foo/ http://0.0.0.0:8000/
    ProxyPassReverse /foo/ http://0.0.0.0:8000/
    ProxyPass /static/ http://0.0.0.0:8000/static/
    ProxyPassReverse /static/ http://0.0.0.0:8000/static/
</VirtualHost>

And now all I get is:

[16/Dec/2021 10:54:14] "GET /api/ HTTP/1.1" 200 5763
[16/Dec/2021 10:54:14] "GET /static/rest_framework/css/bootstrap.min.css HTTP/1.1" 404 1949
[16/Dec/2021 10:54:14] "GET /static/rest_framework/css/bootstrap-tweaks.css HTTP/1.1" 404 1958
[16/Dec/2021 10:54:14] "GET /static/rest_framework/js/jquery-3.5.1.min.js HTTP/1.1" 404 1952

I ran python manage.py collectstatic and my settings.py are:

DEBUG = True
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
FORCE_SCRIPT_NAME = '/foo'

If I leave my proxy on / everything works fine. Is there any solution or another approach to keep my django app on path and still getting statics?

0 Answers
Related