On one server I have some 30 PHP sites running under Apache. All those sites use the same (HTTP) API to fetch some data. The API is hosted elsewhere (under my control)
The API uses Nginx with keep-alive and the PHP sites use CURL for making the API requests.
A visitor to 1 of the 30 sites would generate an API call, and the connection to the API would be closed by apache/PHP as soon as the HTML is delivered to the visitor.
What I'm looking for is something like a local proxy to the API that is able to maintain the connection to it so the PHP sites can profit from the keepalive.
Anyway of accomplishing this?
Best Answer
Nginx configured as a reverse proxy can do this easily :
Now you can point your scripts to the local server instead of the remote one, here's a demo with
curl
:As you can see, even the Host header is passed as-is.
Or you can make the transition seamless by making the remote hostname point to your local machine, either in
/etc/hosts
or in your DNS resolver's configuration. In this case, make sure to use only IP addresses instead of hostnames in the pool definition in Nginx's config, otherwise the proxy will also loop back to itself and that would cause a bit of a disaster.Once the hosts file has been changed accordingly, the proxying is seamless :
As you can see, our local server behaves just like the remote one and any program attempting to access the remote hostname will actually connect to our local server.
Note that this may need additional configuration for HTTPS-based hosts.