繁体   English   中英

POST 方法仅适用于我的本地计算机,但不适用于远程服务器

[英]POST methods are only working on my local machine but not on remote server

我在我的 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

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

您还可以通过检查应用程序日志来验证这一点。 如果反向代理错误处理请求,那么它们可能最终不会到达您的 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 ,那么您的请求可能会正确通过。

另请参阅此相关答案,了解发布后 POST 端点出现的类似问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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