C.W.K.
Stream
Lesson 03 of 07 · published

Reverse Proxy 구성

~11 min · production, nginx, caddy

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

WebSocket proxying 이 default 아냐

대부분 HTTP-1.1 reverse proxy 가 default 로 Upgrade header forward 안 함, 그래서 WebSocket handshake 가 proxy 자체에서 400 으로 실패. 명시적으로 enable 해야 함. Enable 후엔 idle timeout 늘려 long-lived WebSocket connection 이 mid-session 안 죽게.

Nginx vs Caddy

Nginx 가 명시적 proxy_set_header Upgrade $http_upgrade + Connection $connection_upgrade 필요. Caddy 가 WebSocket 감지하고 자동 forward — 특별 config 없음. Quick PoC 엔 Caddy 가 ergonomic 으로 이김; Nginx 가 production default 인 건 능력 아니라 친숙함 때문.

Code

Nginx 전체 WebSocket proxy·nginx
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 443 ssl http2;
    server_name api.example.com;

    ssl_certificate     /etc/ssl/example.com.crt;
    ssl_certificate_key /etc/ssl/example.com.key;

    location /ws {
        proxy_pass http://websocket_backend;
        proxy_http_version 1.1;

        # Forward upgrade headers
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        # Standard headers
        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;

        # Long-lived connections need long timeouts
        proxy_read_timeout  3600s;
        proxy_send_timeout  3600s;

        # Disable buffering for streaming
        proxy_buffering off;
    }
}
Caddy — 같은 일, 10 줄·text
api.example.com {
    reverse_proxy /ws/* websocket_backend:8000
    # Caddy auto-handles Upgrade headers and TLS via Let's Encrypt.
}

External links

Exercise

FastAPI WebSocket 서버 앞에 Nginx 세움. 의도적으로 Upgrade header 빼 — 400 응답 봐. 다시 추가; handshake 성공 확인. proxy_read_timeout 30s 로 설정 + idle connection 이 30s 마다 죽는 거 봐. 3600s 로 되돌려.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.