통합 · 초보자

fetch, undici, axios 및 got로 Node.js에서 프록시를 사용하는 방법

지원된 Node.js 릴리스에서, NODE USE ENV PROXY=1는 HTTP PROXY, HTTPS PROXY 및 NO PROXY를 내장 fetch에 사용할 수 있습니다. --use-env-proxy는 Node.js 22.21+ 및 24.5+에서 사용할 수 있습니다. 명시된 undici ProxyAgent 또는 EnvHttpProxyAgent는 대안입니다. 자신의 수송 또는 사용자 지정 에이전트가 구성을 필요로하는 라이브러리는, 각 프로세스가 아닌 다른 프로세스보다는 모두 처리됩니다.

2 분 읽음 6개의 단면도 검토일 편집팀 Proxy Cyber

내장 fetch 및 undici#

Node의 글로벌 fetch undici입니다. 그것을 주십시요 ProxyAgent 요청당 dispatcher 옵션, 또는 한 번 글로벌 설정 setGlobalDispatcher.

undici의
import { fetch, ProxyAgent, setGlobalDispatcher } from "undici";

const proxy = new ProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");

// Per request
const res = await fetch("https://api.ipify.org", { dispatcher: proxy });
console.log(await res.text());

// Or globally, for every fetch in the process
setGlobalDispatcher(proxy);

A ProxyAgent 수영장 연결, 그래서 연속 요청은 한 연결과 따라서 한 출구를 재사용 할 수 있습니다. 요청에 따라 신선한 출구를 만들면 에이전트를 만듭니다. pipelining: 0 으로 connections: 1, 또는 단순히 다른 사용 -sid- 논리 세션 당.

엑소서스#

axios의 자체 proxy 옵션은 HTTPS를 안정적으로 터널하지 않습니다. 표준 접근법은 에이전트입니다. https-proxy-agent 으로 proxy: false.

엑소서스
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";

const agent = new HttpsProxyAgent("http://USER-cc-de-city-berlin-sid-cart42-ttl-15m:PASS@res.proxy-cyber.com:9000");
const client = axios.create({ httpsAgent: agent, httpAgent: agent, proxy: false, timeout: 30000 });

const { data } = await client.get("https://api.ipify.org");
console.log(data);

got#

got
import got from "got";
import { HttpsProxyAgent } from "https-proxy-agent";

const agent = new HttpsProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
const body = await got("https://api.ipify.org", { agent: { https: agent, http: agent }, timeout: { request: 30000 } }).text();
console.log(body);

양말5#

양말-proxy-agent
import fetch from "node-fetch";
import { SocksProxyAgent } from "socks-proxy-agent";

const agent = new SocksProxyAgent(process.env.PROXY_URL);
const response = await fetch("https://api.ipify.org", { agent });
console.log(await response.text());

환경 변수#

undici는 읽지 않습니다 HTTPS_PROXY 기본적으로. 최근 undici version ship EnvHttpProxyAgent, 이는; 세계 파견자와 전체 프로세스로 설정, 제 3 자 라이브러리를 포함 하 여 fetch에 내장, 프록시를 통해 이동.

EnvHttpProxy가
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
// HTTPS_PROXY=http://USER-cc-us:PASS@res.proxy-cyber.com:9000 NO_PROXY=localhost node app.js
setGlobalDispatcher(new EnvHttpProxyAgent());

통화 및 교체#

  • 1개 ProxyAgent 끈끈한 세션당 (다른) -sid-), 그 세션의 모든 요청에 의해 공유.
  • 넓은 교체를 위해, 1개의 대리인을 지키고 그러나 연결 재사용을 해제하고, 또는 N 회의 ids와 둥근robin를 가진 N 대리인을 그(것)들을 실행하십시오.
  • Watch UND_ERR_SOCKET 으로 ECONNRESET: 주거 출구가 나갔다; 한 번 재시행, 다음 연결은 다른 것을 선택합니다.

자주 묻는 질문#

왜 axios는 HTTPS에 대한 내 프록시를 무시합니까?

내장 프록시 지원은 CONNECT 터널에 제한됩니다. httpsAgent와 설정 프록시로 https-proxy-agent를 사용하십시오: false so axios는 자체를 처리하지 않습니다.

몇 가지 요청만 프록시 할 수 있습니까?

예. 필요한 요청에 대한 파견자 또는 대리인을 통과하고 다른 사람을 비난; setGlobalDispatcher를 호출하지 않는 한 아무 것도 글로벌하지 않습니다.

당신의 계정

한 계정에서 프록시 관리

네트워크, 장소 주문 및 한 대시보드에서 균형 및 지원 요청을 따르십시오. 우리의 팀 리뷰 지불 및 배달 후 연결 세부 사항을 제공합니다.

계정 만들기