【问题标题】:POST methods are only working on my local machine but not on remote serverPOST 方法仅适用于我的本地计算机,但不适用于远程服务器
【发布时间】:2019-12-27 08:26:17
【问题描述】:

我在我的 Ubuntu 18.04 服务器上发布了一个 WEB API 应用程序 (.Net Core 3.1),它在 NGINX 后面运行。 我的 GET 方法在本地机器和远程服务器上都有效。但是我的 POST 方法只能在我的本地机器上工作,但不能在远程服务器上工作,尽管我尝试了很多东西。我从远程服务器收到 400(错误请求)错误。我无法弄清楚它背后的原因是什么。 这是我的代码:

 public class MyCls
{
    public string MyString { get; set; }
    public int MyInt { get; set; }
}

[Route("[controller]")]
[ApiController]
public class NewController : ControllerBase
{

    [HttpGet("MyGetRoute")]
    public string Method1()
    {
        return "Hello from server.";
    }

    [HttpPost("MyPostRoute")]
    public  MyCls Method2(MyCls str)
    {

            var options = new JsonSerializerOptions
            {
                // PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            };

        str.MyString = str.MyString + "  John";
        str.MyInt = str.MyInt + 2;
        return str;

    }

    [HttpPost("MyPostRoute2")]
    public string Method3(MyCls str2)
    {

        return str2.MyString;

    }
}

这是我在 html 页面中的代码:

 function postIt() {

        axios.post('New/MyPostRoute', {
            MyString: 'Fred', 
            MyInt: 4,
        }
        ).then(function (response) {
            document.getElementById("p1").innerHTML = response.data.myString + "   " + response.data.myInt;
            console.log(response.Text);
        })
            .catch(function (error) {
                console.log(error);
            })
            .finally(function () {

            });
    }

    function postIt2() {

        axios.post('New/MyPostRoute2', {
            MyString: 'Fred', 
        }
        ).then(function (response) {
            document.getElementById("p1").innerHTML = response.data;
            console.log(response.Text);
        })
            .catch(function (error) {
                 console.log(error);
            })
            .finally(function () {

            });
    }

    function getIt() {
        axios.get('New/MyGetRoute')
            .then(function (response) {
                document.getElementById("p1").innerHTML = response.data;
                console.log(response.Text);
            })
            .catch(function (error) {
                console.log(error);
            })
            .finally(function () {

            });
    }

这是我的 NGINX 配置:

    server {
    listen        80;
    listen [::]:80;

    index index.html home.html
    server_name   dotnet.xxxxxx.com www.dotnet.xxxxxx.com;
    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   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;
    }
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
   listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/xxxxxx.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/xxxxxx.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
 server {
    if ($host = dotnet.xxxxxx.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

【问题讨论】:

  • 既然你的问题是在把你的web应用放在 nginx之后出现的,你能不能也分享一下你的nginx配置?
  • @poke 我添加了 NGINX 配置。
  • 我认为你应该看看你的 Nginx 日志。添加到 /etc/nginx/nginx.conf 的第一行:error_log /var/log/nginx/error.log debug;之后重新启动 nginx,你应该有一些日志条目。

标签: c# asp.net-core nginx .net-core asp.net-core-webapi


【解决方案1】:

如果问题出现在 将其置于不同的环境之后,那么您应该始终尝试找出该环境的配置是否是问题的根源。在您的情况下,该应用程序在本地运行良好,因此在生产中部署时它不太可能突然中断。相反,您在此之前放置的反向代理更有可能是您的问题的原因。

您也可以通过检查您的应用程序日志来验证这一点。如果反向代理错误地处理请求,那么它们可能最终不会到达您的 ASP.NET Core 应用程序,或者它们最终会出现格式错误。因此,您应该在日志中什么也看不到,或者出现一些适当的错误。

在这种情况下,问题很可能与以下 nginx 配置有关:

proxy_set_header Connection 'upgrade';

您不应将Connection 标头永久设置为'upgrade'。升级连接仅在您想要切换协议时使用,最常见的是在您使用 WebSockets 时。在这种情况下,您只想在子路径上执行此操作,因为它会阻止正常请求正常工作。

设置Connection 标头的正确方法是使用'keep-alive',就像您所做的那样

proxy_set_header   Connection keep-alive;

无论如何,您应该只设置每个标题一次。如果您删除 Connection 'upgrade' 行并仅保留 Connection keep-alive 行,那么您的请求可能会正常通过。

另请参阅this related answer,了解发布后 POST 端点出现的类似问题。

【讨论】:

  • 靶心!!注释掉行“proxy_set_header Connection 'upgrade';”并重新启动 nginx 解决了这个问题。非常感谢亲爱的戳。
猜你喜欢
  • 2013-03-16
  • 1970-01-01
  • 2011-02-03
  • 2012-03-16
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
相关资源
最近更新 更多