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

Proxy 사용 사례

~20 min · auth gate, geo, A/B testing

Level 0Curious
0 XP0/68 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Auth gate

Session cookie read. Sign-out user 를 protected path 에서 redirect, sign-in user 를 /login 에서 redirect.

Geolocation routing

Vercel 이 country / region / city 가진 request.geo 노출. 첫 hit 에 locale-specific path 로 redirect.

A/B test bucketing

첫 visit 에 variant 고르는 cookie 설정. 후속 request 가 cookie read 하고 그에 맞게 route. Bucket 이 server state 없이 navigation 너머로 살아남음.

Header rewriting

Security header, request ID, downstream code 가 read 가능한 feature-flag header 추가.

Code

Auth gate·ts
import { NextRequest, NextResponse } from 'next/server';

export function proxy(request: NextRequest) {
  const session = request.cookies.get('session');
  const isAuth = request.nextUrl.pathname.startsWith('/login');
  const isProtected = request.nextUrl.pathname.startsWith('/app');

  if (isProtected && !session) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
  if (isAuth && session) {
    return NextResponse.redirect(new URL('/app', request.url));
  }
  return NextResponse.next();
}
Geo redirect·ts
export function proxy(request: NextRequest) {
  const country = request.geo?.country ?? 'US';
  if (country === 'KR' && !request.nextUrl.pathname.startsWith('/ko')) {
    return NextResponse.redirect(new URL('/ko' + request.nextUrl.pathname, request.url));
  }
  return NextResponse.next();
}
A/B bucket·ts
export function proxy(request: NextRequest) {
  const bucket = request.cookies.get('ab')?.value;
  const res = NextResponse.next();
  if (!bucket) {
    res.cookies.set('ab', Math.random() < 0.5 ? 'A' : 'B', {
      maxAge: 60 * 60 * 24 * 30,
      httpOnly: true,
    });
  }
  return res;
}

External links

Exercise

proxy.ts 에 동작 셋 추가: auth gate, security header injection (예: CSP), A/B cookie. 각각 curl 로 검증.

Progress

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

댓글 0

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

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