Proxy
Proxy
An intermediary server between a client and a server. Forward/reverse proxies. Used for caching, security, and load balancing.
Proxy
An intermediary server between a client and a server. Forward/reverse proxies. Used for caching, security, and load balancing.
A proxy means "agent" or "intermediary" and is an intermediate server that sits between a client and a server on a network and relays requests and responses. A proxy does more than simply forward traffic; it performs a variety of functions such as caching, security filtering, load balancing, anonymization, and SSL Termination. In modern web architecture, a proxy is a key component for performance optimization and enhanced security.
A Forward Proxy sits on the client side and sends requests to external servers on behalf of the client. In corporate networks, it is used for Internet access control, content filtering, and logging user activity. It provides anonymity by hiding the client's actual IP and plays a role similar to a VPN. Representative examples include Squid and Privoxy.
A Reverse Proxy sits on the server side, receives client requests, and forwards them to the actual server (Origin Server) behind it. The client sees only the reverse proxy and is unaware of the existence of the actual server. It provides a variety of functions such as load balancing (distributing traffic across multiple servers), SSL termination (handling HTTPS at the proxy), caching (storing frequently requested content), compression, and security (WAF integration, DDoS protection). Nginx, HAProxy, Apache Traffic Server, and Envoy are representative examples.
An API Gateway is a special form of reverse proxy that serves as the single entry point for all API requests in a microservices architecture. It provides API-specific functions such as authentication/authorization, Rate Limiting, request transformation, API version management, and monitoring. Examples include Kong, AWS API Gateway, and Traefik. A CDN (Content Delivery Network) is also a reverse proxy at global scale, caching content on edge servers around the world and responding from the location closest to the user.
# /etc/nginx/nginx.conf 또는 /etc/nginx/conf.d/app.conf
# 업스트림 서버 그룹 정의 (로드밸런싱)
upstream backend_servers {
# 라운드 로빈 (기본)
server 10.0.1.10:3000 weight=5; # 가중치 높음
server 10.0.1.11:3000 weight=3;
server 10.0.1.12:3000 backup; # 백업 서버
# 연결 유지 설정 (Keep-alive)
keepalive 32;
}
# API 서버 그룹 (최소 연결 방식)
upstream api_servers {
least_conn; # 연결 수가 가장 적은 서버로 라우팅
server 10.0.2.10:8080;
server 10.0.2.11:8080;
}
# HTTP 서버 블록
server {
listen 80;
server_name example.com www.example.com;
# HTTPS 리다이렉트
return 301 https://$server_name$request_uri;
}
# HTTPS 서버 블록
server {
listen 443 ssl http2;
server_name example.com www.example.com;
# SSL 인증서
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
# SSL 보안 설정
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
# 보안 헤더
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# Gzip 압축
gzip on;
gzip_types text/plain text/css application/json application/javascript;
gzip_min_length 1000;
# 정적 파일 캐싱
location /static/ {
alias /var/www/static/;
expires 30d;
add_header Cache-Control "public, immutable";
}
# API 요청 프록시
location /api/ {
proxy_pass http://api_servers;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 타임아웃 설정
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 버퍼링 설정
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
}
# WebSocket 프록시
location /ws/ {
proxy_pass http://backend_servers;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 86400; # 24시간
}
# 기본 요청 프록시
location / {
proxy_pass http://backend_servers;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
# 캐싱 설정
proxy_cache my_cache;
proxy_cache_valid 200 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating http_500 http_502;
add_header X-Cache-Status $upstream_cache_status;
}
# 헬스체크 엔드포인트
location /health {
access_log off;
return 200 "OK";
add_header Content-Type text/plain;
}
}
# 캐시 영역 정의 (http 블록에 추가)
# proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m max_size=1g;
import http from 'http';
import httpProxy from 'http-proxy';
import { URL } from 'url';
// 프록시 서버 생성
const proxy = httpProxy.createProxyServer({
changeOrigin: true, // 호스트 헤더 변경
ws: true, // WebSocket 지원
xfwd: true, // X-Forwarded-* 헤더 추가
proxyTimeout: 30000, // 30초 타임아웃
timeout: 30000,
});
// 에러 핸들링
proxy.on('error', (err, req, res) => {
console.error('Proxy error:', err);
if (res.writeHead) {
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Bad Gateway', message: err.message }));
}
});
// 프록시 요청 로깅
proxy.on('proxyReq', (proxyReq, req, res, options) => {
console.log(`[PROXY] ${req.method} ${req.url} -> ${options.target}`);
// 커스텀 헤더 추가
proxyReq.setHeader('X-Proxy-Timestamp', Date.now().toString());
});
// 프록시 응답 수정
proxy.on('proxyRes', (proxyRes, req, res) => {
// 응답 헤더 추가
res.setHeader('X-Proxied-By', 'my-proxy-server');
console.log(`[RESPONSE] ${req.url} - ${proxyRes.statusCode}`);
});
// 라우팅 규칙 정의
interface RouteConfig {
prefix: string;
target: string;
rewrite?: (path: string) => string;
}
const routes: RouteConfig[] = [
{
prefix: '/api/users',
target: 'http://user-service:3001',
rewrite: (path) => path.replace('/api/users', '/users'),
},
{
prefix: '/api/orders',
target: 'http://order-service:3002',
rewrite: (path) => path.replace('/api/orders', '/orders'),
},
{
prefix: '/api/products',
target: 'http://product-service:3003',
},
{
prefix: '/',
target: 'http://frontend:3000',
},
];
// 경로에 맞는 라우트 찾기
function findRoute(path: string): RouteConfig | undefined {
return routes.find(route => path.startsWith(route.prefix));
}
// HTTP 서버 생성
const server = http.createServer((req, res) => {
const route = findRoute(req.url || '/');
if (!route) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Route not found' }));
return;
}
// 경로 재작성
if (route.rewrite && req.url) {
req.url = route.rewrite(req.url);
}
// 프록시 실행
proxy.web(req, res, { target: route.target });
});
// WebSocket 업그레이드 처리
server.on('upgrade', (req, socket, head) => {
const route = findRoute(req.url || '/');
if (route) {
proxy.ws(req, socket, head, { target: route.target });
} else {
socket.destroy();
}
});
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});
import requests
from requests.auth import HTTPProxyAuth
# 1. 기본 HTTP/HTTPS 프록시 설정
proxies = {
'http': 'http://proxy.example.com:8080',
'https': 'http://proxy.example.com:8080',
}
response = requests.get(
'https://api.example.com/data',
proxies=proxies,
timeout=30
)
print(response.json())
# 2. 인증이 필요한 프록시
proxies_with_auth = {
'http': 'http://username:password@proxy.example.com:8080',
'https': 'http://username:password@proxy.example.com:8080',
}
# 또는 HTTPProxyAuth 사용
auth = HTTPProxyAuth('username', 'password')
response = requests.get(
'https://api.example.com/data',
proxies={'http': 'http://proxy.example.com:8080'},
auth=auth
)
# 3. SOCKS 프록시 (pip install requests[socks] 필요)
socks_proxies = {
'http': 'socks5://localhost:1080',
'https': 'socks5://localhost:1080',
}
response = requests.get(
'https://api.example.com/data',
proxies=socks_proxies
)
# 4. Session을 사용한 프록시 설정 (연결 재사용)
session = requests.Session()
session.proxies.update({
'http': 'http://proxy.example.com:8080',
'https': 'http://proxy.example.com:8080',
})
# 여러 요청에 동일한 프록시 사용
response1 = session.get('https://api.example.com/users')
response2 = session.get('https://api.example.com/orders')
# 5. 환경 변수로 프록시 설정
# export HTTP_PROXY=http://proxy.example.com:8080
# export HTTPS_PROXY=http://proxy.example.com:8080
# export NO_PROXY=localhost,127.0.0.1,.internal.com
import os
os.environ['HTTP_PROXY'] = 'http://proxy.example.com:8080'
os.environ['HTTPS_PROXY'] = 'http://proxy.example.com:8080'
# requests는 자동으로 환경 변수 사용
response = requests.get('https://api.example.com/data')
# 6. 특정 요청에서 프록시 비활성화
response = requests.get(
'https://internal.example.com/data',
proxies={'http': None, 'https': None} # 프록시 무시
)
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free