简体   繁体   English

Yarp 反向代理 - 响应 502 错误网关

[英]Yarp Reverse Proxy - Response 502 Bad Gateway

I want to implement a reverse proxy using Yarp.我想使用 Yarp 实现反向代理。 The Api is up and running as i can send request to it. Api 已启动并正在运行,因为我可以向它发送请求。 However, anytime i try the same request through the proxy, i get an error 502 Bad gateway但是,每当我通过代理尝试相同的请求时,我都会收到错误 502 Bad gateway

Startup.cs启动.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddReverseProxy()
     .LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseRouting();
    // Register the reverse proxy routes
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapReverseProxy();
    });
}

Appsettings应用设置

"ReverseProxy": {
  "Routes": {
    "route1": {
      "ClusterId": "cluster1",
      "Match": {
        //"Methods": [ "GET", "POST", "PUT", "HEAD", "OPTIONS", "DELETE" ],
        "Path": "{**catch-all}"
      }
    }
  },
  "Clusters": {
    "cluster1": {
      "Destinations": {
        "cluster1/destination1": {
          "Address": "https://localhost:44339/"
        }
      }
    }
  }
}

I am getting a response - 502 bad gateway anytime i send a request through the proxy我收到响应 - 502 bad gateway 每当我通过代理发送请求时

You should click output and select ProjectName-Asp.Net Core Web Server to check the details.您应该单击output并选择ProjectName-Asp.Net Core Web Server以检查详细信息。

I use your code and also append another settings, and check the logs, I found the reason is http://localhost:44339 not start.我使用您的代码并附加了另一个设置,并检查了日志,我发现原因是 http://localhost:44339 无法启动。 It should use another webapp's port in local.它应该在本地使用另一个 webapp 的端口。

在此处输入图像描述

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  //"AllowedHosts": "*"

  "ReverseProxy": {
    "Routes": [
      {
        "ClusterId": "cluster1",
        "Match": {
          //"Methods": [ "GET", "POST", "PUT", "HEAD", "OPTIONS", "DELETE" ],
          "Path": "{**catch-all}"
        }
      },
      {
        // matches /something/* and routes to 2 external addresses
        "ClusterId": "cluster2",
        "Match": {
          "Path": "/something/{*any}"
        }
      }
    ],
  "Clusters": {
      "cluster1": {
        "Destinations": {
          "cluster1/destination1": {
            "Address": "http://localhost:44339/"
          }
        }
      },
      "cluster2": {
        "Destinations": {
          "first_destination": {
            "Address": "https://baidu.com"
          },
          "another_destination": {
            "Address": "https://bing.com"
          }
        },
        "LoadBalancingPolicy": "PowerOfTwoChoices"
      }

    }
  }
}

Test Result测试结果

default: https://localhost:44370默认:https://localhost:44370

在此处输入图像描述

https://localhost:44370/somthing https://localhost:44370/somthing

it will redirect to bing site.它将重定向到 bing 站点。

在此处输入图像描述

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

相关问题 远程服务器返回了意外的响应:(502)Azure WCF中的错误网关 - The remote server returned an unexpected response: (502) Bad Gateway in Azure WCF ASP.NET Core YARP 反向代理多个监听端口 - ASP.NET Core YARP Reverse Proxy multiple listen ports Azure网站 - 502 - Web服务器在充当网关或代理服务器时收到无效响应 - Azure Websites - 502 - Web server received an invalid response while acting as a gateway or proxy server UCWA获得502响应(Bad Gateway,策略阻止请求被代理) - UCWA getting a 502 response (Bad Gateway, policy prevents request from being proxied) 如何修复“502 - Web 服务器在充当网关或代理服务器时收到无效响应” - How to fix '502 - Web server received an invalid response while acting as a gateway or proxy server' Bot在测试中抛出502 Bad Gateway - Bot throwing 502 Bad Gateway on test 502 Bad Gateway /在passwordigninasync调用失败 - 502 Bad Gateway / failing on passwordsigninasync call 如何解决 502 Bad Gateway 错误 - How to troubleshoot 502 Bad Gateway error 为什么会发生“(502) Bad Gateway”错误? - Why "(502) Bad Gateway" error happen? 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM