ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.37k stars 1.64k forks source link

Bad Request with Ocelot ReRoute using Eureka as Service Provider at LocalHost #1053

Closed JayChuah95 closed 7 months ago

JayChuah95 commented 5 years ago

Expected Behavior

Reroute to /api/User/GetStuff

Actual Behavior

Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.

Steps to Reproduce the Problem

Project Link: https://github.com/JayChuah95/AntyChatty/tree/master/AntyChatty

  1. Startup multiple project and go to url 'localhost:10000/Auth/GetStuff'

My ocelot.json

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/User/GetStuff",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/Auth/GetStuff",
      "UseServiceDiscovery": true,
      "ServiceName": "AuthService",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/api/User/GetAuthorize",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/Auth/GetAuthorize",
      "UseServiceDiscovery": true,
      "ServiceName": "AuthService",
      "UpstreamHttpMethod": [ "Get" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "ApiSecurity",
        "AllowedScopes": []
      }
    }
  ],
  "GlobalConfiguration": {
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration",
    "ServiceDiscoveryProvider": {
      "Type": "Eureka",
      "Host": "localhost",
      "Port": "8761"
    }
  }
}

AuthService appsettings.json

{
  "spring": {
    "application": {
      "name": "AuthService"
    }
  },
  "eureka": {
    "client": {
      "serviceUrl": "http://localhost:8761/eureka",
      "shouldRegisterWithEureka": true,
      "shouldFetchRegistry": true,
      "validateCertificates": false
    },
    "instance": {
      "port": 20000
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "AppSettings": {
    "Secret": "0265776e-b490-4253-9be4-7d73457700b1"
  }
}

Specifications

JayChuah95 commented 5 years ago

Output Message

ApiGateway>       Request starting HTTP/1.1 GET http://localhost:10000/Auth/GetStuff  
ApiGateway> info: Ocelot.RateLimit.Middleware.ClientRateLimitMiddleware[0]
ApiGateway>       requestId: 0HLR42UO95R3M:00000001, previousRequestId: no previous request id, message: EndpointRateLimiting is not enabled for /api/User/GetStuff
ApiGateway> info: Ocelot.Authentication.Middleware.AuthenticationMiddleware[0]
ApiGateway>       requestId: 0HLR42UO95R3M:00000001, previousRequestId: no previous request id, message: No authentication needed for /Auth/GetStuff
ApiGateway> info: Ocelot.Authorisation.Middleware.AuthorisationMiddleware[0]
ApiGateway>       requestId: 0HLR42UO95R3M:00000001, previousRequestId: no previous request id, message: /api/User/GetStuff route does not require user to be authorised
ApiGateway> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
ApiGateway>       Request finished in 7.2071ms 400 text/html; charset=us-ascii
JayChuah95 commented 4 years ago

@catcherwong May I know is there any issue on my configuration? Thanks for the help

catcherwong commented 4 years ago

@JayChuah95 Based on your sample, it seems that all things run well.

bb

JayChuah95 commented 4 years ago

@catcherwong Thanks for the clarification. It seem like there is some issue on my localhost networking routing. Could you help with this? As I check on my httperr.log, it doesn't route to my localhost > ::1%0 20000 image

JayChuah95 commented 4 years ago

Hi @catcherwong It turn up is a networking issue. The fe80::xxxx:xxxx:xxxx:xxxx%20 is my link-local ipv6 address.

It seem like the ipv6 address is not working. May I know what is the means by "record the full URL address from API Gateway to downstream services"?

catcherwong commented 4 years ago

API Gateway will send HTTP requests to the downstream services, in your sample is AuthService.

And what is the request URL here? http://localhost:30000/api/User/GetStuff or http://fe80::xxxx:xxxx:xxxx:xxxx%20:30000/api/User/GetStuff or others.

JayChuah95 commented 4 years ago

@catcherwong It suppose to be like the following example: Upstream > http://localhost:10000/Auth/GetStuff Then route to > http://localhost:20000/api/User/GetStuff

The output when I call http://localhost:10000/Auth/GetStuff image

catcherwong commented 4 years ago

Maybe you can change the default Log Level to Debug, ocelot will show more information for the request.

cc

{
  "spring": {
    "application": {
      "name": "Api-Gateway"
    }
  },
  "eureka": {
    "client": {
      "serviceUrl": "http://localhost:8761/eureka",
      "shouldRegisterWithEureka": true,
      "shouldFetchRegistry": true,
      "validateCertificates": false
    },
    "instance": {
      "port": 10000
    }
  },
 //  add the following section.
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "AppSettings": {
    "Secret": "0265776e-b490-4253-9be4-7d73457700b1"
  }
}
JayChuah95 commented 4 years ago

@catcherwong image The Downstream url is http://obest157:20000/api/User/GetStuff

obest157 is my computer name

catcherwong commented 4 years ago

When you access http://obest157:20000/api/User/GetStuff, can you get the right result or Bad Request?

JayChuah95 commented 4 years ago

@catcherwong image Bad Result on the url

catcherwong commented 4 years ago

You should resolve this problem at first, why you can not access this url normally?

JayChuah95 commented 4 years ago

@catcherwong I think this is a networking issue? It can be access if I changed it to localhost:20000 instead of the computer name

catcherwong commented 4 years ago

I guess this could be a issue for some network settings as well.

JayChuah95 commented 4 years ago

Temperary Solution go to .vs\config and change the applicationhost.config Modify from <binding protocol="http" bindingInformation="*:20000:localhost" /> to <binding protocol="http" bindingInformation="*:20000:*" />

raman-m commented 1 year ago

Hi @JayChuah95 ! Is it still the issue? 😄

raman-m commented 7 months ago

It seem like there is some issue on my localhost networking routing. https://github.com/ThreeMammals/Ocelot/issues/1053#issuecomment-553183891

Seems it was the discussion of 2 DevOps engineers :rofl:

raman-m commented 7 months ago

Not Ocelot issue!