PHP Reverse Proxy

0, Preface

For some resources on the website, direct calls are likely to cause cross-domain problems, and you also need to consider the anti-leech links set by the other party, so you can use a reverse proxy to replace the access domain name with your own.

However, some websites have set certain rules and cannot be reversed directly with NGINX, so the phpReverseProxy project of jshensh is used.

1. About this project

Supported features:

  • Forward the user's GET request
  • Forward the user's POST request
  • Forward user's PUT request
  • Forward user's DELETE request
  • Forward all headers sent by the user (except Accept-Encoding and Host)
  • Forward all headers returned by the server (except Content-Length and Content-Encoding)
  • Replace what the server returns

2. Build

Pull the item under Site Directory:

Bash
1
git clone https://github.com/jshensh/phpReverseProxy

If accessing GitHub is also difficult, refer to the article on this site:

Build a GitHub mirror station
https://blog.tsinbei.com/archives/152/

Installation dependencies:

Bash
1
composer require jshensh/php-curl-class

3. Configuration

Replace the origin site access protocol on the sixth line of index.php, the origin site domain name on the seventh line, and the current site domain name on the eighth line.

The output replacement array starting from line 154 is modified according to the actual situation.

E.g:

PHP
1
2
3
$originProtocol = 'https';
$originSite = 'a.com';
$thisSite = 'b.com';

You can reverse proxy https://a.com, and the access protocol of b.com can be customized in the pagoda, not set here.

If you need to support default file, such as index.html, you can add a line:

PHP
1
$_SERVER['REQUEST_URI']=str_replace('index.html','',$_SERVER['REQUEST_URI']);

That is, when returning to the source, remove index.html from the request URI. On some websites where the default file is not index.html, but can be accessed through the root directory, this method works.

The program by default replaces the target website with its own website, such as reverse proxy a.com, where a.com will be modified to b.com and then returned. If you need to add other substitutions, you can modify it as follows:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
try {
     new ReverseProxy([
         'replace' => [
             [
                 $originSite,
                 '[email protected]'
             ], [
                 $thisSite,
                 '[email protected]'
             ]
         ],
         'originProtocol' => $originProtocol,
         'originSite' => $originSite,
         'thisSite' => $thisSite
     ]);
} catch (\Exception $e) {
     echo '<h1>Proxy Error</h1><p>' . $e->getMessage() . '</p>';
}

In the example, [email protected] will be replaced by [email protected].

4, Pseudo-static

Pseudo-static rules:

Comment first then view it after your comment is approved. Join QQ Group to display all hidden texts.

Author
Hsukqi Lee
Posted on

2022-12-13

Edited on

2022-12-13

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet