Search code examples
.netreverse-proxyblazor-server-sidems-yarp

Is there any method to bind localhost + port to custom hostname


I just tried to set up a reverse proxy in blazor web server application with YARP. When I built it, it runs on localhost:5278. I just want to change adress with something else like www.mybeautifultestsite.com. Editing the hosts file does not enable me to bind to http:://www.mybeautifultestsite.com on the 5278 port. I still need to use http:://www.mybeautifultestsite.com:5278 to reach it. Is there any method for this? Here my appsettings.json file. Thanks in advance.

"ReverseProxy": {
 "Routes": {
  "api-route": {
    "ClusterId": "api-cluster",
    "Match": {
      "Host": "www.mybeautifultestsite.com",
      "Path": "{**catch-all}"
    }
  }
},
"Clusters": {
  "api-cluster": {
    "Destinations": {
      "api-destination": {
        "Address": "http://127.0.0.1:5278"
      }
    }
  }
}

} }


Solution

  • By default Kestrel will listen on a random port between 5000-5300 (7000-7300 for HTTPS).

    In addition to adding the hostname to the hosts file you need to instruct Kestrel to listen on port 80. Something like this:

    {
      // Kestrel
      "AllowedHosts": "*",
      "Kestrel": {
        "Endpoints": {
          "Http": {
            "Url": "http://[::]:80",
            "Protocols": "Http1AndHttp2"
          },
        }
      },
    
      // YARP
      "ReverseProxy": {
        "Routes": {
          "api-route": {
            "ClusterId": "api-cluster",
              "Match": {
                "Host": "www.mybeautifultestsite.com",
                "Path": "{**catch-all}"
              }
            }
          }
        },
        "Clusters": {
          "api-cluster": {
            "Destinations": {
              "api-destination": {
                "Address": "http://127.0.0.1:5278"
              }
            }
          }
        }
      }
    }
    

    Disclaimer: Not tested