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

  • The Apache's webserver order of precedence is pretty complicated and is documented here but explained beautifully here. In your case, the ProxyPass inside the Location block takes precedence as it wins the last match. Either you can arrange your configuration and place the <Directory> directive below your <Location> block, or you can create another <Location /my_app/static/ block at the end which will help you to better evaluate which block will match what.