问题
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
:
- using a post request without any parameters gives a 200 both locally and on the server as expected.
- using a post request with x-www-form-urlencoded content gives a 200 locally, but a 400 on the server.
Echo
:
- Post request with query string parameter returns the message with code 200 both locally and on the server.
- Post request with message as x-www-form-urlencoded content returns the message with code 200 locally, but gives a 400 on the server.
FormEcho
:
- Post request with message as x-www-form-urlencoded content returns the message with code 200 locally, but gives a 400 on the server.
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.
Why does my application reject post requests with body content?
EDIT:
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;
}
回答1:
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.
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;
}
That is also the recommendation from the official ASP.NET Core docs.
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.
So adjusting the nginx configuration should probably solve your problem.
Note that Connection keep-alive
来源:https://stackoverflow.com/questions/59234780/asp-net-core-3-getting-badrequest-response-on-post-action-with-body-content-afte