asp net core 3发布后,对带有正文内容的POST操作获取BadRequest响应 [英] asp net core 3 Getting BadRequest response on POST action with body content after publish
问题描述
我正在使用asp.net core 3构建一个网站,该网站托管在Debian 9服务器上,并使用nginx作为反向代理.我遇到的问题是,当我在Visual Studio中本地运行应用程序时,我的POST请求可以工作,但是当我发布该应用程序并将其上传到我的服务器时,它们突然不再起作用了.
I'm building a website with asp.net core 3 which I'm hosting on a Debian 9 server with nginx as a reverse proxy. I'm running against this issue that my POST requests do work when I run the application locally in visual studio, but when I publish the application and upload it to my server, they suddenly don't work anymore.
这是我用于测试的控制器:
This is the controller that I'm using for testing:
public class TestController : Controller
{
[HttpPost]
public IActionResult Ping()
{
return Ok();
}
[HttpPost]
public IActionResult Echo(string message)
{
return Ok(message);
}
[HttpPost]
public IActionResult FormEcho([FromForm]string message)
{
return Ok(message);
}
}
我正在使用邮递员来测试这些端点.
I'm using postman to test these endpoints.
Ping
:
- 使用不带任何参数的发布请求会在本地和服务器上按预期提供200.
- 使用带有x-www-form-urlencoded内容的发布请求会在本地产生200,但在服务器上会产生400.
Echo
:
- 带有查询字符串参数的发布请求在本地和服务器上均返回代码为200的消息.
- 带有x-www-form-urlencoded内容的带有消息的发布请求在本地返回代码为200的消息,但在服务器上给出的消息为400.
FormEcho
:
- 带有x-www-form-urlencoded内容的带有消息的发布请求在本地返回代码为200的消息,但在服务器上给出的消息为400.
通过该测试,我认为我的服务器会以某种方式拒绝带有正文内容的发布请求.我还从日志中注意到,对于在服务器上返回400的请求,日志中没有任何内容,就好像该请求甚至没有到达我的应用程序一样.
From this test, I'd think that somehow post requests with body content are rejected on my server. I also noticed from the logs that for the requests that return a 400 on the server, there is nothing in the logs, as if the request doesn't even reach my application.
为什么我的应用程序拒绝包含正文内容的发帖请求?
这是我在nginx中的配置:
Here is my configuration in nginx:
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
server_name 51.38.35.10;
error_log /var/log/nginx/error.log info;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
}
}
server {
listen 80;
listen [::]:80;
server_name 51.38.35.10;
return 302 https://$server_name$request_uri;
}
推荐答案
如果ASP.NET Core应用程序甚至没有记录传入请求(而其他应用程序仍在工作),那么看来它实际上并没有收到任何传入请求.在这种情况下,可能是您的反向代理配置不允许这些请求传递到您的应用.
If the ASP.NET Core application does not even log incoming requests (while others work), then it appears that it doesn’t actually get any incoming requests. In that case, it is probably your reverse proxy configuration that isn’t letting these requests through to your app.
我通常通过nginx对ASP.NET Core应用程序使用以下配置:
I usually use the following configuration for ASP.NET Core applications through nginx:
location / {
proxy_pass http://localhost:5000;
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;
}
这也是 ASP.NET Core官方文档的推荐.
设置不同的Connection
标头值,尤其是Upgrade
,似乎有点问题.与WebSocket的HTTP Upgrade
标头一起使用时,我个人刚刚看到了Upgrade
值.
Setting a different Connection
header value, in particular Upgrade
, seems to be a bit problematic. I’ve personally just seen the Upgrade
value when used with a HTTP Upgrade
header for WebSockets.
因此,调整nginx配置可能会解决您的问题.
So adjusting the nginx configuration should probably solve your problem.
请注意,Connection keep-alive
这篇关于asp net core 3发布后,对带有正文内容的POST操作获取BadRequest响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!