← Back to Blog

Proxies in Axios on Node.js: HttpsProxyAgent, TLS Errors, and IP Address Rotation

We discuss configuring a proxy in axios using HttpsProxyAgent, common TLS errors (CERT_HAS_EXPIRED, ECONNRESET), and a working implementation of proxy rotation in Node.js.

📅November 17, 2026

If you are scraping Wildberries, Ozon, or collecting data through axios on Node.js, sooner or later you will encounter IP blocks and strange TLS errors when connecting through a proxy. In this article, we will cover the setup of HttpsProxyAgent, typical causes of SSL connection failures, and a working implementation of proxy rotation with retry logic.

Why axios Needs a Proxy

By default, axios accesses the internet using your server's IP address. If you make 50 requests per minute to a marketplace API or scrape pages from Wildberries, the server will quickly notice abnormal activity and ban the IP due to rate-limiting or WAF rules (Cloudflare, DataDome, PerimeterX). Proxies solve three tasks: they distribute the load among different IPs, bypass geo-blocks (for example, regional prices on Ozon or AliExpress), and allow you to emulate requests from a specific country or city.

An important nuance: axios is built on top of the native http/https module in Node.js, and it does not have built-in support for proxies for HTTPS requests via the proxy parameter in the config — this parameter does not work correctly for HTTPS targets through HTTP proxies, which becomes the source of most problems. The solution is to explicitly pass the HTTP(S) agent.

Installing and Basic Setup of HttpsProxyAgent

To work with HTTP/HTTPS proxies in Node.js, the package https-proxy-agent is used. It creates a CONNECT tunnel through the proxy server, which is necessary for the correct operation of HTTPS requests.

npm install axios https-proxy-agent

A basic example of a request through a proxy:

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

const proxyUrl = 'http://45.12.34.56:8000';
const agent = new HttpsProxyAgent(proxyUrl);

async function fetchData() {
  try {
    const response = await axios.get('https://api.example.com/data', {
      httpAgent: agent,
      httpsAgent: agent,
      proxy: false // make sure to disable axios's built-in proxy handling
    });
    console.log(response.data);
  } catch (err) {
    console.error('Request error:', err.message);
  }
}

fetchData();

Note the proxy: false — without this line, axios may attempt to apply its internal proxy logic on top of the agent, leading to double tunneling and errors like ECONNRESET.

Username/Password Authentication in Proxy

Most paid proxy providers require authentication via username and password. The credentials are passed directly in the proxy URL in the format http://user:pass@host:port. It is important to escape special characters in the password using encodeURIComponent, otherwise, the proxy URL will be constructed incorrectly, and the request will simply not reach the server.

const username = encodeURIComponent('user_login');
const password = encodeURIComponent('P@ssw0rd!123');
const host = '45.12.34.56';
const port = 8000;

const proxyUrl = `http://${username}:${password}@${host}:${port}`;
const agent = new HttpsProxyAgent(proxyUrl);

const response = await axios.get('https://httpbin.org/ip', {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false
});

console.log('Current IP:', response.data.origin);

For resident proxies with IP binding (whitelist), username/password authentication is not needed — the server itself determines that the request came from an allowed address. This is convenient for CI/CD pipelines where the server's IP is static and known in advance.

SOCKS5 Proxy in axios via SocksProxyAgent

If the provider issues SOCKS5 endpoints (often typical for mobile proxies with rotation by IMEI), a different package is needed — socks-proxy-agent. The connection logic is similar; only the agent class changes.

npm install socks-proxy-agent
const { SocksProxyAgent } = require('socks-proxy-agent');

const socksUrl = 'socks5://user:[email protected]:1080';
const agent = new SocksProxyAgent(socksUrl);

const response = await axios.get('https://api.example.com/data', {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false
});

SOCKS5 operates at the TCP level and does not modify HTTP headers, so it often provides a more stable connection when working with sites that analyze behavior at the transport level.

TLS Errors: Causes and Solutions

Working through a proxy often breaks the standard TLS handshake because an intermediate CONNECT tunnel appears between the client and the target server. Let's discuss the most common errors.

Error Cause Solution
UNABLE_TO_VERIFY_LEAF_SIGNATURE The proxy intercepts TLS and replaces the certificate (MITM) Change the provider to a transparent tunnel without MITM
CERT_HAS_EXPIRED The system time of the server is out of sync, or the proxy certificate has expired Synchronize NTP, update CA certificates
ECONNRESET The proxy terminated the connection, double proxying Add proxy: false, check keep-alive
ETIMEDOUT The proxy is overloaded or blocked by the target server IP rotation, increase timeout
DEPTH_ZERO_SELF_SIGNED_CERT The proxy uses a self-signed certificate for debugging Temporarily set rejectUnauthorized: false (for testing only!)

Disabling certificate verification in production is a bad idea, but for local debugging, it is sometimes the only way to understand whether the problem lies in the code or the proxy itself:

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

const agent = new HttpsProxyAgent('http://user:pass@host:port');
agent.options.rejectUnauthorized = false; // ONLY for debugging, not for production!

const response = await axios.get('https://api.example.com/data', {
  httpsAgent: agent,
  proxy: false
});

If the error consistently occurs with one specific proxy provider, while requests go through without issues (without proxy), the problem lies with the proxy server, not your code. Most often, this means that the proxy is performing a MITM interception of HTTPS traffic, which is critical, for example, when collecting data via an API with request signature verification.

Proxy Rotation and IP Pool

For large-scale scraping of Wildberries, Ozon, or competitor price monitoring, a single static IP will not suffice — it will be banned after the first 100-200 requests. Rotation is needed: either server-side (the provider changes the outgoing IP for each request), or client-side, where you manually cycle through a list of proxies.

Here is an example of a simple pool with cyclic rotation at the code level:

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

const proxyList = [
  'http://user:[email protected]:8000',
  'http://user:[email protected]:8000',
  'http://user:[email protected]:8000'
];

let currentIndex = 0;

function getNextAgent() {
  const proxyUrl = proxyList[currentIndex];
  currentIndex = (currentIndex + 1) % proxyList.length;
  return new HttpsProxyAgent(proxyUrl);
}

async function fetchWithRotation(url) {
  const agent = getNextAgent();
  return axios.get(url, {
    httpAgent: agent,
    httpsAgent: agent,
    proxy: false,
    timeout: 10000
  });
}

// Example of bulk requests
async function run() {
  const urls = ['https://api.example.com/item/1', 'https://api.example.com/item/2'];
  for (const url of urls) {
    try {
      const res = await fetchWithRotation(url);
      console.log(url, res.status);
    } catch (err) {
      console.error(url, err.message);
    }
  }
}

run();

This approach is good for small scripts, but for industrial volumes (thousands of requests per hour), it is easier to use a provider with automatic rotation at the backbone level — you simply send requests to one endpoint, while the IP changes on the provider's side on a timer or for each request. Residential proxies are well-suited for this — they have a wide pool of real IPs, which reduces the likelihood of being banned due to behavioral patterns.

Retry Logic and Timeouts

Proxies occasionally drop — this is normal when working with rotating pools. To prevent the script from crashing on the first error, add retry with exponential backoff and switch proxies on retry attempts. The axios-retry package simplifies this task.

npm install axios-retry
const axios = require('axios');
const axiosRetry = require('axios-retry').default;
const { HttpsProxyAgent } = require('https-proxy-agent');

const client = axios.create({
  timeout: 15000
});

axiosRetry(client, {
  retries: 3,
  retryDelay: (retryCount) => retryCount * 1000,
  retryCondition: (error) => {
    return (
      error.code === 'ECONNRESET' ||
      error.code === 'ETIMEDOUT' ||
      (error.response && error.response.status >= 500)
    );
  },
  onRetry: (retryCount, error, requestConfig) => {
    // Change proxy on each retry attempt
    const agent = getNextAgent();
    requestConfig.httpAgent = agent;
    requestConfig.httpsAgent = agent;
    console.log(`Attempt ${retryCount}, changing proxy due to: ${error.code}`);
  }
});

async function safeFetch(url) {
  const agent = getNextAgent();
  return client.get(url, {
    httpAgent: agent,
    httpsAgent: agent,
    proxy: false
  });
}

This combination — rotation plus retry — addresses 90% of the stability issues encountered during large-scale scraping of marketplaces. The remaining 10% are usually related to the anti-bot protection of the website itself (CAPTCHAs, TLS fingerprinting, header analysis) — this is already a separate topic beyond the configuration of axios.

Which Type of Proxy to Choose for axios Projects

The choice of proxy directly affects how many TLS errors and bans you will encounter. Different tasks require different types of IPs.

Task Recommended Type Why
Scraping Wildberries/Ozon Residential Proxies Real IPs from providers, less chance of hitting WAF
High-load API Monitoring Datacenter Proxies High speed and low cost per traffic volume
Bypassing geo-restrictions of strict APIs Mobile Proxies Carrier IPs are less likely to get on blocklists

For CI/CD and server-to-server integrations without strict anti-bot protection, datacenter proxies remain the most economical option — they establish a TLS tunnel faster and are less likely to timeout with a stable uplink.

Checklist Before Production

  • Always pass proxy: false along with httpsAgent
  • Never leave rejectUnauthorized: false in production
  • Escape username and password using encodeURIComponent
  • Add an explicit timeout — without it, axios may hang indefinitely
  • Use retry with proxy rotation, not just a simple retry on the same IP
  • Log the error code (err.code), not just the message
  • Test proxies via httpbin.org/ip before running the main script

Conclusion

Setting up a proxy in axios via HttpsProxyAgent requires attention to detail: the correct proxy: false flag, proper authentication, and competent handling of TLS errors. By adding IP rotation and retry logic, you obtain a resilient script for scraping marketplaces or monitoring APIs.

If your project involves large-scale data collection or bypassing regional restrictions, consider trying residential proxies for tasks with anti-bot protection or datacenter proxies for high-load integrations where response speed is crucial.