In Net::HTTP, the proxy is defined by the third to sixth arguments of Net::HTTP.new: proxy address, port, username and password. If you leave out the third argument, its default value is :ENV and Net::HTTP reads the proxy from an environment variable. Here is the trap: even when the target is HTTPS, Net::HTTP never looks at https_proxy. This post covers setup with arguments, the environment behaviour, the HTTPS tunnel, password encoding and timeouts.
Defining the proxy with arguments
Net::HTTP takes the proxy details when the object is created. The Ruby documentation gives the signature like this:
Net::HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil,
p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil)The first two arguments are the target site. The four arguments from p_addr to p_pass define the proxy. A complete request looks like this:
require "net/http"
uri = URI("https://api.ipify.org/?format=json")
http = Net::HTTP.new(uri.host, uri.port,
"pr.proxynet.io", 8000, "user", "pass")
http.use_ssl = true
res = http.request(Net::HTTP::Get.new(uri))
puts res.code
puts res.body # the proxy's exit IPIf you pass only an address and no port, Net::HTTP uses 80 as the proxy port. So always write the port explicitly. Net::HTTP.start also accepts the proxy arguments, for opening the same connection with a block:
Net::HTTP.start(uri.host, uri.port,
"pr.proxynet.io", 8000, "user", "pass",
use_ssl: true) do |http|
puts http.get(uri.request_uri).body
endstart does not take the p_no_proxy and p_use_ssl arguments. Those two go only to new. The eighth argument, p_use_ssl, is for speaking TLS to the proxy itself. On a proxy endpoint that speaks plain HTTP, such as pr.proxynet.io:8000, leave it empty.
Environment variables and :ENV
When p_addr is not given, Net::HTTP takes the proxy from the http_proxy variable. Uppercase HTTP_PROXY is read too, but Ruby prints a warning for it. The source code always looks up the environment proxy with an http scheme address. As a result, https_proxy is never read, even when the target is HTTPS.
| Call | Behaviour |
|---|---|
Net::HTTP.new(host, port) | Uses http_proxy if it is set |
Net::HTTP.new(host, port, nil) | No proxy, environment not read |
Net::HTTP.new(host, port, "pr.proxynet.io", 8000, "user", "pass") | The given proxy, environment not read |
Net::HTTP.start(host, port) | Uses http_proxy if it is set |
Net::HTTP.start(host, port, nil) | No proxy |
The environment path has two more exceptions. Hosts on the no_proxy list go direct. A target whose name resolves to an IPv4 address starting with 127. or to ::1 also goes direct. That is why a localhost target skips the proxy when you test locally.
If you set only https_proxy, the request does not fail. It quietly leaves from your own IP. We collected how other tools read these variables in why the HTTP_PROXY variable is not working. One line is enough for a quick check:
ruby -rnet/http -e 'h = Net::HTTP.new("example.com", 443); p h.proxy?, h.proxy_address'If you see false and nil, Net::HTTP found no proxy in the environment.
HTTPS targets and CONNECT
On a connection with use_ssl = true, Net::HTTP first sends the proxy a CONNECT request. The request carries the line CONNECT api.ipify.org:443 HTTP/1.1 and a Host header. If there is a username, a Proxy-Authorization: Basic header is added to this request as well. Once the proxy returns 200, the TLS handshake runs inside the tunnel, directly with the target site.
The certificate is checked against the target site's name, not the proxy's. The proxy cannot see inside the encrypted traffic. We walk through the tunnel step by step in HTTP CONNECT and the HTTPS proxy tunnel.
When the target is plain HTTP, no tunnel is opened. Net::HTTP sends the request to the proxy with the full URL and adds the Proxy-Authorization header to every request. The difference shows up on the error side too:
| Target | What a rejected login looks like |
|---|---|
HTTPS (use_ssl = true) | Net::HTTPClientException is raised, message 407 "Proxy Authentication Required" |
| HTTP | No exception, a Net::HTTPProxyAuthenticationRequired response object is returned |
On an HTTP target a 407 can stay silent. Code that does not check res.code treats this response as a success.
Special characters in the password
p_user and p_pass passed as arguments are used as they are. Write the password pass@123 into the argument unchanged. No encoding is needed.
In a URL-style address the rule flips. @, #, : and / are separators in a URL. Written unencoded, they make the parser split the address in the wrong place. In the http_proxy value, write the password as pass%40123. Net::HTTP decodes the username and password from the environment itself.
Be careful if you parse the address yourself. URI#user and URI#password return the encoded form. Pass that value as an argument and the proxy receives pass%40123, so you get a 407. On Ruby 3.1 and later, use decoded_user and decoded_password:
require "net/http"
# PROXY_URL=http://user:pass%40123@pr.proxynet.io:8000
proxy = URI(ENV.fetch("PROXY_URL"))
target = URI("https://api.ipify.org/?format=json")
http = Net::HTTP.new(target.host, target.port,
proxy.host, proxy.port,
proxy.decoded_user, proxy.decoded_password)
http.use_ssl = true
puts http.request(Net::HTTP::Get.new(target)).bodyTo encode the password before putting it into a URL, write URI.encode_uri_component("pass@123"). The result is pass%40123. Naming the variable PROXY_URL instead of http_proxy stops Net::HTTP from reading the value on its own. The proxy then applies only where you pass it.
Timeouts
Three timeouts in Net::HTTP directly affect a proxy connection. All three default to 60 seconds.
| Setting | What it waits for | When exceeded |
|---|---|---|
open_timeout | TCP connection and TLS handshake with the proxy | Net::OpenTimeout |
read_timeout | Each read, including the CONNECT response | Net::ReadTimeout |
write_timeout | Each write | Net::WriteTimeout |
With a proxy, open_timeout measures the proxy, not the target site. The proxy does the work of reaching the target during CONNECT. Meanwhile Net::HTTP waits for the response under read_timeout. That is why a slow target can show up as Net::ReadTimeout even during the connection phase.
http.open_timeout = 10
http.read_timeout = 30These two values are a sound starting point for most jobs. On a rotating exit, each new connection may leave from a different IP. A short open_timeout drops a stuck connection early and gives a retry a chance.
SOCKS5 and the old Proxy class
Net::HTTP does not speak SOCKS5. Its source code has no SOCKS support. Net::HTTP also ignores the scheme of the proxy address. Even if you write http_proxy=socks5://..., it takes the address and port and speaks HTTP to that port. The request then fails.
Ruby's socket library has a SOCKSSocket class. It works only if Ruby was compiled with a SOCKS library, and Net::HTTP does not use it. When working with Net::HTTP, use your account's HTTP endpoint. Proxynet also offers SOCKS5 Proxy; if you need SOCKS5, pick a separate Ruby client that supports it.
In older code you will come across the Net::HTTP::Proxy class method:
Net::HTTP::Proxy("pr.proxynet.io", 8000, "user", "pass").start(host, 443, use_ssl: true) do |http|
# ...
endThis method still works and returns a subclass with the proxy preset. The comment in the source code marks it as obsolete and recommends passing the same arguments to Net::HTTP.new. If p_addr is nil, plain Net::HTTP is returned. In new code, use new or start.
Faraday and HTTParty
Faraday and HTTParty are open-source Ruby HTTP libraries. Each names the proxy parameter differently and reads environment variables in its own way.
| Library | Proxy parameter | Environment variable |
|---|---|---|
| Net::HTTP | p_addr, p_port, p_user, p_pass | Only http_proxy |
| Faraday | proxy: (URL or uri, user, password) | http_proxy or https_proxy, by target scheme |
| HTTParty | http_proxyaddr, http_proxyport, http_proxyuser, http_proxypass | Leaves it to Net::HTTP if no proxy is given |
In Faraday you write Faraday.new(url: "https://api.ipify.org", proxy: "http://user:pass@pr.proxynet.io:8000"). To ignore the environment completely, set Faraday.ignore_env_proxy = true. In HTTParty, the class-level line http_proxy "pr.proxynet.io", 8000, "user", "pass" fills the same four options.
On the same machine, Faraday can work through https_proxy while plain Net::HTTP goes out without a proxy. If you use both libraries together, pass the proxy explicitly as arguments, not through the environment.
Troubleshooting
The first step is to see whether the connection really goes to the proxy. The line http.set_debug_output($stderr) makes Net::HTTP print the first connection it opens as opening connection to pr.proxynet.io:8000.... If you see the target site's name there, the proxy is not in use.
This output also prints the CONNECT request, including its Proxy-Authorization header. The Base64 in that header is not encryption and decodes easily. Do not write debug output to persistent logs.
| Symptom | Likely cause |
|---|---|
| Request works but the IP does not change | Only https_proxy is set, or p_addr was never given |
407 "Proxy Authentication Required" | Wrong credentials, or an encoded password sent via uri.password |
Failed to open TCP connection to pr.proxynet.io:8000 | Proxy address, port or network access |
Net::OpenTimeout | No TCP connection to the proxy |
Net::ReadTimeout right after connecting | The target responds slowly, read_timeout is short |
| Connection drops without a response | The address is a SOCKS5 port and Net::HTTP sends it HTTP |
We cover the other causes of a 407, IP whitelisting included, in the post on the HTTP 407 error.
The bottom line
In Net::HTTP, pass the proxy as arguments, write the port explicitly and add use_ssl = true for HTTPS. Do not leave the timeouts at the 60-second default. Above all, check this: Net::HTTP reads only http_proxy, so a proxy set through https_proxy is silently skipped. Once the code is ready, you can try the same setup directly with Residential Proxy.
Sources
- class Net::HTTP, Proxy Server section: docs.ruby-lang.org
- URI::Generic#find_proxy: docs.ruby-lang.org
- net-http source code, lib/net/http.rb: github.com
- RFC 9110, 9.3.6 CONNECT: rfc-editor.org
Frequently asked questions
Does Net::HTTP read the https_proxy variable?
No. When Net::HTTP looks for a proxy in the environment, it always uses an address with the http scheme. So even for an HTTPS target only http_proxy is read, and uppercase HTTP_PROXY is read with a warning. If only https_proxy is set, the request goes direct without an error. Passing the proxy as arguments removes this ambiguity.
How do I turn off the proxy in Net::HTTP?
Pass nil as p_addr: Net::HTTP.new(host, port, nil). This call also stops it from reading environment variables. The same rule applies to Net::HTTP.start, where you write start(host, port, nil). To exclude specific hosts, use the no_proxy variable or the p_no_proxy argument of new.
Does Net::HTTP support SOCKS5 proxies?
No. Net::HTTP's source code has no SOCKS support, and it ignores the scheme of the proxy address. Even when given a socks5:// address, it speaks HTTP to that port. Pick the HTTP endpoint when using a proxy with Net::HTTP. If SOCKS5 is a must, you need a separate Ruby client or library that supports SOCKS5.
Can I still use Net::HTTP::Proxy?
Yes, it works. It returns a Net::HTTP subclass with the proxy preset, which is why old code does not break. However, the comment in the source code marks the method as obsolete and recommends passing the same arguments directly to Net::HTTP.new. In code you write today, use new or start.
What should I do if the proxy password contains @?
If you pass the password as an argument, nothing: write pass@123 as it is. If the password sits inside a URL, encode @ as %40: pass%40123. If you parse that URL yourself, use decoded_password instead of password. Otherwise the proxy receives the encoded password and returns a 407.
What is the default timeout in Net::HTTP?
60 seconds. open_timeout, read_timeout and write_timeout all default to 60. With a proxy, open_timeout measures the connection to the proxy. The time to reach the target counts toward read_timeout while the CONNECT response is awaited. For most jobs, 10 seconds for open_timeout and 30 for read_timeout is a good start.




