Bibi's DevLog ๐Ÿค“๐ŸŽ

[Nginx] Nginx๋ฅผ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ํฌํŠธ๋กœ HTTP Request ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/AWS

[Nginx] Nginx๋ฅผ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ํฌํŠธ๋กœ HTTP Request ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๊ธฐ

๋น„๋น„ bibi 2021. 6. 2. 23:51

ํ”„๋กœ์ ํŠธ - Nginx๋ฅผ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ํฌํŠธ๋กœ HTTP Request ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๊ธฐ

์ถœ์ฒ˜๐Ÿ™‡โ€โ™‚๏ธ

https://velog.io/@damiano1027/Nginx-Nginx%EC%99%80-SpringBoot-%EB%82%B4%EC%9E%A5-Tomcat-%EC%97%B0%EB%8F%99

์—์–ด๋น„์—”๋น„ ํ”„๋กœ์ ํŠธ์—์„œ 3000ํฌํŠธ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ํ”„๋ก ํŠธ ๋ถ„๋“ค์„ ์œ„ํ•ด Nginx๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŒ…์„ ํ•ด ๋ณด์•˜๋‹ค.

(์œ„ ๋งํฌ๋ฅผ ์ฝ๊ณ  ๋”ฐ๋ผํ–ˆ๋‹ค - ์ž์„ธํ•œ ์„ค๋ช…์€ ์œ„ ๋งํฌ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ด์•ผ๊ฒ ๋‹ค)

  1. (ec2) Nginx ์„ค์น˜ ๋ฐ ์‹œ์ž‘

    sudo apt-get install nginx
    sudo service nginx start
  2. (ec2) ec2 ์ธ์Šคํ„ด์Šค [๋ณด์•ˆ] - [๋ณด์•ˆ๊ทธ๋ฃน] - [์ธ๋ฐ”์šด๋“œ ์„ค์ •]์—์„œ ์›ํ•˜๋Š” ํฌํŠธ ์—ด์–ด์ฃผ๊ธฐ

  3. (ec2) ์„ค์ •ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์ ์šฉ

    cd /etc/nginx/sites-available
    sudo vi test.conf

    ๋งŒ๋“ค์–ด์ง„ test.conf์— ์•„๋ž˜ ๋‚ด์šฉ ๋„ฃ๊ธฐ

    server {
        listen 3000;
        listen [::]:3000;
    
        server_name ec2ํผ๋ธ”๋ฆญIPv4DNS์ฃผ์†Œ ;
    
        location / {
             proxy_pass http://ec2ํผ๋ธ”๋ฆญIPv4:8080;
             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;
        }
    }
    • 3000๋ฒˆ ํฌํŠธ๋กœ ec2์ธ์Šคํ„ด์Šค์— ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  request์— ๋Œ€ํ•ด, 8080ํฌํŠธ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๋‚ด์šฉ์ด๋‹ค.
  4. (ec2) sites-enabled์— ์‹ฌ๋ณผ๋ฆญ ๋งํฌ ๋งŒ๋“ค๊ธฐ

    • ์œ„ ์„ค์ •ํŒŒ์ผ ์ €์žฅ ํ›„
    • sudo ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled
    • cd /etc/nginx/sites-enabled
    • ls -l
    • test.conf ํŒŒ์ผ(์‹ฌ๋ณผ๋ฆญ ๋งํฌ)์ด ์ถ”๊ฐ€๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    • default ๋„ ์กด์žฌํ•  ๊ฒƒ์ด๋‹ค. ์ด ํŒŒ์ผ์€ ์‚ญ์ œํ•ด์•ผ ํ•œ๋‹ค. (rm -rf default ๋˜๋Š” sudo rm -rf default)
  5. (ec2) Ngnix ์žฌ์‹œ์ž‘ ๋ฐ ํ™•์ธ

    • sudo service nginx reload ๋˜๋Š” sudo service nginx restart๋กœ Nginx๋ฅผ ์žฌ์‹œ์ž‘ํ•œ๋‹ค
    • ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ•ด๋‹น ec2์„œ๋ฒ„์˜ 3000๋ฒˆ ํฌํŠธ๋กœ ์ ‘์†ํ•ด ๋ณธ๋‹ค.
    • 8080ํฌํŠธ๋กœ ์ ‘์†ํ•  ๋•Œ์™€ ๋™์ผํ•œ ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜จ๋‹ค๋ฉด ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์„ฑ๊ณต!