4mb 图像文件上传在上传到生产服务器时出现 HTTP 413 错误

4mb image file upload gives HTTP 413 error when uploading to production server

我正在尝试将图像从客户端上传到服务器进行处理,然后再上传到 S3 AWS 存储桶。它在本地服务器上运行良好,但在实时服务器上运行不佳。

XMLHttpRequest cannot load https://api.parthenon-click.com/img_upload/cover. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.parthenon-click.com' is therefore not allowed access. The response had HTTP status code 413.

我将 ElasticBeanstalk PHP v5.4 用于客户端服务器,将 Node v7.0 与 Express v4 服务器用于后端。客户端很简单 html/css/jQuery。我从用户库中抓取一张照片并使用以下方式将其传递到后端:

    $.ajax({
      url: api_url,
      type: 'POST',
      data: form,
      processData: false,
      contentType: false,
      success: function(data){
          console.log('upload successful!');
      },
      error: function(err){
          console.log(err);
      }
});

只要文件小于 1mb,它就可以工作并保存图像。尽管我仍然收到 504 HTTP 错误。 只要我是 运行 本地服务器,无论文件大小如何,它都能正常工作。

所以我尝试提高 body-parserhtaccessmulter 限制:

正文解析器:

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit:50000 }));
app.use(bodyParser.raw({ limit: '10mb'}) );

穆尔特:

var upload = multer({
    storage: storage,
    limits: {
        fileSize: 10000000
    }
})

.htaccess:

php_value upload_max_filesize 30M

我的头文件适用于所有其他类型的 api 请求。我使用以下方式设置它们:

router.use(function(req, res, next) {
    // do logging
        //set CORS policy to 'allow'
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    next();
});

最后但同样重要的是,所有域版本都启用了 SSL。

None这个问题已经解决了。我在这里错过了什么?

在 Node 之前使用像 Nginx 这样的网络服务器时,所有请求都会通过 Nginx 并在到达 Node.js 之前由 nginx 验证。

Nginx 的配置文件默认位于 /etc/nginx/nginx.conf。这个文件有很多所有请求的默认属性,其中之一是 client_max_body_size。 属性 的默认值为 1m,就像您的 1MB 文件一样。

这也是您的 .htaccess 文件没有执行任何操作的原因(Node.js 本身从不对 .htaccess 执行任何操作,除非您愿意)

您可以手动将其更改为不同的数字,这将是 nginx 的新最大主体大小(在重新加载 nginx 之后),但是如果您想要 "full" 控制并让应用程序检查主体大小,您也可以将其设置为 0,以禁用检查客户端请求正文大小。
Setting size to 0 disables checking of client request body size.

请注意,更改 属性 也会更改 PHP 请求的最大上传大小。