上传一个1MB的PDF时,我收到“上传太大,最大为4MB”的提示

当我上传小于 4MB 的 PDF 文件时,我收到了“上传文件过大,最大尺寸为 4MB”的错误消息。
以下是我的测试:

  • 68KB 的 PDF 文件按预期成功上传。
  • 2.3MB 的 JPG 文件按预期成功上传。
  • 一个 PDF 文件 (2.3 MB) 的 2.3MB 文件触发了错误!
  • 3.6MB 的 JPG 文件按预期成功上传。

奇怪的是,上传在 meta.discourse.org 上成功了,也许你更改了一些设置。

我没有修改任何包含 max 的设置。

我正在使用版本 2.9.0.beta9 4af080d20b,托管在 https://discuss.afpy.org 上的自托管实例。

我的论坛上没有这个问题,因为我没有更改文件大小限制。

1 个赞

在我的测试站点上运行它(4b561277a9),没有任何对 max 设置或文件限制的更改,如果我将 pdf 添加到 authorized extensions,它对我来说可以正常上传,并且在重置为默认值时会显示正确的错误消息“抱歉,您尝试上传的文件未获授权”。

我还能尝试什么来查看是否可以重现?

不,因为该文件大小低于默认的 nginx 大小限制。不过,这确实是一个有用的线索。

@Julien_Palard 自我托管的网站在 Discourse 和浏览器之间有一个自定义的反向代理,因此该错误可能来自配置错误的反向代理。

5 个赞

您的 Discourse 前面有反向代理吗?您是如何安装的?

我正在使用 nginx,我的配置如下:

所以我的 nginx 配置是:

      server
      {
          listen [::]:80; listen 80;
          server_name {{ discourse_domain }};
          access_log /var/log/nginx/{{ discourse_domain }}-access.log;
          error_log /var/log/nginx/{{ discourse_domain }}-error.log;
          return 301 https://$host$request_uri;
      }
      server
      {
          listen [::]:443 ssl; listen 443 ssl;
          server_name {{ discourse_domain }};
          access_log /var/log/nginx/{{ discourse_domain }}-access.log;
          error_log /var/log/nginx/{{ discourse_domain }}-error.log;
          include snippets/letsencrypt-{{ discourse_domain }}.conf;
          location / {
              proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
              proxy_set_header Host $http_host;
              proxy_http_version 1.1;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header X-Forwarded-Proto https;
              proxy_set_header X-Real-IP $remote_addr;
          }
      }

这是来自 Debian bullseye 的 nginx 1.18.0-6.1+deb11u2。

我收到 nginx 的 413 Request Entity Too Large 错误,所以显然我需要进一步研究,以了解为什么“大”图像可以正常工作,而“不算太大”的 PDF 却不行……

3.0MB 的 JPG 的 Content-Length 为 557215,所以出人意料地只有 0.5MB!
2.3MB 的 PDF 的 Content-Length 为 2460137,所以不出所料是 2.3MB。

Discourse 是否在上传前对 jpg 进行了缩放?

在我上传的 JPEG 文件中,上传到 Discourse 后,我看到图像的宽度和高度不同(尝试了 https://apod.nasa.gov/apod/image/2209/DSCF4968_PS_Lioce.jpg,这是一个 5703x3892、3.6MB 的图片,但一旦上传,它就变成了 1920×1310、153 KB 的图片)。

1 个赞

nginx 对 client_max_body_size 的默认设置为 1MB,客户端重新缩放的 jpeg 图片都小于 1MB,一切都清楚了。

我已经修复了我的 nginx 配置:Set client_max_body_size to 4m to match Discourse configuration. · AFPy/infra@861af29 · GitHub

现在可以正常工作了。你说得对:是我的反向代理的问题。

谢谢大家 :heart:,抱歉打扰了!

4 个赞

我不想错误地添加#unsupported-install标签,但我认为这个主题从一开始就应该有它,对吧?

嗯,有一些关于如何配置反向代理的解释性主题,所以这通常是我们能提供帮助的。

1 个赞

Angry Season 4 GIF by The Office

是的!我们甚至还有一篇关于这个超赞功能的博客文章

7 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.