DEV Community

Leo Lin
Leo Lin

Posted on

How to Configure a Proxy for Ruby: Net::HTTP, Faraday, gem and Bundler (http_proxy, HTTPS_PROXY, NO_PROXY and bundle config)

By Leo Lin, Product Manager at FlashIP (https://www.flaship.com/), Singapore. Disclosure: I work on FlashIP.

Ruby has no single proxy setting. Net::HTTP, Faraday, gem and Bundler each read the proxy a little differently, and the biggest surprise is that plain Net::HTTP ignores https_proxy. Below is what the official docs and the library source say for current releases (net-http, Faraday 2.x, RubyGems and Bundler 4.1). Hostnames and credentials are placeholders.

1. Net::HTTP: http_proxy only

Net::HTTP.new and Net::HTTP.start take the proxy as optional arguments after host and port. The default for p_addr is :ENV, which means "read the environment", so Net::HTTP.get(uri) picks up a proxy without you asking.

export http_proxy=http://user:pass@proxy.example.com:8080
export no_proxy=localhost,internal.example.com,10.0.0.0/8
ruby app.rb
Enter fullscreen mode Exit fullscreen mode

Details worth knowing:

  • Only http_proxy is read, even for HTTPS. The docs describe the proxy as coming from ENV['http_proxy'], and the source confirms it: Net::HTTP looks up the proxy for an http:// URI built from your target host, so https_proxy is never consulted. An HTTPS request is tunnelled through the http_proxy server with CONNECT.
  • Lowercase is preferred. HTTP_PROXY is accepted as a fallback but prints a warning that it's discouraged. If REQUEST_METHOD is set (a CGI environment), uppercase HTTP_PROXY isn't trusted and CGI_HTTP_PROXY is used instead.
  • no_proxy/NO_PROXY is a comma-separated list. A bare entry such as example.com matches the host and its subdomains; an entry with a leading dot matches subdomains only. You can add :port to an entry, and IP ranges like 10.0.0.0/8 are compared against the resolved address.
  • Loopback always goes direct. If the target resolves to 127.x or ::1, the environment proxy is skipped.
  • Credentials go in the URL and are percent-decoded, so encode @ or : in a password.

To set the proxy in code, or turn it off:

require "net/http"

http = Net::HTTP.new("api.example.com", 443,
                     "proxy.example.com", 8080, "user", "pass")
http.use_ssl = true
res = http.get("/")

direct = Net::HTTP.new("api.example.com", 443, nil) # nil = no proxy, ignore ENV
Enter fullscreen mode Exit fullscreen mode

Net::HTTP.new also takes a p_no_proxy string after the password; Net::HTTP.start doesn't support that filter.

2. Faraday: the proxy option

Faraday takes a proxy: option on the connection, either as a URL string or as a hash with uri, user and password:

require "faraday"

conn = Faraday.new(
  url: "https://api.example.com",
  proxy: { uri: "http://proxy.example.com:8080", user: "user", password: "pass" }
)
conn.get("/status")
Enter fullscreen mode Exit fullscreen mode
  • A string without a scheme gets http:// added, so proxy: "proxy.example.com:8080" works.
  • Per request, you can override it with req.options.proxy.update(uri: "...") inside the block.
  • Without an explicit proxy, Faraday reads the environment for the request URL. Unlike raw Net::HTTP, that means an https:// URL uses https_proxy (or HTTPS_PROXY), and no_proxy is applied.
  • To ignore the environment everywhere, set Faraday.ignore_env_proxy = true.

3. gem: --http-proxy, .gemrc and env vars

RubyGems has its own flag and config key:

gem install rails --http-proxy http://proxy.example.com:8080
Enter fullscreen mode Exit fullscreen mode
# ~/.gemrc
:http_proxy: http://proxy.example.com:8080
Enter fullscreen mode Exit fullscreen mode
  • Precedence: --http-proxy (or -p) and the :http_proxy: key override the proxy environment variables.
  • Environment variables are per scheme. For an https:// source such as rubygems.org, RubyGems reads https_proxy/HTTPS_PROXY; for http:// sources, http_proxy/HTTP_PROXY.
  • Separate credentials: if the proxy URL has no user and password, RubyGems reads HTTP_PROXY_USER and HTTP_PROXY_PASS (or the HTTPS_PROXY_ versions for the https proxy).
  • NO_PROXY entries match the domain and its subdomains; a leading dot is stripped.

4. Bundler: no proxy key in bundle config

There's no proxy setting in bundle config. According to the bundle-config man page (Bundler 4.1), Bundler takes the proxy from the :http_proxy key in ~/.gemrc and falls back to the environment. For an https source, https_proxy wins over http_proxy, the *_PROXY_USER/*_PROXY_PASS variables work the same as for gem, and NO_PROXY=* bypasses the proxy for every host. The same page says :http_proxy: :no_proxy in ~/.gemrc turns proxies off entirely, environment variables included.

export HTTPS_PROXY=http://user:pass@proxy.example.com:8080
export NO_PROXY=gems.internal.example.com
bundle install
Enter fullscreen mode Exit fullscreen mode

What bundle config does help with behind a proxy:

  • ssl_ca_cert (BUNDLE_SSL_CA_CERT): a PEM file or folder of CA certificates, for proxies that re-sign TLS. bundle config set --global ssl_ca_cert /etc/ssl/corp-ca.pem
  • mirror.SOURCE_URL: point rubygems.org at an internal mirror, for example bundle config set --global mirror.https://rubygems.org https://gems.internal.example.com.
  • timeout (BUNDLE_TIMEOUT): seconds per network request, default 10. Raise it if a slow proxy times out.

Quick checklist

  1. Set both http_proxy and https_proxy. Net::HTTP only reads the first; RubyGems, Bundler and Faraday use the second for HTTPS.
  2. In no_proxy, list bare domains such as example.com. All four tools treat them as the domain plus subdomains, while a leading dot means different things in different tools.
  3. Pass nil as the proxy address to Net::HTTP.new, or set Faraday.ignore_env_proxy = true, to go direct regardless of the environment.
  4. Configure gem and Bundler through ~/.gemrc or env vars, and use bundle config only for CA certificates, mirrors and timeouts.

If you need an external proxy to test against, FlashIP (https://www.flaship.com/) offers residential, mobile 4G/5G, static ISP and datacenter proxies over HTTP(S) and SOCKS5. The settings above work the same for any HTTP proxy endpoint.

Sources

Top comments (0)