경로(prefix)로 서브앱 붙이기: /social, /playdrawing 같은 구성
한 도메인에서 /social/ 같은 경로로 다른 앱(프록시/정적)을 붙일 때의 nginx 설정과 주의점
서브도메인을 새로 파기 어려운 상황에서는, 한 도메인 안에서 경로(prefix)로 앱을 붙이기도 합니다.
https://example.com/social/→ 별도 API 서버로 프록시https://example.com/playdrawing/→ 별도 정적 SPA를 alias로 서빙
이 방식은 빠르지만, 슬래시(/)와 경로 결합에서 실수가 자주 납니다.
1) /social → 프록시(백엔드 서버)
트레일링 슬래시 정규화
location = /social {
return 302 /social/;
}
실제 프록시
location ^~ /social/ {
proxy_pass http://127.0.0.1:4600;
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;
}
2) /playdrawing → 정적 SPA alias
location = /playdrawing {
return 301 /playdrawing/;
}
location ^~ /playdrawing/ {
alias /srv/playdrawing/public/;
try_files $uri $uri/ /playdrawing/index.html;
}
3) 가장 흔한 함정 3가지
/social과/social/을 섞어 쓰면 상대경로/쿠키 path가 꼬임proxy_pass끝의/유무로 업스트림 요청 경로가 바뀜- SPA의 asset 경로가 절대경로(
/assets/...)면 prefix 아래에서 깨짐
관련 레시피: