How to set proxy in Http request in Laravel 7?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Set a Proxy for HTTP Requests in Laravel 7: Bridging DEV and Production Environments
As developers, we often encounter situations where code works perfectly in a local development environment but fails when deployed to production. A very common culprit is network configuration, especially when dealing with corporate firewalls or restrictive networks that mandate the use of an HTTP/S proxy for outbound requests.
You've encountered this challenge: your Laravel HTTP requests work fine on your local machine (DEV), but fail in production because a proxy is required. The good news is that Laravel’s built-in `Http` facade, which relies on Guzzle under the hood, provides mechanisms to handle these configurations.
This post will guide you through the most effective ways to configure proxy settings for HTTP requests in your Laravel application, ensuring seamless operation across all environments.
## Understanding Why Proxies are Necessary
A proxy server acts as an intermediary for network requests. In many corporate or secured environments (like those behind a VPN or specific firewalls), direct outbound connections are blocked. By routing traffic through a proxy, the request is authenticated and routed correctly through the necessary gateway. For production deployments, setting this up correctly is crucial for successful API communication.
## Method 1: Configuring Proxies via Environment Variables (The Laravel Way)
The most robust and recommended approach in a modern Laravel application is to leverage environment variables. This keeps your configuration separate from your core business logic, making your application portable and secure.
While the `Http` facade doesn't expose a direct method like `withProxy('http://...')`, it relies on underlying HTTP client settings. The best practice here is to configure the environment variables that the underlying Guzzle client reads automatically.
You should define these variables in your `.env` file:
```dotenv
# .env file
# HTTP Proxy Settings
HTTP_PROXY=http://your-proxy-server:port
HTTPS_PROXY=http://your-proxy-server:port
NO_PROXY=localhost,127.0.0.1,.internaldomain.com
```
**Explanation:**
* `HTTP_PROXY`: Used for standard HTTP requests.
* `HTTPS_PROXY`: Essential for securing API calls over HTTPS.
* `NO_PROXY`: Specifies hosts that should bypass the proxy (e.g., local services).
When Laravel initializes its HTTP client, it often picks up these environment variables automatically. This method keeps your code clean and adheres to the principle of separation of concerns, aligning with best practices for building scalable applications, much like those promoted by resources on [laravelcompany.com](https://laravelcompany.com).
## Method 2: Explicitly Setting Proxy in Code (For Specific Requests)
If you need fine-grained control—for instance, if only *some* requests require a proxy, or if the environment variables are not sufficient for a specific Guzzle configuration—you can pass custom options directly to the `withClient` method when bootstrapping your HTTP client.
While this is more verbose, it gives you explicit control over the underlying connection settings. Since Laravel 7 uses Guzzle, we can inject these proxy settings into the request flow.
Here is an example demonstrating how you might configure a custom client instance with proxy settings:
```php
use Illuminate\Support\Facades\Http;
// Define your proxy details
$proxy = 'http://user:password@proxy.example.com:8080';
// Configure the HTTP client to use the proxy for all subsequent requests
$client = Http::withProxy($proxy);
// Now make your request using this configured client instance
$response = $client->withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
])
->withToken('xxxxxxxxxxxxxx')
->post('https://xxxxxxxxx.com/v0.1/messages/', [
'from' => [
'type' => 'xxxx',
'number' => 'xxxxxxxx',
],
'to' => [
'type' => 'xxxxx',
'number' => 'xxxxxx',
],
'message' => [
'content' => [
'type' => 'text',
'text' => 'test message from laravel'
]
]
]);
// $response now holds the result of the proxied request.
```
**Note:** In a typical application setup, configuring the proxy globally via environment variables (Method 1) is preferred over setting it per-request unless you have a very specific, isolated requirement.
## Conclusion
To successfully deploy HTTP requests in Laravel across different environments, understanding how network configuration works at the underlying PHP/Guzzle level is key. For most scenarios, relying on **Environment Variables** (`HTTP_PROXY`, `HTTPS_PROXY`) provides the cleanest, most maintainable, and most scalable solution for handling proxies in production. If you require dynamic or granular control over proxy settings, explicitly configuring the HTTP client instance via methods like `withProxy()` offers the necessary power. By mastering these configuration techniques, you ensure your Laravel application remains robust and functional no matter where it is deployed.