Nokogiri Proxy Configuration
Nokogiri parses HTML and XML; it does not perform HTTP by itself. The official example uses Net::HTTP through a proxy, then parses the body—see proxy-examples/ruby/nokogiri-proxy.rb. Apply RubyProxyHeaders::NetHTTP.patch! to the same Net::HTTP setup when you need custom CONNECT headers.
Installation
gem install nokogiri
Add gem install ruby-proxy-headers for HTTPS CONNECT header support on Net::HTTP.
Basic Proxy Configuration
require 'nokogiri'
require 'net/http'
require 'openssl'
require 'uri'
uri = URI('https://api.ipify.org?format=json')
proxy = URI('http://username:password@proxyhost:31280')
http = Net::HTTP.new(
uri.host, uri.port,
proxy.host, proxy.port,
proxy.user, proxy.password
)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
response = http.request(Net::HTTP::Get.new(uri))
doc = Nokogiri::HTML(response.body)
puts doc.text
Custom Proxy Headers
require 'nokogiri'
require 'uri'
require 'openssl'
require 'ruby_proxy_headers/net_http'
RubyProxyHeaders::NetHTTP.patch!
uri = URI('https://example.com/')
proxy = URI('http://username:password@proxyhost:31280')
http = Net::HTTP.new(
uri.host, uri.port,
proxy.host, proxy.port,
proxy.user, proxy.password
)
http.use_ssl = true
http.proxy_connect_request_headers = { 'X-ProxyMesh-Country' => 'US' }
response = http.request(Net::HTTP::Get.new(uri))
doc = Nokogiri::HTML(response.body)
puts http.last_proxy_connect_response_headers['X-ProxyMesh-IP']
ProxyMesh Headers Reference
Send these headers to control proxy behavior:
X-ProxyMesh-Country- Route through a specific country (e.g., "US"). Only works with world proxy or open proxyX-ProxyMesh-IP- Request a specific outgoing IP addressX-ProxyMesh-Not-IP- Exclude specific IPs from rotation
The proxy returns X-ProxyMesh-IP with the IP address used for the request (when your stack exposes CONNECT response headers).
Resources
- Nokogiri Documentation
- Nokogiri on RubyGems
- Nokogiri on RubyDoc
- ruby-proxy-headers (Nokogiri)
- ruby-proxy-headers on RubyGems
- ruby-proxy-headers on RubyDoc
- ProxyMesh Headers Reference
- Example Code on GitHub
Related Ruby Proxy Guides
Explore proxy configuration for other Ruby HTTP libraries: