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
Details worth knowing:
-
Only
http_proxyis read, even for HTTPS. The docs describe the proxy as coming fromENV['http_proxy'], and the source confirms it:Net::HTTPlooks up the proxy for anhttp://URI built from your target host, sohttps_proxyis never consulted. An HTTPS request is tunnelled through thehttp_proxyserver withCONNECT. -
Lowercase is preferred.
HTTP_PROXYis accepted as a fallback but prints a warning that it's discouraged. IfREQUEST_METHODis set (a CGI environment), uppercaseHTTP_PROXYisn't trusted andCGI_HTTP_PROXYis used instead. -
no_proxy/NO_PROXYis a comma-separated list. A bare entry such asexample.commatches the host and its subdomains; an entry with a leading dot matches subdomains only. You can add:portto an entry, and IP ranges like10.0.0.0/8are compared against the resolved address. -
Loopback always goes direct. If the target resolves to
127.xor::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
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")
-
A string without a scheme gets
http://added, soproxy: "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 anhttps://URL useshttps_proxy(orHTTPS_PROXY), andno_proxyis 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
# ~/.gemrc
:http_proxy: http://proxy.example.com:8080
-
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 readshttps_proxy/HTTPS_PROXY; forhttp://sources,http_proxy/HTTP_PROXY. -
Separate credentials: if the proxy URL has no user and password, RubyGems reads
HTTP_PROXY_USERandHTTP_PROXY_PASS(or theHTTPS_PROXY_versions for the https proxy). -
NO_PROXYentries 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
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 examplebundle 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
- Set both
http_proxyandhttps_proxy.Net::HTTPonly reads the first; RubyGems, Bundler and Faraday use the second for HTTPS. - In
no_proxy, list bare domains such asexample.com. All four tools treat them as the domain plus subdomains, while a leading dot means different things in different tools. - Pass
nilas the proxy address toNet::HTTP.new, or setFaraday.ignore_env_proxy = true, to go direct regardless of the environment. - Configure
gemand Bundler through~/.gemrcor env vars, and usebundle configonly 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
- Net::HTTP (Proxy Server section): https://docs.ruby-lang.org/en/master/Net/HTTP.html
- URI::Generic#find_proxy source: https://github.com/ruby/uri/blob/master/lib/uri/generic.rb
- Faraday connection and proxy options: https://lostisland.github.io/faraday/#/customization/connection-options and https://lostisland.github.io/faraday/#/customization/proxy-options
- RubyGems remote fetcher and request source: https://github.com/rubygems/rubygems/tree/master/lib/rubygems
- bundle-config man page: https://github.com/rubygems/rubygems/blob/v4.1.0/lib/bundler/man/bundle-config.1.ronn
Top comments (0)