Search
Duplicate
๐Ÿ’ง

๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ with upstream

์ƒ์„ฑ์ผ
2022/11/13
ํƒœ๊ทธ
nginx
jenkins
infra
์ด ํฌ์ŠคํŒ…์€ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ ํฌ์ŠคํŒ…์— ์ด์–ด์„œ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋ชฉ์ฐจ

WS ์Šคํฌ๋ฆฝํŠธ ์„ค์ •

์ €ํฌ ํ”„๋กœ์ ํŠธ์˜ CI/CD ๋Š” jenkins ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ์ถ•ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ jenkins ๋Š” WAS ์„œ๋ฒ„์˜ ๋ฐฐํฌ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, WS ์˜ ๋ฐฐํฌ๋„ ํ•จ๊ป˜ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. WS ์—๋Š” ํ”„๋ก ํŠธ ํŒ€์›์ด ๊ฐœ๋ฐœํ•œ ์ •์  ํŽ˜์ด์ง€๊ฐ€ ์žˆ๊ณ , ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋ฅผ ์œ„ํ•œ NginX ์„œ๋ฒ„๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ์„œ๋ฒ„์ธ NginX๋Š” ์•ฑ ์„œ๋ฒ„๋ฅผ ์ˆจ๊ฒจ์ค„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์š”์ฒญ์— ๋Œ€ํ•œ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ ๊ธฐ๋Šฅ๋„ ์žˆ์–ด, ์ €ํฌ๋Š” ์ด NginX ์˜ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

NGNIX ์ดˆ๊ธฐ ์„ค์ •

events { worker_connections 768; } http { upstream morak { server {WAS1 IP}:8080 max_fails=1 fail_timeout=100s; server {WAS1 IP}:8081 max_fails=1 fail_timeout=100s; server {WAS2 IP}:8080 max_fails=1 fail_timeout=100s; server {WAS2 IP}:8081 max_fails=1 fail_timeout=100s; } server { server_name mo-rak.com www.mo-rak.com; location /api { proxy_pass http://morak; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; } } }
Shell
๋ณต์‚ฌ
์ „์ฒด ์Šคํฌ๋ฆฝํŠธ๋Š” https ๊ด€๋ จ ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ๊ธฐ๋„ ํ•˜๊ณ  ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ ๊ธฐ๋Šฅ์„ ์„ค๋ช…ํ•˜๊ธฐ์—๋Š” ๋ถˆํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ๊ณผ ๊ด€๋ จ๋œ ์„ค์ •๋งŒ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.
๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ default.conf ๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” sites-available ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” default.conf ํŒŒ์ผ์„ ์ปค์Šคํ…€ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํŒŒ์ผ์—๋Š” nginx ๊ตฌ๋™์„ ์œ„ํ•œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์ด ๋‹ด๊ฒจ ์žˆ๊ณ , https๋ฅผ ์ ์šฉํ•  ๋•Œ๊นŒ์ง€๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ €ํฌ๋„ ์ฒ˜์Œ์—๋Š” ์ด๊ฑธ ๋ชจ๋ฅด๊ณ  default.conf ์—์„œ http ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹คํ–‰ํ•˜์˜€๋Š”๋ฐ ์„œ๋ฒ„๊ฐ€ ๋œจ์ง€ ์•Š์•„ ๋กœ๊ทธ๋ฅผ ์‚ดํŽด๋ณด๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-available/default.conf
Bash
๋ณต์‚ฌ
๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•  http, upstream ๊ณผ ๊ฐ™์€ ๋ถ€๊ฐ€์ ์ธ ์˜ต์…˜์€ default.conf ์—์„œ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ /etc/nginx ์˜ nginx.conf ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
http ์˜ upstream ์—์„œ๋Š” ์š”์ฒญ์„ ๋ฐ›์„ ์„œ๋ฒ„์˜ ๋ชฉ๋ก์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. nginx ์—์„œ๋Š” upstream ์„œ๋ฒ„์˜ ๋ชฉ๋ก์„ ์ง€์ •ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋งž๊ฒŒ ์š”์ฒญ์„ ๋ถ„์‚ฐ์‹œํ‚ต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋Š” weighted-Round-Robin, hash, IP-hash, ์ตœ์†Œ ์—ฐ๊ฒฐ(least-connection), ์ตœ์†Œ ์‘๋‹ต ์‹œ๊ฐ„(least-time) ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ๊ธฐ๋ณธ๊ฐ’์€ weighted-Round-Robin ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ €ํฌ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ฐ€์ค‘์น˜๊ฐ€ ๋ชจ๋‘ ๊ฐ™์€ Round-Robin ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๊ณ , ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
โ€ข
๋ฌด์ค‘๋‹จ ๋ฐฐํฌ์— ์นด๋‚˜๋ฆฌ์•„ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„ ๊ตณ์ด ์‚ฌ์šฉ์ž๊ฐ€ ๋™์ผํ•œ WAS ๋ฅผ ์ด์šฉํ•  ํ•„์š”๋Š” ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
โ€ข
WAS ์„œ๋ฒ„์˜ ์ž์› ์ƒํƒœ๋Š” ๋™์ผํ•˜๊ณ  API ๋งˆ๋‹ค์˜ ์‘๋‹ต ์†๋„์˜ ์ฐจ์ด๊ฐ€ ๊ทน๋ช…ํ•˜์ง€ ์•Š๊ธฐ์— ์„œ๋ฒ„๋งˆ๋‹ค ๊ท ๋“ฑํ•˜๊ฒŒ ์š”์ฒญ๋ฐ›๋Š” ๋ฐฉ์‹์ด ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์€ ๋‹ค์–‘ํ•œ๋ฐ, max_fails ์™€ fail_timeout ์„ ์„ค์ •ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ๋ก์— ๋„ฃ์€ 4๊ฐœ์˜ ์„œ๋ฒ„ ์ค‘ ๋ธ”๋ฃจ/๊ทธ๋ฆฐ ๋ฐฐํฌ ๋ฐฉ์‹์œผ๋กœ ์ธํ•ด 2๊ฐœ๋Š” ํ•ญ์ƒ ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋˜์–ด์žˆ๋Š” ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ด ๋•Œ nginx ์—์„œ ์ข…๋ฃŒ๋˜์–ด์žˆ๋Š” ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ธ ๋’ค ์‘๋‹ต์ด ์—†์œผ๋ฉด ๋‹ค์Œ ์„œ๋ฒ„๋กœ ํ•ด๋‹น ์š”์ฒญ์„ ๋ฐ”๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์ด ๊ณ„์†๋˜๋ฉด 100๊ฐœ์˜ ์š”์ฒญ ์ค‘ 50๊ฐœ๋Š” ํ•ญ์ƒ ์ข…๋ฃŒ๋˜์–ด์žˆ๋Š” ์„œ๋ฒ„๋กœ ์š”์ฒญ์ด ํ•œ ๋ฒˆ์”ฉ์€ ์ „๋‹ฌ๋˜๊ธฐ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๊ณ , ์„ค์ •ํ•œ max_fails ํšŸ์ˆ˜๋ฅผ ๋„˜์„ ์‹œ fail_timeout ๋งŒํผ ํ•ด๋‹น ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด์ง€ ์•Š๋Š”๋‹ค๋Š” ์„ค์ •์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐํฌ๊ฐ€ ์ง„ํ–‰๋˜์–ด ์šด์˜์ค‘์ธ ํฌํŠธ๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ, 100์ดˆ์˜ ์‹œ๊ฐ„์ œํ•œ์ด ๊ฑธ๋ ค์žˆ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ์ด๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
C ์‹œ์  ์ดํ›„ 97์ดˆ ๋™์•ˆ์€ 4๊ฐœ์˜ ์„œ๋ฒ„ ๋ชจ๋‘ ์œ ๋ฐฐ ์ค‘์ธ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ WAS ๋Š” 8080 ํฌํŠธ์—์„œ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š”๋ฐ, 100 ์ดˆ๋™์•ˆ ์•„๋ฌด ์‘๋‹ต๋„ ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ์— ๋น ์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  fail_timeout ์‹œ๊ฐ„์„ ์ค„์ด๋ฉด, ์•ž์„œ ์–ธ๊ธ‰ํ•œ๋Œ€๋กœ ๋ธ”๋ฃจ ์„œ๋ฒ„๋กœ ์š”์ฒญํ•˜๋Š” ๋นˆ๋„๊ฐ€ ๋Š˜์–ด๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์ด์— ๋”ฐ๋ผ ๋ฐฐํฌํ•  ๋•Œ๋งˆ๋‹ค ์„œ๋ฒ„ ๋ชฉ๋ก์— ์ƒˆ๋กœ์šด ๋ฒ„์ „์ด ์‹คํ–‰๋œ ์„œ๋ฒ„๋“ค์„ ๋™์ ์œผ๋กœ ํ• ๋‹นํ•˜๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
http { upstream morak { server ${service_url_1}; server ${service_url_2}; } }
Bash
๋ณต์‚ฌ
ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ๋˜ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. nginx ๋ฅผ reload ํ•  ๋•Œ ${service_url_1} ๊ณผ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค์˜ ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ• ๋‹นํ•˜๊ณ ์ž ํ–ˆ๋Š”๋ฐ, ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ฝ์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค.
nginx: [emerg] unknown directive "service_url_1" in /etc/nginx/nginx.conf
Bash
๋ณต์‚ฌ
์ฐพ์•„๋ณด๋‹ˆ nginx ์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋ธ”๋ก ๋‚ด์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
โ€ข
์„œ๋ฒ„ ๋ชฉ๋ก์œผ๋กœ ๊ฐ๊ฐ 8080, 8081 ์ด ์žˆ๋Š” ํŒŒ์ผ ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด jenkins ์—์„œ ๋ณด๋‚ด์ค„๊นŒ?
โ—ฆ
๋งŒ์•ฝ ํฌํŠธ์˜ ์ˆœ์„œ๊ฐ€ ๋งž์ง€ ์•Š๊ฑฐ๋‚˜, ์„œ๋ฒ„ ์—๋Ÿฌ๋กœ ์ˆœ์„œ๊ฐ€ ์—‰ํ‚ค๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋“œ์ฝ”๋”ฉ์˜ ๋ฌธ์ œ์ ์ด ๊ทธ๋Œ€๋กœ ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
โ€ข
๊ทธ๋ฆฐ ์„œ๋ฒ„์˜ ๋ชฉ๋ก์„ jenkins ์—์„œ ์•Œ๊ณ  ์žˆ์œผ๋‹ˆ๊นŒ ํ•ด๋‹น ๋ถ€๋ถ„๋งŒ ๋™์ ์œผ๋กœ ๋ฐ”๊พธ๊ณ , ๊ธฐ์กด์˜ nginx.conf ํŒŒ์ผ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ž…๋ ฅํ•˜์—ฌ ๋ถ™์—ฌ๋ณผ๊นŒ?
โ—ฆ
๋™์ ์œผ๋กœ ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๊ธด ํ•˜์ง€๋งŒ, ํ•ด๋‹น ๋ถ€๋ถ„๋งŒ ๋ฐ”๊พธ์–ด ์ฃผ๋ฉด ๋˜๋Š”๋ฐ ๋‹ค๋ฅธ ๋ชจ๋“  ๋ถ€๋ถ„๊นŒ์ง€ ์ง์ ‘ ์ถœ๋ ฅํ•˜๋Š” ๋กœ์ง์„ ์งœ์•ผ ๋˜๊ธฐ๋•Œ๋ฌธ์— ๋ณด๋ฅ˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ด๋Ÿฐ ์ƒ๊ฐ๋“ค์„ ํ•˜๋‹ค๊ฐ€ envsubst ๋ผ๋Š” ๋ฆฌ๋ˆ…์Šค ๋ช…๋ น์–ด๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. envsubst ๋Š” ์ง€์ •ํ•œ ํŒŒ์ผ์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋Œ€์ž…ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. upstream ์˜ ์„œ๋ฒ„ ๋ชฉ๋ก๋งŒ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํฌ๋งท์œผ๋กœ ํ‘œํ˜„๋œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ , ๊ทธ๋ฆฐ ์„œ๋ฒ„ ๋ชฉ๋ก์„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•œ ๋’ค envsubst ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰์‹œ์ผœ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ ๋ฐœ์ƒ์€ ๋์ด ์—†์Šต๋‹ˆ๋‹ค.. ๊ฑฐ์˜ ๋‹ค ์˜จ๊ฑฐ๊ฐ™์€๋ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. nginx ์˜ ์„ค์ • ํŒŒ์ผ์—๋Š” nginx ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ location ๋ธ”๋ก ๋‚ด๋ถ€์˜ proxy_set_header X-Real-IP $remote_addr;๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. envsubst ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ฐพ์„ ๋•Œ $ ๋ฅผ ๋ณด๊ณ  ์ฐพ๋Š”๋ฐ, ํ•ด๋‹น nginx ๊ฐ€ ์ด์šฉํ•˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊นŒ์ง€ ๋ชจ๋‘ ์น˜ํ™˜ํ•ด๋ฒ„๋ฆฌ๊ธฐ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํŒŒ์ผ์— ํ•ด๋‹น ๋ถ€๋ถ„์ด ๋ชจ๋‘ ๊ณต๋ฐฑ์œผ๋กœ ์น˜ํ™˜๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด $ ๋ฅผ $๋กœ ์น˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ๊ณ  ๊ธฐ์กด์— nginx ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋Š” proxy_set_header X-Real-IP ${dollar}remote_addr; ๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ${dollar} ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•˜์—ฌ dollar ๋Š” $๋กœ ๋ฐ”๋€Œ๋„๋ก ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
โ€ข
์ตœ์ข… nginx ์„ค์ • ํŒŒ์ผ
http { upstream morak { server ${service_url_1}; server ${service_url_2}; } server { server_name mo-rak.com www.mo-rak.com; location /api { proxy_pass http://morak; proxy_set_header X-Real-IP ${dollar}remote_addr; proxy_set_header X-Forwarded-For ${dollar}proxy_add_x_forwarded_for; proxy_set_header Host ${dollar}http_host; } } }
Bash
๋ณต์‚ฌ
๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ฆฐ ์„œ๋ฒ„์™€ dollar ๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•˜๊ณ  envsubst ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์Šคํฌ๋ฆฝํŠธ๋ฅผ jenkins ์—์„œ ์‹คํ–‰์‹œ์ผœ ์ฃผ๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
โ€ข
route ์Šคํฌ๋ฆฝํŠธ
rm nginx.env echo "export service_url_1=\"$1\"" >> nginx.env echo "export service_url_2=\"$2\"" >> nginx.env echo "export dollar=\"\$\"" >> nginx.env source nginx.env envsubst < /etc/nginx/nginx-template.conf | sudo tee /etc/nginx/nginx.conf sudo service nginx reload
Bash
๋ณต์‚ฌ
envsubst ๋ช…๋ น์–ด๋กœ ์•ž์„œ ๋“ฑ๋กํ•œ ๊ทธ๋ฆฐ ์„œ๋ฒ„์˜ ๋ชฉ๋ก์„ ์น˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ณ  ์ด๋ฅผ tee ๋ช…๋ น์–ด๋กœ ๊ธฐ์กด์˜ nginx.conf ํŒŒ์ผ์„ ๋ฎ์–ด์“ฐ๋„๋ก ๊ตฌ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ