๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

nginx์—์„œ ์„œ๋ฒ„๋กœ ๋„๋ฉ”์ธ ์ „๋‹ฌํ•˜๊ธฐ

by ํ‘์‹œ๋ฐ” 2023. 8. 26.

๐Ÿ˜ฅ ๋ฐฐ๊ฒฝ

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์š”์ฒญ์˜ ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํŠน์ • ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

 

ํ•„์ž๋Š” ๋„๋ฉ”์ธ ๊ฐ’์€ Request์—์„œ getServerName()์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ๊ณ  ์žˆ์–ด์„œ ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ getServerName()์—๋Š” ์„œ๋ฒ„์˜ ip ์ฃผ์†Œ(172.xxx.xxx.xxx)๋งŒ ๋ฐ›๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ฒŒ ๋˜์—ˆ๊ณ , ๊ฒฐ๊ตญ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ํด๋ผ์ด์–ธํŠธ ์›น ์„œ๋ฒ„(nginx) ์„ค์ • ๊นŒ์ง€ ํ™•์ธํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

โœ‚๏ธ ์„œ๋น„์Šค ๊ตฌ์„ฑ๋„

 

์„œ๋น„์Šค๋Š” ์™ธ๋ถ€์—์„œ ๋„๋ฉ”์ธ ๋’ค์— /api ... ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ, nginx์—์„œ proxy_pass ์„ค์ •์„ ํ†ตํ•ด ๋‚ด๋ถ€ IP ๋ฐฑ์—”๋“œ ์„œ๋ฒ„(172.xxx.xxx.xxx:8080)๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ฒฐ๊ตญ ์„œ๋ฒ„์— ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์€ 172.xxx.xxx.xxx:8080 ์ด ๋˜๊ณ  ์„œ๋ฒ„์—์„œ๋Š” ํ•„์š”ํ•œ ๋„๋ฉ”์ธ(blackshiba.com)์ด ์•„๋‹Œ ๋กœ์ปฌ ip๋กœ ๋ณด์˜€๋˜ ๊ฒƒ์ด๋‹ค.

โœ๏ธ ํ•ด๊ฒฐ

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค.

 

nginx proxy ์„ค์ •์—์„œ header์— Host ๊ฐ’์„ blackshiba.com์œผ๋กœ ์„ค์ •ํ•ด์„œ ๋ณด๋‚ด๋ฉด ๋œ๋‹ค!

 

์„œ๋ฒ„๋Š” ip๋กœ ์ ‘์†ํ•ด์•ผํ•˜๊ณ , ์‹ค์ œ ๋Œ€์ƒ ์›น ์„œ๋ฒ„์—๋Š” ๋„๋ฉ”์ธ์„ ๋„ฃ์–ด์„œ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด Host ๊ฐ’์„ ํ—ค๋”์— ํฌํ•จ์‹œ์ผœ์„œ ๋ณด๋‚ด์•ผ ํ•œ๋‹ค.

 

location / {
  proxy_pass http://172.xxx.xxx.xxx:8080;
  proxy_set_header Host blackshiba.com;
}

 

ํ•ด๋‹น ์„ค์ • ์ดํ›„ Request ๊ฐ’์—์„œ getServerName()์„ ์กฐํšŒํ•˜๋ฉด ๋„๋ฉ”์ธ ๊ฐ’์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

REFERENCE

https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Host

๋Œ“๊ธ€