Pitfalls of Configuring Axios Proxy Server

0 min read

Axios requests have a proxy property that can be used for proxying in Node.js. However, it's not perfect and will fail when proxying HTTPS requests, so it's best not to use it.

1
2
3
4
5
6
7
8
9
import axios from "axios";

axios.get("https://example.com", {
  proxy: {
    host: "127.0.0.1",
    port: 7890,
  },
});

When making HTTPS requests through an HTTP proxy, you need to first send a CONNECT method to notify the proxy server to establish a TCP tunnel to the target server. Otherwise, the proxy server will only send unencrypted requests to port 443 of the target server, which will fail. If you're not sure what I'm talking about, you can check out this article.

For a correct implementation, you can refer to the curl command. Set up an HTTP proxy (127.0.0.1:1025) using Node.js, then use the curl command to send an HTTPS request. The proxy server logs will show that it first sends a CONNECT method.

1
2
curl -v -X POST -x 127.0.0.1:1025 <https://cloudflare.com/>