reverseproxy.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. // The url we're proxying to.
  3. $remoteUrl = 'http://example.org/';
  4. // The url we're proxying from. Please note that this must be a relative url,
  5. // and basically acts as the base url.
  6. //
  7. // If your $remoteUrl doesn't end with a slash, this one probably shouldn't
  8. // either.
  9. $myBaseUrl = '/reverseproxy.php';
  10. // $myBaseUrl = '/~evert/sabre/http/examples/reverseproxy.php/';
  11. use Sabre\HTTP\Client;
  12. use Sabre\HTTP\Sapi;
  13. // Find the autoloader
  14. $paths = [
  15. __DIR__.'/../vendor/autoload.php',
  16. __DIR__.'/../../../autoload.php',
  17. __DIR__.'/vendor/autoload.php',
  18. ];
  19. foreach ($paths as $path) {
  20. if (file_exists($path)) {
  21. include $path;
  22. break;
  23. }
  24. }
  25. $request = Sapi::getRequest();
  26. $request->setBaseUrl($myBaseUrl);
  27. $subRequest = clone $request;
  28. // Removing the Host header.
  29. $subRequest->removeHeader('Host');
  30. // Rewriting the url.
  31. $subRequest->setUrl($remoteUrl.$request->getPath());
  32. $client = new Client();
  33. // Sends the HTTP request to the server
  34. $response = $client->send($subRequest);
  35. // Sends the response back to the client that connected to the proxy.
  36. Sapi::sendResponse($response);