Node.jsのプロキシ連携
In Node.js, the dependable way to send axios requests through a proxy is an agent: create an HttpsProxyAgent from the https-proxy-agent package with your Murphy proxy URL, pass it as httpsAgent, and set proxy: false so axios does not add its own proxy handling on top. Native fetch (Node 18 and later) takes a dispatcher instead, an undici ProxyAgent with the gateway address and your credentials as a Basic token. For SOCKS5, swap in socks-proxy-agent with the same host, port and credentials.
// npm install axios https-proxy-agent undici
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
import { ProxyAgent } from "undici";
// Residential gateway (gw-eu, gw-usa, gw-asia or gw-asia2), port 8888 or 7777 on Private 2.
// ISP proxy: one ip:port:user:pass line, as IP:PORT with USER and PASS.
const HOST = "gw-eu.murphyproxies.com:8888";
const USER = "USERNAME";
const PASS = "PASSWORD";
// The URL setters percent-encode special characters in the credentials.
const proxyUrl = new URL("http://" + HOST);
proxyUrl.username = USER;
proxyUrl.password = PASS;
// 1. axios through https-proxy-agent
const agent = new HttpsProxyAgent(proxyUrl);
const res = await axios.get("https://httpbin.org/ip", {
httpsAgent: agent,
proxy: false, // stop axios applying its own proxy settings on top of the agent
timeout: 30_000,
});
console.log("axios", res.data);
// 2. native fetch with an undici ProxyAgent as the dispatcher
const dispatcher = new ProxyAgent({
uri: "http://" + HOST,
token: "Basic " + Buffer.from(USER + ":" + PASS).toString("base64"),
});
const r = await fetch("https://httpbin.org/ip", {
dispatcher,
signal: AbortSignal.timeout(30_000),
});
console.log("fetch", await r.json());
// If the built-in fetch rejects the dispatcher, import { fetch } from "undici"
// as well, so fetch and ProxyAgent come from the same undici version.
// SOCKS5: same host, port and credentials (npm install socks-proxy-agent)
// import { SocksProxyAgent } from "socks-proxy-agent";
// const socks = new SocksProxyAgent("socks5h://" + proxyUrl.username + ":" + proxyUrl.password + "@" + HOST);
// await axios.get(url, { httpAgent: socks, httpsAgent: socks, proxy: false });5ステップで設定
- 1Install the packages with npm install axios https-proxy-agent undici (add socks-proxy-agent for SOCKS5), and run the file as an ES module (.mjs, or "type": "module" in package.json) so top-level await works.
- 2Put the Murphy host and credentials in the script: gw-eu.murphyproxies.com:8888 for residential (another gw- region, or 7777 on Private 2), or the IP and port of one ISP line with its user and pass.
- 3For axios, build an HttpsProxyAgent from the proxy URL, pass it as httpsAgent and set proxy: false.
- 4For fetch, create an undici ProxyAgent with the gateway URI and a Basic token, and pass it as the dispatcher on each call (or once for the whole process with undici's setGlobalDispatcher).
- 5Log the response from an IP echo endpoint to confirm the request left through the proxy, and keep the timeouts so a dead connection fails fast.
Node.jsのプロキシに関する質問
認証からローテーションまで、Node.jsをMurphy経由で使うためのポイントをまとめました。
axios has its own proxy option and also reads the HTTP_PROXY and HTTPS_PROXY environment variables in Node. If either is set, axios can apply a second layer of proxying on top of your agent and the request fails in confusing ways. proxy: false leaves the agent as the only thing deciding where the request goes.
For plain http:// targets it works, written as proxy: { protocol: "http", host, port, auth: { username, password } }. For https:// targets, that option has a long record of problems, which is why most people pass an HttpsProxyAgent instead. The agent tunnels HTTPS with CONNECT, the way browsers do.
Install socks-proxy-agent and create a SocksProxyAgent from a URL such as socks5h://USERNAME:PASSWORD@gw-eu.murphyproxies.com:8888 (or an ISP IP and port). Pass it as both httpAgent and httpsAgent in axios, with proxy: false. The socks5h scheme makes the proxy resolve hostnames, so DNS lookups do not leave from your machine.
An HTTPS request through a proxy runs inside a CONNECT tunnel, and the tunnel keeps one exit IP for as long as it stays open. undici keeps connections alive by default, so several fetch calls to the same site can share a tunnel and an IP. For a fresh IP each time, open a new connection per request; to hold one IP on purpose, use a sticky session from the dashboard.
Characters like @, : and / break a proxy URL written by hand. Set the credentials through a URL object (proxyUrl.username and proxyUrl.password), which percent-encodes them, as the snippet does, or run each part through encodeURIComponent before you build the string.
Node.js用の認証情報を取得
ターゲットに合ったネットワークを選びましょう。高速で安定したセッションにはISPプロキシ、厳しいボット対策には幅広いIPを持つレジデンシャルプロキシ。