Invalid SockJS path β€˜XXX’ required to have 3 path segments.

ꡬ글에 Invalid SockJS path λΌλŠ” ν‚€μ›Œλ“œλ‘œ 검색해보면 이와 같은 였λ₯˜ λ‘œκ·Έμ— λŒ€ν•œ 쑰치둜 Stomp ν΄λΌμ΄μ–ΈνŠΈλ₯Ό μ‚¬μš©ν•˜λΌλŠ” λ‹΅λ³€μ΄λ‚˜ μŠ€νƒ μ˜€λ²„ν”Œλ‘œμš°μ— μ§ˆλ¬Έν•˜λΌλŠ” 닡변을 μ°Ύμ•„λ³Ό 수 μžˆλ‹€. μ•„λ¬΄νŠΌ μœ„ 상황에 λŒ€ν•œ 원인은 λͺ…ν™•νžˆ μ•Œ 수 μ—†λŠ” μƒν™©μ—μ„œ μ•„λž˜μ™€ 같은 κ΅¬μ‘°μ—μ„œ ν•΄λ‹Ή 였λ₯˜κ°€ λ°œμƒν–ˆλ‹€.

  1. Nginx Websocket Proxy
  2. Spring WebSocket with Stomp + SockJS
  3. sockjs-client@1.6.1
  4. @stomp/stompjs@7.0.0

이슈 νŒŒμ•…

nginx.conf와 같이 μ—”μ§„μ—‘μŠ€μ—μ„œ μ›Ήμ†ŒμΌ“ μ£Όμ†Œ νŒ¨ν„΄μ— λŒ€ν•΄ λ°±μ—”λ“œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μœΌλ‘œμ˜ λ¦¬λ²„μŠ€ ν”„λ‘μ‹œ ꡬ성을 μ•„λž˜μ™€ 같이 ν•΄λ‘” μƒνƒœμ˜€λ‹€. 그리고 Stomp λ°©μ‹μ˜ μ›Ή μ†ŒμΌ“ 연결을 μˆ˜ν–‰ν•˜λŠ” μ—”λ“œν¬μΈνŠΈλŠ” /ws/stomp 둜 μ •μ˜λ˜μ–΄μžˆμ—ˆλ‹€. 본래 /ws λŠ” 일반적인 μ›Ή μ†ŒμΌ“ 연결을 μˆ˜ν–‰ν•˜κ³  /ws/stmop둜 μ‹œμž‘λ˜λŠ” 것은 Stomp둜 λ™μž‘ν•˜λŠ” 것을 μ˜λ„ν•œ 것이닀.

nginx.conf
http { upstream backend { server app:8080; keepalive 128; } server { location /ws/ { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # hop-by-hop proxy_http_version 1.1; proxy_set_header Connection "upgrade"; proxy_set_header Upgrade $http_upgrade; proxy_read_timeout 65s; } } }

Vite 개발 μ„œλ²„μ—μ„œμ˜ Proxy κ΅¬μ„±μ˜ 경우 μ˜¬λ°”λ₯΄κ²Œ Stomp λ°©μ‹μ˜ μ›Ήμ†ŒμΌ“ 연결을 μˆ˜ν–‰λœλ‹€.

ν•˜μ§€λ§Œ, μ—”μ§„μ—‘μŠ€μ™€ ν•¨κ»˜ λ™μž‘μ€‘μΈ 배포 ν™˜κ²½μ—μ„œλŠ” Stomp ν΄λΌμ΄μ–ΈνŠΈκ°€ SockJSλ₯Ό μ‚¬μš©ν•˜μ—¬ 연결을 μˆ˜ν–‰ν•˜λ €κ³  ν• λ•Œ /ws/stomp/info?t=0 μ—”λ“œν¬μΈνŠΈμ— λŒ€ν•΄ 404 응닡을 λ°›κ²Œλ˜κ³  μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ‘œκ·Έμ—λŠ” Invalid SockJS path ...κ°€ 좜λ ₯λ˜λŠ” 것을 ν™•μΈν–ˆλ‹€.

μ†”λ£¨μ…˜

이리저리 μ‹œλ„ν•΄λ³Έ κ²°κ³Ό 해결책은 일반적인 μ›Ή μ†ŒμΌ“ μ—°κ²°κ³Ό Stomp λ°©μ‹μ˜ 연결을 μ•„μ˜ˆ λΆ„λ¦¬ν•˜λŠ” 것이닀. /ws/ 이외에 /ws-stomp/둜 Stomp λ°©μ‹μ˜ μ›Ή μ†ŒμΌ“ 연결을 μœ„ν•œ λ³„λ„μ˜ μ—”λ“œν¬μΈνŠΈ νŒ¨ν„΄μ„ μ‚¬μš©ν•˜κ³  λ¦¬λ²„μŠ€ ν”„λ‘μ‹œ ꡬ성을 ν•˜κ³ λ‚˜λ‹ˆ ν•΄λ‹Ή 증상은 λ°œμƒν•˜μ§€ μ•Šμ•˜λ‹€.

nginx.conf
http { upstream backend { server app:8080; keepalive 128; } server { location ~ ^/(ws|ws-stomp)/ { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # hop-by-hop proxy_http_version 1.1; proxy_set_header Connection "upgrade"; proxy_set_header Upgrade $http_upgrade; proxy_read_timeout 65s; } } }

μ•„λ¬΄νŠΌ μ •ν™•ν•œ 원인에 λŒ€ν•΄μ„œλŠ” λ³„λ„λ‘œ μ°Ύμ•„λ³΄μ•„μ•Όκ² μ§€λ§Œ 기둝으둜 λ‚¨κΈ°κ³ μž ν•œλ‹€.