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

Apache配置LimitRequestBody配合ProxyPass时不生效问题

问题:Apache 2.4的LimitRequestBody指令无法限制代理请求的文件大小

我使用Django开发应用,通过Gunicorn+Apache部署,应用支持以multipart表单提交图片。已在Django层面实现图片大小和尺寸限制,希望在Apache层面限制传递给Django的文件大小。图片由Django存储和获取,Apache不负责存储或分发这些上传的图片。

我的Apache配置如下:

<VirtualHost *:443>
    ServerName myserver.com
    ServerAdmin email@example.com

    # limit size of the body to 10mb
    LimitRequestBody 10485760

    Alias "/static" "/path/to/static/"
    <Directory /path/to/static/>
            Require all granted
    </Directory>
    ProxyPass /static !

    # set this header to allow gunicorn to know if request is https or not
    RequestHeader set X_FORWARDED_PROTO 'https' env=HTTPS

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8500/ 
    ProxyPassReverse / http://127.0.0.1:8500/


    SSLEngine on
    SSLCertificateFile /path/to/certificate.pem
    SSLCertificateKeyFile /path/to/privatekey.pem


    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" %{ms}T" combined_with_time
    CustomLog ${APACHE_LOG_DIR}/path/to/logs/access.log combined_with_time
    ErrorLog ${APACHE_LOG_DIR}/path/to/logs/error.log
</VirtualHost>

按配置预期,上传超过10MB的文件(测试使用约200MB的文件)时,Apache应返回413 Request Entity Too Large,但文件仍能到达http://127.0.0.1:8500/,由Django处理响应。根据Apache 2.4官方文档,未提及该指令不适用于文件上传,因此我认为其应能限制文件上传。另有资料指出Apache 2.2中LimitRequestBody不适用于代理请求,但该限制在2.4版本已移除,而我使用的正是2.4版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 02:16:09