0

The Error

When I test with file size more than 28.6MB (e.g. 30MB) from swagger ui, I get the following error response:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>502 Bad Gateway</title>
</head><body>
<h1>Bad Gateway</h1>
<p>The proxy server received an invalid
response from an upstream server.<br />
</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at ec2-00-00-0-000.compute-1.amazonaws.com Port 80</address>
</body></html>

Project info

  • Project type: dot net core web api (C#)
  • Hosted on: aws (ec2)

What I've done so far

I added a web.config file with the following content

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>

</configuration>

I added the following to the Startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<IISServerOptions>(options =>
    {
        options.MaxRequestBodySize = int.MaxValue;
    });

    services.Configure<FormOptions>(x =>
    {
        x.ValueLengthLimit = int.MaxValue;
        x.MultipartBodyLengthLimit = int.MaxValue;
        x.MultipartHeadersLengthLimit = int.MaxValue;
    });
    ...
    ...
    ...
}

Test model class:

public class TestModel
{
    public IFormFile TestFile { get; set; }
}

Test controller method:

[Route("uploadtest")]
[HttpPost]
[Consumes("multipart/form-data")]
public async Task<ActionResult> UploadTest([FromForm] TestModel fff)
{
    return Ok();
}

Extra info

I do not get the error response when I run it on my local pc from visual studio (IIS Express).

bakee
  • 28
  • 5

2 Answers2

0

You don't get the error in IIS Express because the issue is not with the project, but the hosting server that runs it. In this case with aws ec2. The default max file upload is 2MB, so anything beyond that will not go through. You will have to modify the configuration in the host server.

Here's a link that hopefully will help solve the issue: Amazon EC2 Ubuntu Instance Maximum File Upload Size

0

I was finally able to solved it by setting a KestrelServerOptions as below:

services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize  = int.MaxValue; 
});

Since aws was using kestrel that's why the problem was only happening in aws, not in my local IISExpress server.

Thanks to: https://stackoverflow.com/a/60964837/765416.

bakee
  • 28
  • 5