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:
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:
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:
1 | $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:
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:
1 | 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.
PHP Reverse Proxy
Comments