How does vapor proxy a URL

We know that nginx can reverse proxy. How can this be achieved in Vapor?

location /testapi/ {
  proxy_redirect off;
  proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Referer 'no-referrer-when-downgrade';
  proxy_pass https://www.google.com/;
}

vapor serve can proxy /testapi to https://www.google.com

2 Likes

I don’t think it’s achievable directly with Vapor, but using an HTTP client it’s possible. The general idea is to create an HTTPClient (from the AsyncHTTPRequest package), execute the request using a delegate and stream the response of your request in a Vapor Response object.

I have implemented exactly that and might open-source it one day when I find the time and if there’s an interest (the whole thing is relatively trivial tbh).

Not Vapor, but possibly helpful since it’s Server-Side Swift:

1 Like