ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 03/25(Thu) ํฌํŠธํฌ์›Œ๋”ฉ์ด ์•ˆ๋  ๋•Œ
    HangHae99/TIL-hanghae99 2021. 3. 25. 12:16

    ๐Ÿšฉ ์ด์ƒํ•˜๊ฒŒ ํฌํŠธํฌ์›Œ๋”ฉ๋งŒ ์•ˆ๋œ๋‹ค? ์›์ธ์„ ์•Œ์•„๋ณด์ž

    ๊ณผ์ œ ์‹œ์ž‘ ์ „์— week04 ๋ฐฐํฌํ•˜๊ธฐ ์ „์—๋„ ํฌํŠธํฌ์›Œ๋”ฉ๋งŒ ์•ˆ๋์—ˆ๋‹ค. '์•„์ดํ”ผ ์ฃผ์†Œ:8888' ๋Š” ์ž˜ ๋˜๋Š”๋ฐ, ํฌํŠธํฌ์›Œ๋”ฉ ํ›„ 8888์„ ๋–ผ๋ฒ„๋ฆฌ๋ฉด ์›น์‚ฌ์ดํŠธ๊ฐ€ ๋Œ์•„๊ฐ€์ง€ ์•Š์•˜๋‹ค.

    ๊ทธ ๋•, ์ผ๋‹จ ๊ณผ์ œ๋ถ€ํ„ฐ ์‹œ์ž‘ ํ•˜๋Š” ๊ฒŒ ์šฐ์„ ์ด๋ผ ํŒ๋‹จํ–ˆ๋‹ค. ๊ณผ์ œ๋„ ์™„์„ฑํ•˜๊ณ ๋‚˜๋ฉด ๋ฐฐํฌ๋ฅผ ํ•ด์•ผ ๋˜๋‹ˆ๊นŒ ๊ณผ์ œ ๋ฐฐํฌํ•  ๋•Œ ๊ทธ ๋•Œ ํ•ด๋ณด๊ณ  ๋˜ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด ๊ทธ ๋•Œ ํ•ด๊ฒฐํ•˜์ž์‹ถ์—ˆ๋Š”๋ฐ ๊ณผ์ œ๋ฅผ ๋ฐฐํฌํ•˜๋Š” ์ด ์‹œ์ ์— ๋˜ ํฌํŠธํฌ์›Œ๋”ฉ๋งŒ ์•ˆ๋œ๋‹ค.

    ๐Ÿ“• ๊ณผ์ •

    localhost:8888 -> ์•„์ดํ”ผ์ฃผ์†Œ:8888 ์€ ์ž˜๋œ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง„ ์ž˜ ์—ฐ๊ฒฐ์ด ๋๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

    ์—ฌ๊ธฐ์„œ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•˜๊ธฐ ์œ„ํ•ด git bash์— ํฌํŠธํฌ์›Œ๋”ฉ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ–ˆ๋‹ค.

    sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

     

    ์ด๊ฑธ ๊ทธ๋Œ€๋กœ git bash์— ๋ณต์‚ฌ๋ฅผ ํ–ˆ๋Š”๋ฐ ๋‹ค์‹œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์ค„๋กœ ๋„˜์–ด๊ฐ€๋ฒ„๋ ธ๋‹ค.

    ubuntu@ip:~$ 

    ... ์ด ๋ช…๋ น์ค„์ด ์ปค์„œ๋ฅผ ๊นœ๋ฐ•์ด๋ฉด์„œ ๋Œ€๊ธฐ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ ์ž–์ด ๋‹นํ™ฉํ–ˆ๋‹ค.

    ๋‚˜๋Š” ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ 8888์„ ์“ฐ๊ณ  ์žˆ์–ด์„œ ๋’ค์— to port 8080 ์œผ๋กœ ๋ผ์žˆ๋Š” ๋ถ€๋ถ„์„ 8888๋กœ ๋ฐ”๊ฟ”์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

     

    ํฌํŠธํฌ์›Œ๋”ฉ ๋ช…๋ น์–ด๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ž…๋ ฅํ•˜๊ณ  ๋’ค ์ˆซ์ž๋ฅผ 8888๋กœ ๋ฐ”๊พธ๋ฉด ์ตœ๊ทผ์— ํ•œ ๊ฑธ๋กœ ๋ฎ์–ด์“ฐ๊ธฐ ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์–ด ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์ˆซ์ž๋งŒ ๋ฐ”๊ฟ”์„œ ๋‹ค์‹œ ์ž…๋ ฅํ–ˆ๋‹ค.

    ๊ธฐ๋Œ€๋ฅผ ํ•˜๊ณ  ์•„์ดํ”ผ์ฃผ์†Œ๋กœ๋งŒ ๋“ค์–ด๊ฐ€๋ดค๋Š”๋ฐ ์˜ค! ์—ญ์‹œ ์•ˆ๋˜๋”๋ผ. ๋’ค์— ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์—ฌ์„œ ๋“ค์–ด๊ฐ€๋‹ˆ ๋‹น์—ฐํžˆ ๊ทธ๊ฑด ๋Œ์•„๊ฐ€๋”๋ผ.

     

     

    ๊ณผ์ œ ์‹œ์ž‘ ์ „์— week04 ์ž‘์—…ํ•  ๋•Œ๋Š” ์™œ ํฌํŠธํฌ์›Œ๋”ฉ์ด ์•ˆ๋˜๋Š”์ง€ ์ด์œ ์กฐ์ฐจ ๋ชฐ๋ž์ง€๋งŒ, ๊ณผ์ œ ํ•˜๋ฉด์„œ ๋งˆ์ฃผ์นœ ์ง€๊ธˆ ์ด ์ƒํ™ฉ์€ ๋‚ด๊ฐ€ ๋ช…๋ น์–ด๋กœ 80๋ฒˆ ํฌํŠธ -> 8080ํฌํŠธ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•˜๊ณ , ๋˜ 80๋ฒˆ ํฌํŠธ -> 8888ํฌํŠธ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•ด์„œ ์™ ์ง€ ํฌํŠธํฌ์›Œ๋”ฉ์ด 2๊ฐœ๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

     

    ์ด ํ‚ค์›Œ๋“œ๋กœ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋”๋‹ˆ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด์™€ ํฌํŠธํฌ์›Œ๋”ฉ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๊ฐ€ ์žˆ์—ˆ๋‹ค.

    ์šฐ์„  ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” ์„œ๋ฒ„๋Š” ctrl + c๋ฅผ ๋ˆŒ๋ ค์„œ ์ข…๋ฃŒ์‹œ์ผœ ์ฃผ๊ณ  ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธ ๊ทธ๋ฆฌ๊ณ  ์‚ญ์ œ๋ฅผ ๋”ฐ๋ผ ํ•ด๋ณด์ž.

     

     

    ๐Ÿ“‘ ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธ and ์‚ญ์ œ ๋ช…๋ น์–ด

    1. ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด

    sudo iptables -t nat -L --line-numbers

     

    ๋ช…๋ น์–ด๋ฅผ ์น˜๊ณ  ์—”ํ„ฐ๋ฅผ ์น˜๋ฉด ํฌํŠธํฌ์›Œ๋”ฉ ๋œ ๋ฆฌ์ŠคํŠธ๋“ค์ด ๋‚˜์˜จ๋‹ค.

    ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธ

    num 1์€ http (80๋ฒˆ)๋ฅผ 8080๋ฒˆ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•˜๊ณ  ์žˆ๊ณ , num 2๋Š” http (80๋ฒˆ)๋ฅผ 8888๋ฒˆ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค.

    ๋‚˜๋Š” 8080๋ฒˆ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ ํ•˜๋Š” ๊ฑด ํ•„์š”๊ฐ€ ์—†์œผ๋‹ˆ 8080์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ ๋œ ๊ฑธ ์‚ญ์ œํ•ด์•ผ ํ•œ๋‹ค.

     

    2. ํฌํŠธํฌ์›Œ๋”ฉ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด

    sudo iptables -t nat -D PREROUTING 1

    ๋‚˜๋Š” 1๋ฒˆ์„ ์‚ญ์ œํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— PREROUTING ๋’ค์— 1์ด๋ผ๊ณ  ์ ์–ด์ฃผ๊ณ  ์—”ํ„ฐ๋ฅผ ์ณค๋‹ค.

    ์•„๋ฌด์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

     

    3. ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด

    git bash ์—์„œ๋Š” ์•„๋ฌด์ผ๋„ ์—†์ด ๋‹ค์Œ ๋ช…๋ น์ค„๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

    ๋‹ค์‹œ ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์„œ ๋ช‡๋ฒˆ ํฌํŠธ์— ํฌํŠธํฌ์›Œ๋”ฉ์ด ๋ผ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

    8080๋ฒˆ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ ๋ผ์žˆ๋˜ ๊ฑด ์‚ญ์ œ๊ฐ€ ๋˜์—ˆ๊ณ , ํ˜„์žฌ 8888๋ฒˆ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ์ด ๋ผ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค.

    java -jar ๋ณธ์ธjarํŒŒ์ผ.jar

    ์œ„ ๋ช…๋ น์–ด๋กœ ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ ํ•œ ๋’ค ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ ๋นผ๊ณ  ์•„์ดํ”ผ์ฃผ์†Œ๋กœ๋งŒ ์ ‘์†์„ ํ•ด๋ณด๋ฉด ์›น์‚ฌ์ดํŠธ๊ฐ€ ์•„์ฃผ ์ž˜ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๋“œ๋””์–ด ํ•ด๊ฒฐ.

     

     

    โœ๐Ÿผ ํฌํŠธํฌ์›Œ๋”ฉ ๋ช…๋ น์–ด ์š”์•ฝ

    1. ํฌํŠธํฌ์›Œ๋”ฉ ํ•˜๋Š” ๋ช…๋ น์–ด

    sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

    2. ํฌํŠธํฌ์›Œ๋”ฉ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด

    sudo iptables -t nat -L --line-numbers

    3. ํฌํŠธํฌ์›Œ๋”ฉ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด

    sudo iptables -t nat -D PREROUTING 1

     

     

     

     

     

    ์ด๋กœ์จ ๊ณผ์ œ๊ฐ€ ๋์ด๋‚ฌ๋‹ค.

    ๋Œ“๊ธ€