如何将Apache配置为Django应用程序在Docker容器中的反向代理:提供静态文件

huangapple go评论69阅读模式
英文:

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

问题

我有一个在Docker容器中运行的Django应用程序。我在使用Apache配置时遇到了静态文件的问题。

  • 我的Django应用程序在Docker容器内工作,Apache能够正确路由请求。
  • 我确保设置了STATIC_URL = "/static/"DEBUG = False这两个属性。
  • 我确保将静态文件收集到/var/www/my_app/static/目录中。
  • 我确保/var/www/my_app目录对于Apache来说具有正确的访问权限:drwxr-xr-x 3 www-data www-data 4,0K 6月 11 16:25 my_app

这是我的Apache配置:

<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>

当我注释掉以下部分时:

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

然后我的静态文件会被正确提供。看起来好像这部分配置占主导地位时会出现问题。

你能帮我解决这个问题吗?

英文:

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

  • My django app works inside docker container and Apache correctly routes requests to it
  • I made sure to set STATIC_URL = &quot;/static/&quot; and DEBUG = False settings properties
  • I made sure to collect static to /var/www/my_app/static/ directory
  • I made sure that directory /var/www/my_app has correct acccess rights for apache: drwxr-xr-x 3 www-data www-data 4,0K June 11 16:25 my_app

This is my apache config:

&lt;VirtualHost 000.00.0.000:443&gt;
    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/

    &lt;Directory /var/www/my_app/static/&gt;
        Require all granted
    &lt;/Directory&gt;

    &lt;Location /&gt;
        ProxyPass http://localhost:6000/
        ProxyPassReverse http://localhost:6000/
        ProxyPreserveHost on
    &lt;/Location&gt;
&lt;/VirtualHost&gt;

When I commented out:

&lt;Location /&gt;
ProxyPass http://localhost:6000/
ProxyPassReverse http://localhost:6000/
ProxyPreserveHost on
&lt;/Location&gt;

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?

答案1

得分: 1

Apache的Web服务器优先顺序相当复杂,可以在这里找到文档,但这里有很好的解释。在你的情况下,Location 块内的 ProxyPass 具有优先权,因为它是最后匹配的。你可以重新排列你的配置,将 &lt;Directory&gt; 指令放在 &lt;Location&gt; 块下面,或者你可以在最后创建另一个 &lt;Location /my_app/static/ 块,这将帮助你更好地评估哪个块将匹配什么。

英文:

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 &lt;Directory&gt; directive below your &lt;Location&gt; block, or you can create another &lt;Location /my_app/static/ block at the end which will help you to better evaluate which block will match what.

huangapple
  • 本文由 发表于 2023年6月12日 15:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定