简体   繁体   中英

502 Bad Gateway nginx and ASP.NET Core 3.1 when upload big file

I've hosted website on linux server (Ubuntu) with .net core sdk installed alongside with nginx

the nginx default configuration (located in /etc/nginx/sites-available/default for my website

server {
        server_name   mywebsite.com;
        location / {
                proxy_pass         http://127.0.0.1:5902;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection keep-alive;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
                client_max_body_size 0;
                proxy_read_timeout 36000s;
                proxy_connect_timeout 36000s;
                proxy_send_timeout 36000s;
                proxy_buffering off;
                proxy_redirect off;
                proxy_request_buffering off;
                proxy_buffer_size 64k;
                proxy_buffers 16 32k;
                proxy_busy_buffers_size 64k;
                fastcgi_read_timeout 3600;
        }
}

and main nginx config ( /etc/nginx/nginx.conf ):

http
{
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  client_max_body_size 1000M;
  proxy_read_timeout 600;
  proxy_connect_timeout 600;
  proxy_send_timeout 600;

  ...
}

When upload 300 MB of file, seems I got 502 error and it crash.

I don't know why it happens

In Startup.cs

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

and in controller

[DisableRequestSizeLimit]
[Route("/api/info")]
[HttpPost]
public async Task<IActionResult> DetectInfo(IFormFile file)
{
            if (file == null)
            {
                return BadRequest();
            }

            string info = await file.ReadFormFileAsync();
            return Ok(Process(info));
}

where ReadFormFileAsync

public static async Task<string> ReadFormFileAsync(this IFormFile file)
{
            if (file == null || file.Length == 0)
            {
                return await Task.FromResult((string)null);
            }

            using (var bufferedStream = new BufferedStream(file.OpenReadStream()))
            {
                using (var reader = new StreamReader(bufferedStream))
                {
                    return await reader.ReadToEndAsync();
                }
            }
}

How to fix 502 error ?

I am not familiar that much with ASP.NET and how upload streams should be handled, but a "502" error from Nginx certainly means that the upload process is being terminated from your ASP.NET application and the Nginx simply says that it lost connection with the backend ( in your case it's the ASP.NET I believe ). Therefore, I would highly recommend you make sure that you are correctly using the upload methods for ASP.NET or check the logs of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM