英文:
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 = "/static/"
andDEBUG = 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:
<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?
答案1
得分: 1
Apache的Web服务器优先顺序相当复杂,可以在这里找到文档,但这里有很好的解释。在你的情况下,Location
块内的 ProxyPass
具有优先权,因为它是最后匹配的。你可以重新排列你的配置,将 <Directory>
指令放在 <Location>
块下面,或者你可以在最后创建另一个 <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 <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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论