2

I am using Apache2 with Gunicorn/Django to deploy my app. However my app doesn't display the static files like CSS sheets etc.

I read many topics but I think I need help because I probably missed something...

Setting.py

ALLOWED_HOSTS = ['localhost']

STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
STATIC_ROOT = '/var/www/media/myapp/static/'

Apache2 VHost

<VirtualHost *:80>
    ServerName myapp.fr
    ServerAlias www.myapp.fr myapp.fr

    DocumentRoot /home/django-project/myapp/

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://localhost:9000/

    ProxyPass /static/ !
    Alias /static/ /var/www/media/myapp/static/

    <Directory /home/django-project/myapp/>
        Order deny,allow
        Allow from all
        Options -Indexes
    </Directory>

</VirtualHost>
4
  • What happens when you make a request for a static file? Do you get a 404 response? Commented May 31, 2018 at 12:40
  • Yes indeed. And this is the displayed path : <link rel="stylesheet" href="/static/css/base.css"> Commented May 31, 2018 at 13:08
  • Ok, and the file definitely exists in /var/www/media/myapp/static/? Commented May 31, 2018 at 13:41
  • Yes that's why I don't understand. Commented May 31, 2018 at 13:46

2 Answers 2

4

I think the exclusion for for ProxyPass /static/ ! should come before ProxyPass /

ProxyPass /static/ !
ProxyPass / http://localhost:9000/
Alias /static/ /var/www/media/myapp/static/

Otherwise the static request will be sent to gunicorn, which will return a 404.

The ProxyPass documentation says:

The configured ProxyPass and ProxyPassMatch rules are checked in the order of configuration. The first rule that matches wins.

Sign up to request clarification or add additional context in comments.

1 Comment

I was struggling for 4 days in a row to server static files, this answer helped me to fix it and launch to production, tnx
0

This is what has worked for me. as mentioned in the answer before. The ! directive will tell apache not to use reverse-proxy for a specific path, and yes, it needs to go before to take priority

<VirtualHost *:80>
    ServerName myprojectsite.se
    ServerAlias myprojectsite.se

    Alias /static/ /static/

    <Directory /static>
        Require all granted
    </Directory>

    ProxyPass /static/ ! #this goes before the reverse proxy to take priority
    ProxyPass / http://myproject:8000/
    ProxyPassReverse / http://myproject:8000/

</VirtualHost>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.