การตั้งค่า Proxy ใน Node.js: axios, got, fetch และ undici (คู่มือ 2026)

เผยแพร่เมื่อ 6 มิถุนายน 2026 · อ่าน ≈10 นาที

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

axios

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 ของคุณด้วย

got

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)

Native fetch / undici (Node 18+)

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 ด้านบน)

node-fetch (v2/v3)

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 });

การหมุนเวียน: ตัวตนใหม่ต่อ Request เทียบกับ Sticky Sessions

ด้วย 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_PROXYundici ไม่อ่านตัวแปรสภาพแวดล้อมส่ง dispatcher อย่างชัดเจน
ทำงานในเครื่อง แต่ 403 บนเว็บไซต์เป้าหมายไม่ใช่บั๊กของ proxy — เป็น TLS fingerprintgot-scraping หรือเบราว์เซอร์จริง ดู คู่มือ TLS
Free tool · no signup

ตรวจสอบ proxy ของคุณก่อนที่จะ debug โค้ด

ครึ่งหนึ่งของ "โค้ด proxy Node ของฉันพัง" แท้จริงแล้วคือ proxy ที่ตายหรือกำหนดค่าผิด วาง URL ของ proxy ลงในเครื่องมือตรวจของเรา — มันทดสอบการเชื่อมต่อ, การยืนยันตัวตน, จุดออก IP และ latency ในครั้งเดียว

ตรวจสอบ proxy ของฉัน →

ต้องการ IP ที่ผ่านการตรวจสอบชื่อเสียงด้วยใช่ไหม? รับ proxy residential พร้อม500MB ทราฟฟิกฟรี →

สรุป

Residential Proxies สำหรับ Node.js

endpoint SOCKS5 + HTTP, sticky หรือ rotating, การคิดราคาต่อ GB — 500MB ทราฟฟิกฟรี ไม่ต้องใช้บัตร

เริ่มทดลองใช้ฟรี

สำหรับผลิตภัณฑ์ IP ทุกประเภท · พูลโหนดขนาดมหึมาพร้อมใช้งานได้ทุกเมื่อ

สมัครสมาชิกตอนนี้และรับเงินคืนการเติมเงินสูงสุด 100%

ผู้ใช้ใหม่รับ 500MB เมื่อสมัครสมาชิก พร้อมโบนัสในการเติมเงินครั้งแรก ข้อเสนอมีระยะเวลาจำกัด