Node.js에서 요청이 성공해도 지정한 프록시를 거쳤다는 보장은 없습니다. 같은 IP 조회 URL에 직접 연결한 결과와 프록시를 적용한 결과를 비교한 뒤 실제 프로그램에 넣으세요.
내장 fetch와 환경 변수
프로젝트에서 사용하는 Node.js 버전이 환경 변수 기반 프록시를 지원하는지 먼저 확인합니다. 지원 버전에서는 환경 변수 사용을 활성화한 뒤 스크립트를 실행합니다.
const response = await fetch('https://ipinfo.io/json');
console.log(await response.json());NODE_USE_ENV_PROXY=1 HTTPS_PROXY="http://USERNAME:PASSWORD@HOST:PORT" node env-fetch.mjsundici에서 연결 단위로 지정하기
프로그램 전체의 환경을 바꾸기 어려우면 요청에 dispatcher를 지정합니다. 아래처럼 fetch와 ProxyAgent를 같은 undici 패키지에서 가져와 버전 차이로 인한 오류를 줄일 수 있습니다.
import { fetch, ProxyAgent } from 'undici';
const proxy = new ProxyAgent('http://USERNAME:PASSWORD@HOST:PORT');
const response = await fetch('https://ipinfo.io/json', { dispatcher: proxy });
console.log(await response.json());axios와 SOCKS5
axios의 HTTP 프록시는 호스트, 포트, 인증 항목을 분리해 입력합니다. PORT 자리는 실제 숫자로 교체하세요.
import axios from 'axios';
const response = await axios.get('https://ipinfo.io/json', {
proxy: {
protocol: 'http',
host: 'HOST',
port: PORT,
auth: { username: 'USERNAME', password: 'PASSWORD' },
},
timeout: 15_000,
});
console.log(response.data);SOCKS5는 SOCKS용 에이전트를 연결합니다. proxy: false로 axios의 내장 HTTP 프록시 설정이 중복 적용되는 일을 막을 수 있습니다.
import axios from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';
const agent = new SocksProxyAgent('socks5h://USERNAME:PASSWORD@HOST:PORT');
const response = await axios.get('https://ipinfo.io/json', {
httpAgent: agent,
httpsAgent: agent,
proxy: false,
timeout: 15_000,
});
console.log(response.data);응답 점검
인증 거절과 호스트 연결 실패를 구분하세요. 같은 자격 증명을 curl로 확인하면 라이브러리 설정 문제를 분리할 수 있습니다. 주거용은 경로 사용자 이름에 설정된 지역·세션을 사용하며, 고정 ISP는 해당 IP에 배정된 주소와 포트를 사용합니다.