You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中Django应用Apache反向代理静态文件加载问题排查

解决Docker中Django+Apache静态文件加载问题

问题出在Apache配置的优先级上:<Location />块的配置优先级高于普通的ProxyPass和Alias指令,导致你写的ProxyPass /static !排除规则被覆盖,所有请求(包括/static)都被转发到了Django容器,自然无法加载静态文件。

修改Apache配置,把根路径的代理规则从<Location />块中移出,改为全局的ProxyPass指令,并且确保排除/static的规则在根代理规则之前:

<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
    
    # 先排除/static路径,不转发到Django
    ProxyPass /static !  
    Alias /static/ /var/www/my_app/static/

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

    # 全局代理根路径到Django容器,这行要放在排除规则之后
    ProxyPass / http://localhost:6000/
    ProxyPassReverse / http://localhost:6000/
    ProxyPreserveHost on
</VirtualHost>

修改完成后,重启Apache服务:

service apache2 restart

验证内容:

  • 访问https://my_app.com/static/下的静态资源,确认能正常加载
  • 访问Django应用的其他路径,确认应用能正常响应

内容的提问来源于stack exchange,提问作者Przemek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 11:35:20