Skip to content

Route a request to CDN backend #2342

Answered by MihaZupan
milen-denev asked this question in Q&A
Discussion options

You must be logged in to vote

Using direct forwarding that Chris mentioned is likely the easiest option to achieve what you're trying to do: https://microsoft.github.io/reverse-proxy/articles/direct-forwarding.html

For example, you can have a middleware before YARP that looks something like this (untested):

var httpClient = new HttpMessageInvoker(new SocketsHttpHandler()
{
    // ...
});

app.Use(async (context, next) =>
{
    var id = GetHash(context.Request.Path);

    if (IsCached(id))
    {
        context.Request.Path = $"cache-container/{id}";
        var forwarder = context.RequestServices.GetRequiredService<IHttpForwarder>();
        await forwarder.SendAsync(context, "https://cdn.other-domain.net/", httpClient);

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@milen-denev
Comment options

@MihaZupan
Comment options

Answer selected by milen-denev
@MihaZupan
Comment options

@milen-denev
Comment options

@Tratcher
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants