djangodockerapachedjango-staticfilesstatic-files

How to configuring Apache as a Reverse Proxy for a Django App in a Docker Container: Serving Static Files


I have Django app that runs in docker container. I have a problem with serving static files with my Apache configuration.

This is my apache config:

<VirtualHost 000.00.0.000:443>
    ServerName my_app.com

    SSLEngine on
    SSLProxyEngine on

    SSLCertificateFile /etc/apache2/certs/cert.pem
    SSLCertificateKeyFile /etc/apache2/certs/cert.key
    SSLCACertificateFile /etc/apache2/certs/DigiCertCA.crt

    CustomLog /var/log/apache2/my_app.log combined
    ErrorLog /var/log/apache2/my_app.log
    
    ProxyPass /static !  
    Alias /static/ /var/www/my_app/static/

    <Directory /var/www/my_app/static/>
        Require all granted
    </Directory>

    <Location />
        ProxyPass http://localhost:6000/
        ProxyPassReverse http://localhost:6000/
        ProxyPreserveHost on
    </Location>
</VirtualHost>

When I commented out:

<Location />
ProxyPass http://localhost:6000/
ProxyPassReverse http://localhost:6000/
ProxyPreserveHost on
</Location>

than my static files are served properly. So it looks like that there is some issue that the this part of the config takes precedence.

Could you help me please to resolve the issue?


Solution

  • You can tell your proxy to ignore a certain directory with the ProxyPass ! directive.
    Here is a working example

    Alias "/static/" "/home/intranet/intraBlog/static/"
    
    
    <Directory "/home/intranet/intraBlog/static">
        Require all granted
    </Directory>
    
    
    <Location "/">
        ProxyPass http://127.0.0.1:8000/
        ProxyPassReverse http://127.0.0.1:8000/
    </Location>
    
    <Location "/static">
        ProxyPass !
    </Location>