Ruby SSL Proxy Examples

Complete guide with production-ready examples for configuring SSL proxy in Ruby using curb, HTTParty, and rest-client gems

Ruby curb HTTParty rest-client HTTPS Proxy

What is an SSL Proxy?

An SSL proxy is a proxy server that handles encrypted HTTPS connections. When you make HTTP requests through an SSL proxy, the proxy terminates the SSL connection, processes the request, and establishes a new SSL connection to the target server.

Use Case: SSL proxies are commonly used for security compliance, IP whitelisting, and maintaining consistent IP addresses for API access.

Production SSL Proxy Setup with curb

1. Setup

Create app.rb:

require 'curb'

proxy_host = ENV['PROXY_HOST']
proxy_port = ENV['PROXY_PORT']
proxy_user = ENV['PROXY_USER']
proxy_pass = ENV['PROXY_PASS']

if proxy_host.nil? || proxy_port.nil? || proxy_user.nil? || proxy_pass.nil?
  puts "PROXY_HOST, PROXY_PORT, PROXY_USER, and PROXY_PASS environment variables must be set."
  exit
end

begin
  curl = Curl::Easy.new("https://outboundgateway.com/ip/")

  # --- KEY CHANGE: Use https:// to connect to the proxy ---
  curl.proxy_url = "https://#{proxy_host}:#{proxy_port}"
  curl.proxy_tunnel = true
  curl.proxypwd = "#{proxy_user}:#{proxy_pass}"

  # Keep verbose on to see the TLS handshake with the proxy
  #curl.verbose = true

  puts "Attempting to connect to https://outboundgateway.com/ip/ through HTTPS proxy #{proxy_host}:#{proxy_port}..."

  curl.perform
  puts "\n--- Success! ---"
  puts "Status: #{curl.response_code}"
  puts "Body:\n#{curl.body_str}"
rescue => e
  warn "\n--- Request failed ---"
  warn "Error: #{e.message}"
  raise
end

2. Running Locally

Note: Use single quotes for passwords with special characters (!, @, :) to prevent shell interpretation.

# 1. Install the curb gem
gem install curb

# 2. Set variables and run (Linux/macOS)
export PROXY_HOST="eu-01.outboundgateway.com"
export PROXY_PORT="8443"
export PROXY_USER="your_username"
export PROXY_PASS='your_complex_password!' # Use single quotes

ruby app.rb

3. Running with Docker

Create Dockerfile:

FROM ruby:3.2-slim

WORKDIR /app
COPY app.rb .

# Install build dependencies for curb gem
RUN apt-get update && apt-get install -y build-essential libcurl4-openssl-dev && \
    gem install curb && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

CMD ["ruby", "app.rb"]

Build and Run:

# 1. Build the image
docker build -t ruby-curb-proxy-app .

# 2. Run the container (Linux/macOS)
docker run --rm \
  -e PROXY_HOST="eu-01.outboundgateway.com" \
  -e PROXY_PORT="8443" \
  -e PROXY_USER="your_username" \
  -e PROXY_PASS='your_complex_password!' \
  ruby-curb-proxy-app

Note: Replace your_username and your_complex_password! with your actual credentials.

Production SSL Proxy Setup with HTTParty

HTTParty provides a simpler interface by automatically detecting the https_proxy environment variable. This approach requires no explicit proxy configuration in your code.

1. Setup

Create app.rb:

require 'httparty'
require 'uri'
require 'json'

proxy_host = ENV['PROXY_HOST']
proxy_port = ENV['PROXY_PORT']
proxy_user = ENV['PROXY_USER']
proxy_pass = ENV['PROXY_PASS']

if proxy_host.nil? || proxy_port.nil? || proxy_user.nil? || proxy_pass.nil?
  puts "PROXY_HOST, PROXY_PORT, PROXY_USER, and PROXY_PASS environment variables must be set."
  exit
end

# Construct the standard HTTPS proxy URL
# This format tells HTTParty to use a secure connection to the proxy itself
proxy_url = "https://#{proxy_user}:#{proxy_pass}@#{proxy_host}:#{proxy_port}"

# Set the environment variable. HTTParty will automatically pick this up.
ENV['https_proxy'] = proxy_url

puts "Attempting to connect to https://outboundgateway.com/ip/ using the 'https_proxy' environment variable..."

begin
  # No proxy options needed here; HTTParty uses ENV['https_proxy'] automatically
  response = HTTParty.get('https://outboundgateway.com/ip/')

  puts "\n--- Success! ---"
  puts "Status: #{response.code}"
  puts "Body:\n#{JSON.pretty_generate(response.parsed_response)}"
rescue => e
  warn "\n--- Request failed ---"
  warn "Error: #{e.message}"
  raise
end

Note: HTTParty automatically detects and uses the ENV['https_proxy'] variable, so you don't need to pass any proxy options to the HTTParty.get method.

2. Running Locally

Note: Use single quotes for passwords with special characters (!, @, :) to prevent shell interpretation.

# 1. Install the HTTParty gem
gem install httparty

# 2. Set variables and run (Linux/macOS)
export PROXY_HOST="eu-01.outboundgateway.com"
export PROXY_PORT="8443"
export PROXY_USER="your_username"
export PROXY_PASS='your_complex_password!' # Use single quotes

ruby app.rb

3. Running with Docker

Create Dockerfile:

FROM ruby:3.2-slim

WORKDIR /app

# Copy the script into the container
COPY app.rb .

# Install the httparty gem
RUN gem install httparty

# Command to run the script
CMD ["ruby", "app.rb"]

Build and Run:

# 1. Build the image
docker build -t ruby-httparty-proxy-app .

# 2. Run the container (Linux/macOS)
docker run --rm \
  -e PROXY_HOST="eu-01.outboundgateway.com" \
  -e PROXY_PORT="8443" \
  -e PROXY_USER="your_username" \
  -e PROXY_PASS='your_complex_password!' \
  ruby-httparty-proxy-app

Note: Replace your_username and your_complex_password! with your actual credentials.

Production SSL Proxy Setup with rest-client

rest-client is a simple HTTP client library for Ruby that also automatically detects the https_proxy environment variable, providing a clean and straightforward interface for making HTTP requests through proxies.

1. Setup

Create app.rb:

require 'rest-client'
require 'json'

proxy_host = ENV['PROXY_HOST']
proxy_port = ENV['PROXY_PORT']
proxy_user = ENV['PROXY_USER']
proxy_pass = ENV['PROXY_PASS']

if proxy_host.nil? || proxy_port.nil? || proxy_user.nil? || proxy_pass.nil?
  puts "PROXY_HOST, PROXY_PORT, PROXY_USER, and PROXY_PASS environment variables must be set."
  exit
end

# Construct the standard HTTPS proxy URL
# This format tells rest-client to use a secure connection to the proxy itself
proxy_url = "https://#{proxy_user}:#{proxy_pass}@#{proxy_host}:#{proxy_port}"

# Set the environment variable. rest-client will automatically pick this up.
ENV['https_proxy'] = proxy_url

puts "Attempting to connect to https://outboundgateway.com/ip/ using the 'https_proxy' environment variable with rest-client..."

begin
  # No proxy options needed here; rest-client uses ENV['https_proxy'] automatically
  response = RestClient.get('https://outboundgateway.com/ip/')

  puts "\n--- Success! ---"
  puts "Status: #{response.code}"
  puts "Body:\n#{JSON.pretty_generate(JSON.parse(response.body))}"
rescue => e
  warn "\n--- Request failed ---"
  warn "Error: #{e.message}"
  raise
end

Note: rest-client automatically detects and uses the ENV['https_proxy'] variable, so you don't need to pass any proxy options to the RestClient.get method.

2. Running Locally

Note: Use single quotes for passwords with special characters (!, @, :) to prevent shell interpretation.

# 1. Install the rest-client gem
gem install rest-client

# 2. Set variables and run (Linux/macOS)
export PROXY_HOST="eu-01.outboundgateway.com"
export PROXY_PORT="8443"
export PROXY_USER="your_username"
export PROXY_PASS='your_complex_password!' # Use single quotes

ruby app.rb

3. Running with Docker

Create Dockerfile:

FROM ruby:3.2-slim

WORKDIR /app

# Copy the script into the container
COPY app.rb .

# Install the rest-client gem
RUN gem install rest-client

# Command to run the script
CMD ["ruby", "app.rb"]

Build and Run:

# 1. Build the image
docker build -t ruby-rest-client-proxy-app .

# 2. Run the container (Linux/macOS)
docker run --rm \
  -e PROXY_HOST="eu-01.outboundgateway.com" \
  -e PROXY_PORT="8443" \
  -e PROXY_USER="your_username" \
  -e PROXY_PASS='your_complex_password!' \
  ruby-rest-client-proxy-app

Note: Replace your_username and your_complex_password! with your actual credentials.

SSL Proxy Best Practices

1. Always Use HTTPS

Never send credentials or sensitive data over unencrypted connections when using proxies.

2. Verify SSL Certificates

Always verify SSL certificates in production. Disable verification only for development environments.

3. Use Connection Timeouts

Set appropriate connection and read timeouts to prevent hanging connections.

4. Handle Errors Gracefully

Implement proper error handling for proxy authentication failures, SSL errors, and connection timeouts.

5. Use Environment Variables

Store proxy credentials in environment variables, not in code.

Common Issues and Solutions

SSL Certificate Verification Failed

Solution: Update CA certificates or use proper certificate bundle. For development only, disable verification.

407 Proxy Authentication Required

Solution: Verify proxy credentials and ensure proper authentication format (username:password).

Connection Timeout

Solution: Increase timeout values or check network connectivity to proxy server.

Gem Installation Issues

Solution: Ensure build-essential and libcurl4-openssl-dev are installed before installing curb gem.

Ready to Use SSL Proxy in Ruby?

Get your static SSL proxy IPs today and start making secure HTTPS connections with consistent IP addresses for compliance and security requirements.

Get Started with OutboundGateway

Related SSL Proxy Guides

Python SSL Proxy

Complete Python examples using requests library with environment variables.

View Python Guide →

Node.js SSL Proxy

JavaScript/Node.js implementation with axios and https-proxy-agent.

View Node.js Guide →

Java SSL Proxy

Enterprise-grade proxy setup with Apache HttpClient and OkHttp.

View Java Guide →

PHP SSL Proxy

PHP cURL implementation with environment variables and Docker support.

View PHP Guide →

💎 Ruby Advantage: Ruby offers elegant syntax and multiple HTTP client options. The curb gem provides direct libcurl bindings for advanced control, while HTTParty offers a simpler, more intuitive interface perfect for rapid development and API integration.