HTTP client ของ Node.js ทุกตัวจัดการ proxy แตกต่างกัน และครึ่งหนึ่งของพวกมันไม่จัดการ การยืนยันตัวตน ของ proxy เลยถ้าไม่มี helper package axios ละเลย proxy option ของตัวเองอย่างเงียบ ๆ สำหรับเป้าหมาย HTTPS ในบางเวอร์ชัน, fetch เนทีฟไม่มี proxy option ใด ๆ เลย และข้อความ error — ECONNRESET, 407 หรือแค่ค้างไปเฉย ๆ — ไม่บอกอะไรคุณเลย
นี่คือเอกสารอ้างอิงที่ครบถ้วนและใช้งานได้สำหรับการกำหนดเส้นทาง axios, got, node-fetch, fetch เนทีฟ (undici) และ superagent ผ่าน proxy residential ที่ยืนยันตัวตนแล้ว มันคือคู่หูฝั่ง Node ของคู่มือ Python ของเรา (requests/httpx/aiohttp, Scrapy) ตัวอย่างทั้งหมดใช้รูปแบบ placeholder มาตรฐาน — แทนที่ด้วยข้อมูลรับรองจริงของคุณ:
socks5h://USERNAME:PASSWORD@us.jibaoproxy.com:913
อย่าใช้ proxy option ที่มีในตัว ใช้ proxy agent แทน HTTP client ของ Node มอบหมายการจัดการการเชื่อมต่อให้กับอ็อบเจ็กต์ "agent" และ agent package (https-proxy-agent, socks-proxy-agent) implement CONNECT tunneling และการยืนยันตัวตนอย่างถูกต้อง option ที่มีในตัวมักจะไม่ทำ — การกำหนดค่า proxy ของ axios เป็นตัวอย่างที่ฉาวโฉ่ที่สุด
npm install socks-proxy-agent https-proxy-agent
const axios = require("axios");
const { SocksProxyAgent } = require("socks-proxy-agent");
const agent = new SocksProxyAgent(
"socks5h://USERNAME:PASSWORD@us.jibaoproxy.com:913"
);
const res = await axios.get("https://api.ipify.org?format=json", {
httpAgent: agent, // for http:// targets
httpsAgent: agent, // for https:// targets
proxy: false, // IMPORTANT: disable axios's own proxy handling
});
console.log(res.data); // -> the proxy's exit IP
สองสิ่งที่คนพลาด: คุณต้องตั้ง ทั้ง httpAgent และ httpsAgent (axios เลือกตาม scheme ของเป้าหมาย) และคุณต้องตั้ง proxy: false เพื่อให้ axios ไม่พยายามนำตัวแปรสภาพแวดล้อม HTTP_PROXY มาใช้ทับ agent ของคุณด้วย
const got = require("got");
const { SocksProxyAgent } = require("socks-proxy-agent");
const agent = new SocksProxyAgent(
"socks5h://USERNAME:PASSWORD@us.jibaoproxy.com:913"
);
const body = await got("https://api.ipify.org?format=json", {
agent: { http: agent, https: agent },
}).json();
ถ้าคุณกำลัง scraping เป้าหมายที่มีการป้องกันด้วย got ลองดู got-scraping แทน — API เดียวกัน บวกกับการสร้าง header เหมือนเบราว์เซอร์และการเลียนแบบ HTTP/2 fingerprint (ทำไมเรื่องนั้นถึงสำคัญ: อธิบาย JA3/JA4)
fetch ที่มีในตัวของ Node มาจาก undici, ละเลยตัวแปรสภาพแวดล้อมของ proxy และไม่มี agent option วิธีของ undici คือ dispatcher:
const { ProxyAgent } = require("undici");
// undici's ProxyAgent speaks HTTP CONNECT (use your HTTP proxy port)
const dispatcher = new ProxyAgent({
uri: "http://us.jibaoproxy.com:1000",
token: "Basic " + Buffer.from("USERNAME:PASSWORD").toString("base64"),
});
const res = await fetch("https://api.ipify.org?format=json", { dispatcher });
console.log(await res.json());
หมายเหตุ: ProxyAgent ของ undici รองรับเฉพาะ HTTP-proxy เท่านั้น สำหรับ SOCKS5 กับ fetch เนทีฟ ให้วาง local forwarder ไว้ด้านหน้ามัน หรือใช้ client ที่รับ socks agent ได้ (axios/got ด้านบน)
const fetch = require("node-fetch");
const { SocksProxyAgent } = require("socks-proxy-agent");
const agent = new SocksProxyAgent(
"socks5h://USERNAME:PASSWORD@us.jibaoproxy.com:913"
);
const res = await fetch("https://api.ipify.org?format=json", { agent });
ด้วย rotating residential gateway คุณไม่ต้องจัดการรายการ IP — gateway มอบจุดออกใหม่ให้คุณต่อการเชื่อมต่อ หรือยึดจุดออกหนึ่งต่อ session id ใน Node สิ่งนั้นแมปอย่างเรียบร้อยไปยัง หนึ่ง agent ต่อหนึ่งตัวตน:
// Sticky: same session id -> same exit IP across requests
function identityAgent(sessionId) {
return new SocksProxyAgent(
`socks5h://USERNAME-session-${sessionId}:PASSWORD@us.jibaoproxy.com:913`
);
}
// Account A keeps IP A, account B keeps IP B - cookies and IP move together
const agentA = identityAgent("acct_a");
const agentB = identityAgent("acct_b");
นำ agent กลับมาใช้ใหม่เพื่อการ pooling การเชื่อมต่อภายในตัวตนเดียว อย่าแชร์ agent เดียวข้ามตัวตน เมื่อไรควรหมุนเวียนเทียบกับยึดติดเป็นหัวข้อของมันเอง — ดู sticky เทียบกับ rotating sessions
| อาการ | สาเหตุ | วิธีแก้ |
|---|---|---|
407 Proxy Authentication Required | ข้อมูลรับรองไปไม่ถึง proxy | ใส่ user:pass ใน URL ของ agent ไม่ใช่ในการกำหนดค่าของ client |
| axios ทำงานบน http:// แต่ล้มเหลวบน https:// | ตั้งแค่ httpAgent เท่านั้น | ตั้ง httpsAgent ด้วย และ proxy: false |
ECONNRESET ทันที | พอร์ตผิด / โปรโตคอลผิด (พอร์ต HTTP กับ SOCKS agent) | จับคู่ประเภทของ agent ให้ตรงกับพอร์ต |
| DNS รั่ว / ชื่อโฮสต์ภายในล้มเหลว | socks5:// resolve DNS ในเครื่อง | ใช้ socks5h:// — ตัว h ส่งชื่อโฮสต์ผ่าน proxy |
| fetch เนทีฟละเลย HTTP_PROXY | undici ไม่อ่านตัวแปรสภาพแวดล้อม | ส่ง dispatcher อย่างชัดเจน |
| ทำงานในเครื่อง แต่ 403 บนเว็บไซต์เป้าหมาย | ไม่ใช่บั๊กของ proxy — เป็น TLS fingerprint | got-scraping หรือเบราว์เซอร์จริง ดู คู่มือ TLS |
socks-proxy-agent / https-proxy-agent) อย่าเชื่อใจ proxy option ที่มีในตัวproxy: false got: agent: {http, https} fetch เนทีฟ: dispatcher ProxyAgent ของ undicisocks5h:// เพื่อให้ DNS resolve ผ่าน proxy — ไม่มีการรั่วendpoint SOCKS5 + HTTP, sticky หรือ rotating, การคิดราคาต่อ GB — 500MB ทราฟฟิกฟรี ไม่ต้องใช้บัตร
เริ่มทดลองใช้ฟรีผู้ใช้ใหม่รับ 500MB เมื่อสมัครสมาชิก พร้อมโบนัสในการเติมเงินครั้งแรก ข้อเสนอมีระยะเวลาจำกัด