Curl Proxy Server Example

-x routes a request through a proxy. The scheme in the proxy address decides how curl talks to it, and a surprising number of "curl is ignoring my settings" problems come from environment variables doing the same job silently.

Last updated . How this page is checked.

The command

curl -x http://proxy.example.com:8080 https://httpbin.org/ip

What the flags do

FlagWhat it does
-x [scheme://]host:portUse this proxy. Long form: --proxy. Default scheme is http.
-U user:passwordCredentials for the proxy itself. Long form: --proxy-user.
--noproxy listComma-separated hosts to reach directly. "*" disables proxies.
-x socks5h://host:portSOCKS5, with hostnames resolved by the proxy.

HTTP proxies and HTTPS targets

Through an HTTP proxy, a plain http:// request is handed to the proxy to perform. For an https:// target curl instead asks the proxy to open a tunnel with CONNECT and then does TLS end to end inside it, so the proxy sees the destination host and nothing else. Verification of the target's certificate works exactly as it does without a proxy.

Proxy credentials are separate from site credentials: -U authenticates you to the proxy, -u to the destination.

curl -x http://proxy.example.com:8080 -U alice:pw https://httpbin.org/ip
curl -x http://proxy.example.com:8080 -U alice:pw -u apiuser:apipw https://api.example.com/

SOCKS proxies

SOCKS works at the connection level and carries any protocol. The scheme matters: socks5:// resolves the hostname on your machine and sends the proxy an IP address, while socks5h:// sends the hostname and lets the proxy resolve it. Use the h form whenever local DNS should not see the lookup, or cannot answer it, as with an SSH tunnel into a private network.

ssh -D 1080 -N bastion.example.com &            # a local SOCKS proxy over SSH
curl -x socks5h://localhost:1080 https://internal.example.com/

Environment variables

Curl reads https_proxy, HTTPS_PROXY, all_proxy, ALL_PROXY and no_proxy. The one exception is http_proxy, which is honoured in lower case only, a deliberate guard against a CGI security problem. A proxy set in the environment applies to every curl command in that shell, which is the usual explanation when a request behaves differently on a server than on your laptop.

export https_proxy=http://proxy.example.com:8080
export no_proxy=localhost,127.0.0.1,.internal.example.com

curl https://httpbin.org/ip                    # via the proxy
curl --noproxy "*" https://httpbin.org/ip      # direct, ignoring the environment

Common mistakes

  • 407 Proxy Authentication Required comes from the proxy, not the site. It wants -U.
  • The default proxy port is 1080 when you leave it out, not 8080 or 3128.
  • Corporate proxies that inspect TLS present their own certificate. Trust the company CA with --cacert; do not reach for -k.

Related curl examples

More under Network behaviour

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I use a proxy with curl?

Pass -x followed by the proxy address, for example curl -x http://proxy.example.com:8080 URL. Add -U user:password if the proxy requires authentication.

How do I use a SOCKS5 proxy with curl?

Use -x socks5h://host:port to send hostnames to the proxy for resolution, or socks5://host:port to resolve them locally.

How do I make curl ignore the proxy environment variables?

Add --noproxy "*" to the command. Curl then connects directly regardless of http_proxy, https_proxy or all_proxy.