Node.js SSL Proxy Examples

Complete guide with production-ready example for configuring SSL proxy in Node.js using axios and https-proxy-agent

axios https-proxy-agent 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

1. Setup

Create index.js:

const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');

const proxyHost = process.env.PROXY_HOST;
const proxyPort = process.env.PROXY_PORT;
const proxyUser = process.env.PROXY_USER;
const proxyPass = process.env.PROXY_PASS;

if (!proxyHost || !proxyPort || !proxyUser || !proxyPass) {
  console.error('PROXY_HOST, PROXY_PORT, PROXY_USER, and PROXY_PASS environment variables must be set.');
  process.exit(1);
}

// ENCODE credentials to handle special characters (!, @, :) in passwords
const encodedUser = encodeURIComponent(proxyUser);
const encodedPass = encodeURIComponent(proxyPass);
const fullProxyUrl = `${proxyHost}:${proxyPort}`;

// Initialize Agent with encoded credentials and SSL bypass (if using internal CA)
const proxyAgent = new HttpsProxyAgent(
  `https://${encodedUser}:${encodedPass}@${fullProxyUrl}`,
  { rejectUnauthorized: false } 
);

const targetUrl = 'https://outboundgateway.com/ip/';

const instance = axios.create({
  httpsAgent: proxyAgent,
});

(async () => {
  try {
    console.log(`Attempting to connect to ${targetUrl} through proxy ${fullProxyUrl}...`);
    
    const response = await instance.get(targetUrl, {
      headers: {
        'User-Agent': 'My-NodeJS-App/1.0'
      }
    });

    console.log('Success! Response from outboundgateway.com:');
    console.log(JSON.stringify(response.data, null, 2));
  } catch (error) {
    console.error('An error occurred:', error.message);
    if (error.response) {
      console.error('Response data:', error.response.data);
    }
  }
})();

Create package.json:

{
  "name": "node-proxy-example",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "axios": "^1.7.2",
    "https-proxy-agent": "^7.0.5"
  }
}

2. Running Locally

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

# 1. Install dependencies
npm install

# 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 here

node index.js

3. Running with Docker

Create Dockerfile:

FROM node:20-alpine

WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

CMD ["node", "index.js"]

Build and Run:

# 1. Build the image
docker build -t node-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!' \
  node-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. Use Connection Pooling

Reuse connections for better performance, especially for high-frequency API calls.

3. Handle Timeouts Properly

Set appropriate timeouts to prevent hanging connections, especially with proxy servers.

4. Monitor Performance

Track request times and switch to faster proxies if performance degrades.

5. Use Environment Variables

Store proxy credentials in environment variables, not in code.

Common Issues and Solutions

ECONNRESET or ETIMEDOUT

Solution: Check proxy server connectivity and increase timeout values.

SSL Certificate Errors

Solution: Use system CA certificates or configure custom CA for self-signed certificates.

Proxy Authentication Failures

Solution: Verify proxy credentials and check authentication method requirements.

DNS Resolution Issues

Solution: Ensure proxy server can resolve target domain names or use IP addresses directly.

Ready to Use SSL Proxy in Node.js?

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

Python implementation with requests, urllib3, and aiohttp libraries.

View Python Guide →

Java SSL Proxy

Enterprise-grade proxy setup with Apache HttpClient and OkHttp.

View Java Guide →

PHP SSL Proxy

Web-focused proxy configuration with cURL, Guzzle, and Symfony HTTP Client.

View PHP Guide →

cURL SSL Proxy

Command-line and shell script examples for automation and testing.

View cURL Guide →

🚀 Performance Comparison: Node.js excels at concurrent connections and async operations, making it ideal for high-throughput applications. For CPU-intensive tasks, consider Java, or for quick scripting, Python might be more suitable.