October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Go HTTP Proxy Setup: Route net/http Client Requests

Go’s default net/http transport reads proxy settings from the environment. For fixed or dynamic routing, configure Transport.Proxy and reuse the client and transport.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For most Go programs, setting HTTP_PROXY and HTTPS_PROXY is enough: the default net/http transport reads proxy settings from the environment. Use a custom http.Transport with its Proxy field when your application needs fixed or per-request routing. The http.Client handles requests; its transport decides how connections are routed.

Choose environment settings or an explicit transport

Approach Best for What to check
Default environment-aware transport Proxy configuration managed by the deployment environment Confirm the process receives the intended variables and that NO_PROXY covers destinations that should connect directly. The default transport uses ProxyFromEnvironment as documented by Go’s net/http package and transport source.
Custom Transport.Proxy Fixed or application-specific proxy routing Choose the proxy URL for each request. A newly constructed transport does not automatically inherit the default transport’s proxy function; set Proxy explicitly if you want proxy routing.

With a custom transport, a nil Proxy field—or a proxy function that returns a nil URL—means the request uses no proxy. If your application wants environment-based routing on a custom transport, set Proxy: http.ProxyFromEnvironment.

As an Amazon Associate I earn from qualifying purchases.

Configure a proxy through environment variables

For an HTTP destination, Go consults HTTP_PROXY; for an HTTPS destination, it consults HTTPS_PROXY. NO_PROXY specifies destinations that should bypass proxying. A proxy value can be a URL or a host[:port] value; when the scheme is omitted, Go assumes http. Invalid values can cause proxy selection to return an error.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
export HTTP_PROXY="http://proxy.example:8080"
export HTTPS_PROXY="http://proxy.example:8080"
export NO_PROXY="localhost,.internal.example"

These example values show syntax only; they are not a live proxy endpoint. Supply the actual values through your deployment’s configuration, and avoid putting production credentials in shell commands that may be saved in shell history.

Check what NO_PROXY matches

Go’s vendored httpproxy implementation documents comma-separated domain names, IP prefixes, CIDR values, optional ports, the wildcard *, and special treatment for localhost and loopback addresses. The precise matching behavior is an implementation detail rather than a promise covered by Go’s compatibility guarantee; check the source for the Go version you deploy if a bypass rule is important.

Set a fixed proxy on an http.Client

For application-controlled configuration, parse the proxy URL and assign http.ProxyURL to a reusable transport:

package proxyclient

import (
    "net/http"
    "net/url"
)

func newClient() (*http.Client, error) {
    proxyURL, err := url.Parse("http://proxy.example:8080")
    if err != nil {
        return nil, err
    }

    transport := &http.Transport{
        Proxy: http.ProxyURL(proxyURL),
    }
    return &http.Client{Transport: transport}, nil
}

Replace the example URL with the proxy address for your application. Create the client and transport once and reuse them rather than constructing a new transport for every request.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Select a proxy dynamically

For request-specific routing, assign a function to Transport.Proxy. It receives the outgoing request and returns the proxy URL to use, or nil when that request should connect directly. Keep the selection policy in that function explicit—for example, base it on the request host—and return any parsing or policy errors rather than silently routing to an unintended destination.

Understand HTTPS proxying and proxy credentials

When an HTTPS destination uses an HTTP proxy, Go’s transport establishes a tunnel with the proxy using CONNECT, then carries the HTTPS connection through it. An ordinary call such as client.Get("https://example.com") does not require you to construct a CONNECT request yourself. The Transport documentation cautions against treating Transport as a general-purpose mechanism for issuing CONNECT requests.

The documented proxy URL schemes include http, https, socks5, and socks5h. The documentation says socks5 is treated like socks5h. If a proxy URL contains user information, Go sends those credentials in a Proxy-Authorization header. Handle proxy credentials as secrets: do not publish them in source code or logs, and restrict access to the configuration that supplies them.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reuse clients and manage connections

Go’s net/http documentation states: “Clients and Transports are safe for concurrent use by multiple goroutines and for efficiency should only be created once and re-used.” A transport also caches connections for later use, so reusing it can avoid repeatedly establishing connections.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If an application contacts many hosts or needs to control idle connections, the transport documentation describes CloseIdleConnections, MaxIdleConnsPerHost, and DisableKeepAlives as relevant controls. Choose them based on the application’s connection-management needs rather than creating a fresh transport per request.

Troubleshoot a proxy that appears to be ignored

  • Check the destination scheme. HTTP requests use HTTP_PROXY; HTTPS requests use HTTPS_PROXY.
  • Check the process environment. The variables must be present in the environment of the running Go process, not only in an interactive shell.
  • Check bypass rules. A matching NO_PROXY entry can make the destination connect directly.
  • Check custom transports. If you supplied &http.Transport{}, its Proxy field is not automatically copied from the default transport. Set Proxy: http.ProxyFromEnvironment or provide a fixed or dynamic proxy function.
  • Check URL form and errors. Use a valid proxy URL or host[:port]; malformed settings can fail proxy selection.
  • Check CGI deployments. The vendored proxy-selection implementation infers CGI from the presence of REQUEST_METHOD. When HTTP_PROXY would apply in that case, it returns an error because request-derived environment data could be malicious. This specific safeguard does not mean Go rejects every proxy variable in every runtime. See the implementation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.