- How to set proxy in PHP-FPM
- Condition
- STEP 1. Set environment globally
- STEP 2. Set php-fpm environment
- STEP 3. Set php-fpm environment
- STEP 4. Test run
- Top comments (0)
- Read next
- Tiny CRM — Linode + DEV Hackathon Submission
- How to install php-sqlite3 for php 8.1 on Ubuntu 21.10
- Implementing Feature Flags
- A practical example of using Symfony PropertyInfo component
- Saved searches
- Use saved searches to filter your results more quickly
- License
- zounar/php-proxy
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Using proxy servers with PHP’s build-in HTTP functions
- Proxy Authentication
- Tools:
How to set proxy in PHP-FPM
One of our clients server lives inside of tight security VPC. Therefore, all of our EC2 web server must use HTTP_PROXY, HTTPS_PROXY. When I try to use Concrete CMS (formally concrete5) to fetch the latest language files from remote server, I’ve got the curl timeout error and couldn’t update the language. So I need to set the proxy for PHP-FPM. If you use Apache’s PHP module or CLI, PHP will use server’s environment settings. However, for php-fpm you must also configure in php.d/www.conf. Checked & tested with Amazon Linux 2 with PHP7.4 installed via amazon-linux-extra.
Condition
STEP 1. Set environment globally
Although php-fpm doesn’t reference to server’s global setting, you may still want to use PHP-CLI to run via SSH. So let’s set it.
$ sudo vi /etc/environment export http_proxy="http://10.0.0.1:8080" export https_proxy="http://10.0.0.1:8080" export HTTP_PROXY="http://10.0.0.1:8080" export HTTPS_PROXY="http://10.0.0.1:8080" export no_proxy="127.0.0.1,localhost" export NO_PROXY=$no_proxy
STEP 2. Set php-fpm environment
$ sudo vi /etc/php-fpm.d/www.conf # Add the following two lines anywhere env[HTTP_PROXY] = 10.0.0.1:8080 env[HTTPS_PROXY] = 10.0.0.1:8080
STEP 3. Set php-fpm environment
sudo service php-fpm reload
STEP 4. Test run
I’ve prepared the sample PHP script.
Change $proxy variable to your proxy.
And save it to your webroot. Get my sample PHP script here.
https://en.katzueno.com/2021/10/08/how-to-set-proxy-in-php-fpm/
Top comments (0)
Read next
Tiny CRM — Linode + DEV Hackathon Submission
How to install php-sqlite3 for php 8.1 on Ubuntu 21.10
Implementing Feature Flags
A practical example of using Symfony PropertyInfo component
Katz currently works as the Chief Communications Officer at Macareux Digital, Inc. to spread the word of Concrete CMS, a free open source web publishing application.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
License
zounar/php-proxy
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Add static properties to control curlopt options
Git stats
Files
Failed to load latest commit information.
README.md
This proxy script allows you to forward all HTTP/HTTPS requests to another server. Works for all common request types including GET, POST requests with files, PATCH and PUT requests. It has minimal set of requirements (PHP >=5.6, libcurl, gzip) which are available even on the smallest free hostings and has its own simple authorization and cookie support.
- Copy the Proxy.php script to publicly-accessible folder of a PHP web server (the script is standalone and has no PHP dependencies)
- Make a cURL request targeting this script
- Add Proxy-Auth header with auth key found here
- Add Proxy-Target-URL header with URL to be requested by the proxy
- (Optional) Add Proxy-Debug header for debug mode
In order to protect using proxy by unauthorized users, consider changing Proxy-Auth token in proxy source file and in all your requests.
This might be useful when you want to redirect requests coming into your app.
- Run composer require zounar/php-proxy
- Add Proxy::run(); line to where you want to execute it (usually into a controller action)
- In this example, the script is in AppController — actionProxy :
use Zounar\PHPProxy\Proxy; class AppController extends Controller < public function actionProxy() < Proxy::$AUTH_KEY = ''; // Do your custom logic before running proxy $responseCode = Proxy::run(); // Do your custom logic after running proxy // You can utilize HTTP response code returned from the run() method > >
In order to protect using proxy by unauthorized users, consider changing Proxy-Auth token by calling Proxy::$AUTH_KEY = »; before Proxy::run() . Then change the token in all your requests.
Following example shows how to execute GET request to https://www.github.com. Proxy script is at http://www.foo.bar/Proxy.php. All proxy settings are kept default, the response is automatically echoed.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); curl_exec($request);
In order to show some debug info from the proxy, add Proxy-Debug: 1 header into the request. This will show debug info in plain-text containing request headers, response headers and response body.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com', 'Proxy-Debug: 1' )); curl_exec($request);
Some sites may return different content for different user agents. In such case add User-Agent header to cURL request, it will be automatically passed to the request for target site. In this case it’s Firefox 70 for Ubuntu.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0', 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); curl_exec($request);
Error 301 Moved permanently
It might occur that there’s a redirection when calling the proxy (not the target site), eg. during http -> https redirection. You can either modify/fix the proxy URL (which is recommended), or add CURLOPT_FOLLOWLOCATION option before curl_exec .
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); curl_exec($request);
Save response into variable
The default cURL behavior is to echo the response of curl_exec . In order to save response into variable, all you have to do is to add CURLOPT_RETURNTRANSFER cURL option.
$request = curl_init('http://www.foo.bar/Proxy.php'); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); curl_setopt($request, CURLOPT_HTTPHEADER, array( 'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2', 'Proxy-Target-URL: https://www.github.com' )); $response = curl_exec($request);
About
Using proxy servers with PHP’s build-in HTTP functions
How to perform HTTP requests trough proxy servers in PHP.
Sometimes, for whatever reason, you may need to hide your own IP address. A common way to hid your IP is to use a proxy server to process your HTTP requests. Setting up your browser to use a proxy server is easy enough, but setting up scripts and programs to use proxies can require a bit more effort. This tutorial shows you how to make your PHP scripts use a proxy when performing HTTP requests with functions such as fread and file_get_contents.
If you remember the example from the last tutorial, adding a proxy can be done as easily as demonstrated in the below example:
$sURL = "http://beamtic.com/Examples/ip.php"; // The Request URL $aHTTP['http']['proxy'] = 'tcp://127.0.0.1:8118'; // The proxy ip and port number $aHTTP['http']['request_fulluri'] = true; // use the full URI in the Request. I.e. http://beamtic.com/Examples/ip.php $aHTTP['http']['method'] = 'GET'; $aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n"; $aHTTP['http']['header'] .= "Referer: http://beamtic.com/\r\n"; $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo 'Real IP:' . $_SERVER['REMOTE_ADDR'] . '
'; echo 'Proxy IP:'. $contents;As in the prior Tutorials, you can also use the URL provided in this example for testing purposes. ip.php is a simple php script outputting the IP of the client. If this is different than your own IP, the proxy works.
Proxy Authentication
If the proxy that you are using requires authentication, you will simply add the proxy authorization http header, in a manner similar to what is shown in the last php tutorial. Logging in to a proxy is done by combining the username and password, only separated by a single colon «:», then encoding it using base64, and send it in the Proxy-Authorization http header like shown in below example:
$sLogin = base64_encode('username:password'); $sURL = "http://beamtic.com/Examples/ip.php"; // The Request URL $aHTTP['http']['proxy'] = 'tcp://127.0.0.1:8118'; // The proxy ip and port number $aHTTP['http']['request_fulluri'] = true; // Use the full URI in the Request. I.e. http://beamtic.com/Examples/ip.php $aHTTP['http']['method'] = 'GET'; $aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n"; $aHTTP['http']['header'] .= "Referer: http://beamtic.com/\r\n"; $aHTTP['http']['header'] .= "Proxy-Authorization: Basic $sLogin"; $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo 'Real IP:' . $_SERVER['REMOTE_ADDR'] . '
'; echo 'Proxy IP:'. $contents;Tools:
You can use the following API endpoints for testing purposes:
https://beamtic.com/api/user-agent
https://beamtic.com/api/request-headers