Nginx ๋ง›๋ณด๊ธฐ

xgroยท2022๋…„ 5์›” 27์ผ
0
post-thumbnail

๐Ÿ“Œย Nginx๋ž€?

๐Ÿ‘‰ NGINX

NGINX๋Š” ์ตœ์‹  ์›น ์š”๊ตฌ๋ฅผ ์ถฉ์กฑํ•˜๋„๋ก ์„ค๊ณ„๋œ ์›น ์„œ๋ฒ„ ํ”Œ๋žซํผ ์ด๋‹ค.

์ด ์›น ์‚ฌ์ดํŠธ๋Š” ์„ฑ์žฅํ•˜๋Š” ์›น์˜ ์š”๊ตฌ์‚ฌํ•ญ์ธ '1๋งŒ ๊ฐœ์˜ ์—ฐ๊ฒฐ'์„ ์ œ๊ณตํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๋‹ต์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ๋‹ค . ๋”ฐ๋ผ์„œ ์ด ๊ธฐ์ˆ ์€ ํšจ์œจ์ ์ธ ๋ฐฉ์‹์œผ๋กœ ๋งŽ์€ ์ˆ˜์˜ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋™์‹œ์— ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ ์ค‘์ ์„ ๋‘๊ณ  ์žˆ๋‹ค. NGINX๋Š” 2004 ๋…„์— ์ผ๋ฐ˜์ธ์—๊ฒŒ ๊ณต๊ฐœ๋˜์—ˆ์œผ๋ฉฐ ๊พธ์ค€ํžˆ ์ธ๊ธฐ๋ฅผ ์–ป๊ณ  ์žˆ๋‹ค.

โœ… NGINX ํŠน์ง•

apache์™€ ๋‹ฌ๋ฆฌ NGINX๋Š” ์˜ค๋Š˜๋‚  ์›น์˜ ์š”๊ตฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค. ํšจ์œจ์„ฑ์— ์ค‘์ ์„ ๋‘๊ณ  ์žˆ์œผ๋ฉฐ ๊ฒฝ๋Ÿ‰ ์•„ํ‚คํ…์ฒ˜์™€ ๋†’์€ ๋™์‹œ์„ฑ์ด ์žˆ๋‹ค.

  • ์ด๋ฒคํŠธ ์ค‘์‹ฌ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ œ๊ณต
  • ์ œํ•œ๋œ ํ•˜๋“œ์›จ์–ด ๋ฆฌ์†Œ์Šค๋กœ๋„ ์—ฌ๋Ÿฌ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ๋™์‹œ์— ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ
  • ๋‹จ์ผ ์Šค๋ ˆ๋“œ๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์—ฐ๊ฒฐ์„ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์ตœ์†Œํ•œ์˜ ๋ฆฌ์†Œ์Šค๋กœ ์›น ์„œ๋ฒ„์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋…๋ฆฝํ˜• HTTP ์„œ๋ฒ„๋กœ ๋ฐฐ์น˜ ๊ฐ€๋Šฅ

๐Ÿ‘‰ย NGINX ํ™˜๊ฒฝํŒŒ์ผ ๊ณต์‹๋ฌธ์„œ

  • Full Example Configuration
    • nginx.confย : ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋ณธ ํ™˜๊ฒฝ ์„ค์ •
    • proxy.confย : ํ”„๋ก์‹œ ๊ด€๋ จ ํ™˜๊ฒฝ ์„ค์ •
    • fastcgi.confย : FastCGI ๊ด€๋ จ ํ™˜๊ฒฝ ์„ค์ •
    • mime.typesย : ํŒŒ์ผ ํ™•์žฅ๋ช…๊ณผ MIME ํƒ€์ž… ๋ชฉ๋ก

  • Another Full Example
    • nginx.conf

      [์ฐธ๊ณ ] sites.conf : NGINX์— ์˜ํ•ด ์„œ๋น„์Šค๋˜๋Š” ๊ฐ€์ƒ ํ˜ธ์ŠคํŠธ ์›น์‚ฌ์ดํŠธ ํ™˜๊ฒฝ ์„ค์ •. ๋„๋ฉ”์ธ๋งˆ๋‹ค ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•ด์„œ ๋งŒ๋“ค ๊ฒƒ์„ ๊ถŒ์žฅ

      ์ถœ์ฒ˜ - https://prohannah.tistory.com/136[Hello, Hannah!]


๐Ÿ‘‰ย GETTING START

โœ…ย Try environment

  • Homebrew: 3.4.11

  • nginx version: nginx/1.21.6

  • macOS

   	$ brew install nginx

โœ…ย Quick start

  • Start NGINX
    $ nginx
  • Controlling NGINX
    $ nginx -s <SIGNAL>
    • quitย โ€“ Shut down gracefully (theย SIGQUITย signal)
    • reloadย โ€“ Reload the configuration file (theย SIGHUPย signal)
    • reopenย โ€“ Reopen log files (theย SIGUSR1ย signal)
    • stopย โ€“ Shut down immediately (or fast shutdown, theย SIGTERMย singal)

โœ…ย Configuration

  • nginx.conf ์œ„์น˜

    brew๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์น˜ํ•  ๊ฒฝ์šฐ ์„ค์ •์„ ์œ„ํ•œ nginx.conf ํŒŒ์ผ์€ /opt/homebrew/etc/nginx์— ์ƒ์„ฑ๋œ๋‹ค.

    $ nano /opt/homebrew/etc/nginx/nginx.conf
  • ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
    server {
            listen 80;          # IPv4 ํฌํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค
            listen [::]:80;     # IPv6 ํฌํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค
    
            server_name ๋„๋ฉ”์ธ;   # ๋„๋ฉ”์ธ ์ง€์ •ํ•œ๋‹ค. "_"๊ฐ’ ์‚ฌ์šฉ์‹œ localhost
    
            root /var/www/์œ„์น˜;  # ๊ธฐ๋ณธ ์š”์ฒญ์— ๋Œ€ํ•ด ๋ฐ˜ํ™˜ํ•  ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•œ๋‹ค
            index index.html;   # ์œ„ ๊ฒฝ๋กœ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ์„ ์ง€์ •
    				
    				# location์„ ํ†ตํ•ด ๋ฉ”์ธ ๋„๋ฉ”์ธ์—์„œ ๋ถ„๊ธฐ์ ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
            location / {
                    try_files $uri $uri/ =404;
            }
    }
  • ์ฝ”๋“œ ๋ธ”๋Ÿญ ์ž‘์„ฑ
    • server {}ย :
      ๋„๋ฉ”์ธ ๋‹จ์œ„์˜ 1์ฐจ ๋ผ์šฐํŒ… (https://localhost์ด ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.)
    • location {}ย :
      ๋„๋ฉ”์ธ ๋‚ด๋ถ€์˜ 2์ฐจ ๋ผ์šฐํŒ… (https://localhost/location์ด location ๋ผ์šฐํŒ…์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.)
    • server ๋ธ”๋Ÿญ ๋‚ด ๊ตฌ๋ฌธ๋“ค
      • server_name: server ๋ธ”๋ก์—์„œ ์–ด๋–ค ๋„๋ฉ”์ธ์„ ๋ผ์šฐํŒ… ํ•  ๊ฒƒ์ธ๊ฐ€๊ฐ€ ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
      • access_logย /ย error_log: ์„œ๋ฒ„์— ๋Œ€ํ•œ ๋กœ๊ทธ๋ฅผ ๋‚จ๊น๋‹ˆ๋‹ค.
      • root: server_name์— ํ•ด๋‹นํ•˜๋Š” ๋„๋ฉ”์ธ์ด ์žˆ์„ ๋•Œ, root ํด๋”๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. reverse forwading์˜ ๊ฒฝ์šฐ ์ƒ๋žต๋ฉ๋‹ˆ๋‹ค.
      • index: ์–ด๋–ค ํŒŒ์ผ์„ index ํŒŒ์ผ๋กœ ์ง€์ •ํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. reverse forwading์˜ ๊ฒฝ์šฐ ์ƒ๋žต๋ฉ๋‹ˆ๋‹ค.
    • location ๋ธ”๋Ÿญ ๋‚ด ๊ตฌ๋ฌธ๋“ค
      • try_files $uri $uri/ =404;: nginx๋Š” ์ •์  ํŒŒ์ผ ํ˜ธ์ŠคํŒ…์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ์—, root ํด๋” ๋‚ด์— uri ์— ๋”ฐ๋ฅธ ํด๋”๊ฐ€ ์žˆ๋Š”์ง€ ์ฐพ์•„๋ณด๊ณ , ๋งŒ์•ฝ ์—†๋‹ค๋ฉด 404 ์—๋Ÿฌ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
      • return: ์œ„ ์ฝ”๋“œ์—๋Š” ์ ์šฉ๋˜์–ด ์žˆ์ง€ ์•Š์ง€๋งŒ, http ๋ฉ”์†Œ๋“œ๋กœ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. return 301 https://google.comย ์˜ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค๋ฉด, googlo.com์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋œ๋‹ค.
profile
์•ˆ๋…•ํ•˜์„ธ์š”!

0๊ฐœ์˜ ๋Œ“๊ธ€