[英]502 Bad Gateway nginx and ASP.NET Core 3.1 when upload big file
我在 linux 服务器 (Ubuntu) 上托管了网站,同时安装了 .net core sdk 和 nginx
nginx 默认配置(位于我的网站的/etc/nginx/sites-available/default
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;
}
}
和主要的 nginx 配置( /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;
...
}
当上传 300 MB 的文件时,似乎我收到了 502 错误并且它崩溃了。
我不知道为什么会这样
在Startup.cs
services.AddRazorPages();
...
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = long.MaxValue;
});
并在控制器中
[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));
}
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();
}
}
}
如何修复 502 错误?
我不太熟悉 ASP.NET 以及应该如何处理上传流,但是来自 Nginx 的“502”错误肯定意味着上传过程正在从您的 ASP.NET 应用程序中终止,而 Nginx 只是说它失去了连接使用后端(在您的情况下,我相信它是 ASP.NET)。 因此,我强烈建议您确保正确使用 ASP.NET 的上传方法或检查它的日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.