Skip to content

Commit 70927f7

Browse files
authoredApr 19, 2024
Add FAQ about using a proxy (composer#11933)
1 parent b0ec0f9 commit 70927f7

File tree

3 files changed

+130
-35
lines changed

3 files changed

+130
-35
lines changed
 

‎doc/03-cli.md

+5-35
Original file line numberDiff line numberDiff line change
@@ -1232,20 +1232,12 @@ environment variable if you use Vagrant or VirtualBox and experience issues with
12321232
being found during installation even though they should be present.
12331233

12341234
### http_proxy or HTTP_PROXY
1235+
### HTTP_PROXY_REQUEST_FULLURI
1236+
### HTTPS_PROXY_REQUEST_FULLURI
1237+
### no_proxy or NO_PROXY
12351238

1236-
If you are using Composer from behind an HTTP proxy, you can use the standard
1237-
`http_proxy` or `HTTP_PROXY` env vars. Set it to the URL of your proxy.
1238-
Many operating systems already set this variable for you.
1239-
1240-
Using `http_proxy` (lowercased) or even defining both might be preferable since
1241-
some tools like git or curl will only use the lower-cased `http_proxy` version.
1242-
Alternatively you can also define the git proxy using
1243-
`git config --global http.proxy <proxy url>`.
1244-
1245-
If you are using Composer in a non-CLI context (i.e. integration into a CMS or
1246-
similar use case), and need to support proxies, please provide the `CGI_HTTP_PROXY`
1247-
environment variable instead. See [httpoxy.org](https://httpoxy.org/) for further
1248-
details.
1239+
See the [proxy documentation](faqs/how-to-use-composer-behind-a-proxy.md) for more details
1240+
on how to use proxy env vars.
12491241

12501242
### COMPOSER_AUDIT_ABANDONED
12511243

@@ -1264,32 +1256,10 @@ in performance gains.
12641256
Set to `4` or `6` to force IPv4 or IPv6 DNS resolution. This only works when the
12651257
curl extension is used for downloads.
12661258

1267-
### HTTP_PROXY_REQUEST_FULLURI
1268-
1269-
If you use a proxy, but it does not support the request_fulluri flag, then you
1270-
should set this env var to `false` or `0` to prevent Composer from setting the
1271-
request_fulluri option.
1272-
1273-
### HTTPS_PROXY_REQUEST_FULLURI
1274-
1275-
If you use a proxy, but it does not support the request_fulluri flag for HTTPS
1276-
requests, then you should set this env var to `false` or `0` to prevent Composer
1277-
from setting the request_fulluri option.
1278-
12791259
### COMPOSER_SELF_UPDATE_TARGET
12801260

12811261
If set, makes the self-update command write the new Composer phar file into that path instead of overwriting itself. Useful for updating Composer on a read-only filesystem.
12821262

1283-
### no_proxy or NO_PROXY
1284-
1285-
If you are behind a proxy and would like to disable it for certain domains, you
1286-
can use the `no_proxy` or `NO_PROXY` env var. Set it to a comma separated list of
1287-
domains the proxy should *not* be used for.
1288-
1289-
The env var accepts domains, IP addresses, and IP address blocks in CIDR
1290-
notation. You can restrict the filter to a particular port (e.g. `:80`). You
1291-
can also set it to `*` to ignore the proxy for all HTTP requests.
1292-
12931263
### COMPOSER_DISABLE_NETWORK
12941264

12951265
If set to `1`, disables network access (best effort). This can be used for debugging or
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# How to use Composer behind a proxy
2+
3+
Composer, like many other tools, uses environment variables to control the use of a proxy server and
4+
supports:
5+
6+
- `http_proxy` - the proxy to use for HTTP requests
7+
- `https_proxy` - the proxy to use for HTTPS requests
8+
- `CGI_HTTP_PROXY` - the proxy to use for HTTP requests in a non-CLI context
9+
- `no_proxy` - domains that do not require a proxy
10+
11+
These named variables are a convention, rather than an official standard, and their evolution and
12+
usage across different operating systems and tools is complex. Composer prefers the use of lowercase
13+
names, but accepts uppercase names where appropriate.
14+
15+
## Usage
16+
17+
Composer requires specific environment variables for HTTP and HTTPS requests. For example:
18+
19+
```
20+
http_proxy=http://proxy.com:80
21+
https_proxy=http://proxy.com:80
22+
```
23+
24+
Uppercase names can also be used.
25+
26+
### Non-CLI usage
27+
28+
Composer does not look for `http_proxy` or `HTTP_PROXY` in a non-CLI context. If you are running it
29+
this way (i.e. integration into a CMS or similar use case) you must use `CGI_HTTP_PROXY` for HTTP
30+
requests:
31+
32+
```
33+
CGI_HTTP_PROXY=http://proxy.com:80
34+
https_proxy=http://proxy.com:80
35+
36+
# cgi_http_proxy can also be used
37+
```
38+
39+
> **Note:** CGI_HTTP_PROXY was introduced by Perl in 2001 to prevent request header manipulation and
40+
was popularized in 2016 when this vulnerability was widely reported: https://httpoxy.org
41+
42+
## Syntax
43+
44+
Use `scheme://host:port` as in the examples above. Although a missing scheme defaults to http and a
45+
missing port defaults to 80/443 for http/https schemes, other tools might require these values.
46+
47+
The host can be specified as an IP address using dotted quad notation for IPv4, or enclosed in
48+
square brackets for IPv6.
49+
50+
### Authorization
51+
52+
Composer supports Basic authorization, using the `scheme://user:pass@host:port` syntax. Reserved url
53+
characters in either the user name or password must be percent-encoded. For example:
54+
55+
```
56+
user: me@company
57+
pass: p@ssw$rd
58+
proxy: http://proxy.com:80
59+
60+
# percent-encoded authorization
61+
me%40company:p%40ssw%24rd
62+
63+
scheme://me%40company:p%40ssw%24rd@proxy.com:80
64+
```
65+
66+
> **Note:** The user name and password components must be percent-encoded individually and then
67+
combined with the colon separator. The user name cannot contain a colon (even if percent-encoded),
68+
because the proxy will split the components on the first colon it finds.
69+
70+
## HTTPS proxy servers
71+
72+
Composer supports HTTPS proxy servers, where HTTPS is the scheme used to connect to the proxy, but
73+
only from PHP 7.3 with curl version 7.52.0 and above.
74+
75+
```
76+
http_proxy=https://proxy.com:443
77+
https_proxy=https://proxy.com:443
78+
```
79+
80+
## Bypassing the proxy for specific domains
81+
82+
Use the `no_proxy` (or `NO_PROXY`) environment variable to set a comma-separated list of domains
83+
that the proxy should **not** be used for.
84+
85+
```
86+
no_proxy=example.com
87+
# Bypasses the proxy for example.com and its sub-domains
88+
89+
no_proxy=www.example.com
90+
# Bypasses the proxy for www.example.com and its sub-domains, but not for example.com
91+
```
92+
93+
A domain can be restricted to a particular port (e.g. `:80`) and can also be specified as an IP
94+
address or an IP address block in CIDR notation.
95+
96+
IPv6 addresses do not need to be enclosed in square brackets, like they are for
97+
http_proxy/https_proxy values, although this format is accepted.
98+
99+
Setting the value to `*` will bypass the proxy for all requests.
100+
101+
> **Note:** A leading dot in the domain name has no significance and is removed prior to processing.
102+
103+
## Deprecated environment variables
104+
105+
Composer originally provided `HTTP_PROXY_REQUEST_FULLURI` and `HTTPS_PROXY_REQUEST_FULLURI` to help
106+
mitigate issues with misbehaving proxies. These are no longer required or used.
107+
108+
## Requirement changes
109+
110+
Composer <2.8 used `http_proxy` for both HTTP and HTTPS requests if `https_proxy` was not set,
111+
but as of Composer 2.8.0 it requires [scheme-specific](#usage) environment variables.
112+
113+
The reason for this change is to align Composer with current practice across other popular tools. To help
114+
with the transition, as of Composer 2.7.3 the original behaviour remains but a warning message is
115+
shown instructing the user to add an `https_proxy` environment variable.
116+
117+
To prevent the original behaviour during the transition period, set an empty environment variable
118+
(`https_proxy=`).

‎src/Composer/Console/Application.php

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Composer\Command;
3333
use Composer\Composer;
3434
use Composer\Factory;
35+
use Composer\Downloader\TransportException;
3536
use Composer\IO\IOInterface;
3637
use Composer\IO\ConsoleIO;
3738
use Composer\Json\JsonValidationException;
@@ -404,6 +405,7 @@ function_exists('php_uname') ? php_uname('s') . ' / ' . php_uname('r') : 'Unknow
404405
$io->writeError('<warning>Composer now requires separate proxy environment variables for HTTP and HTTPS requests.</warning>');
405406
$io->writeError('<warning>Please set `https_proxy` in addition to your existing proxy environment variables.</warning>');
406407
$io->writeError('<warning>This fallback (and warning) will be removed in Composer 2.8.0.</warning>');
408+
$io->writeError('<warning>https://getcomposer.org/doc/faqs/how-to-use-composer-behind-a-proxy.md</warning>');
407409
}
408410

409411
return $result;
@@ -480,6 +482,11 @@ private function hintCommonErrors(\Throwable $exception, OutputInterface $output
480482
}
481483
Silencer::restore();
482484

485+
if ($exception instanceof TransportException && str_contains($exception->getMessage(), 'Unable to use a proxy')) {
486+
$io->writeError('<error>The following exception indicates your proxy is misconfigured</error>', true, IOInterface::QUIET);
487+
$io->writeError('<error>Check https://getcomposer.org/doc/faqs/how-to-use-composer-behind-a-proxy.md for details</error>', true, IOInterface::QUIET);
488+
}
489+
483490
if (Platform::isWindows() && false !== strpos($exception->getMessage(), 'The system cannot find the path specified')) {
484491
$io->writeError('<error>The following exception may be caused by a stale entry in your cmd.exe AutoRun</error>', true, IOInterface::QUIET);
485492
$io->writeError('<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#-the-system-cannot-find-the-path-specified-windows- for details</error>', true, IOInterface::QUIET);

0 commit comments

Comments
 (0)
Please sign in to comment.