Skip to content

๐Ÿš€ ๋„์ปค์™€ nginx์˜ ์‚ฌ์šฉ๊ธฐ

Kang Chaeryeon edited this page Nov 18, 2024 · 2 revisions

๊ฐœ์š”

๋กœ์ปฌ์—์„œ ๋„์ปค๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ  ์—ฌ๋Ÿฌ๋ฐฉ๋ฉด์—์„œ ๋””๋ฒ„๊น…์„ ์ง„ํ–‰ํ•˜๋ ค๋‹ค๋ณด๋‹ˆ ๋‚ด๊ฐ€ ์•„์ง ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋“ค์˜ ํŠธ๋ž˜ํ”ฝ์„ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” nginx์˜ ์ •ํ™•ํ•œ ํ๋ฆ„์„ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ์„œ์นญํ•˜๋‹ค๋ณด๋‹ˆ ์กฐ๊ธˆ์€ ํ๋ฆ„์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์ •๋ฆฌ์ฐจ ๋ฌธ์„œ๋กœ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ๋‹ค

์ผ๋‹จ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์˜ ํฐ ์ปจํ…Œ์ด๋„ˆ๋Š” ์ด 3๊ฐœ์ด๋‹ค

  • server container: ๋ฐฑ์—”๋“œ ์„œ๋ฒ„
  • client contianer: ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์„œ๋น™์„ ์ง„ํ–‰ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ
  • nginx container: ํŠธ๋ž˜ํ”ฝ์„ ์•Œ๋งž๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ
  • redis container: ๋กœ์ปฌ์—์„œ๋Š” ํ•„์š”ํ•œ๋ฐ ncloud ์œ„์—์„œ๋Š” ์ด๋ฏธ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ VPC ์•ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ์Œ์œผ๋กœ ๋ณ„๋„๋กœ ํ•„์š”ํ•˜์ง€๋Š” ์•Š๋‹ค

Docker ๋‚ด๋ถ€์—์„œ ํŠธ๋ž˜ํ”ฝ ๊ด€๋ฆฌ ์…‹์—…

์šฐ์„  ์•Œ๋ฉด ์ข‹์€๊ฒŒ ๋ฐ”๋กœ ๋‹ค์ค‘ Docker container๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” docker compose ๋‚ด๋ถ€์—์„œ ๋„คํŠธ์›Œํฌ ํŠธ๋ž˜ํ”ฝ์„ ๊ด€๋ฆฌ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค

์šฐ์„  docker compose๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด

services:
  nginx:
    ports:
      - "80:80"  # ์™ธ๋ถ€ ๋…ธ์ถœ
  
  backend:
    expose:
      - "3000"   # ๋‚ด๋ถ€ ๋„คํŠธ์›Œํฌ์—๋งŒ ๋…ธ์ถœ
  
  database:
    expose:
      - "5432"   # ๋‚ด๋ถ€ ๋„คํŠธ์›Œํฌ์—๋งŒ ๋…ธ์ถœ

networks:
  default:       # ์ž๋™์œผ๋กœ project-name_default ๋„คํŠธ์›Œํฌ ์ƒ์„ฑ
    driver: bridge

์š”๋Ÿฐ์‹์œผ๋กœ ๊ฐ ํ”„๋กœ์ ํŠธ๋งˆ๋‹ค ํ•˜๋‚˜์˜ compose ํŒŒ์ผ์•ˆ์—์„œ ์ปจํ…Œ์ด๋„ˆ๋ผ๋ฆฌ์˜ ํ†ต์‹  ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๋ช…์‹œํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค

๊ฐ€์žฅ ๋ˆˆ์—ฌ๊ฒจ๋ด์•ผํ•˜๋Š” ๊ฒƒ์€ ports์™€ expose, ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์˜ ์—ญํ• ์ด๋‹ค

  • ports: ํ˜ธ์ŠคํŠธ ๋จธ์‹ (์™ธ๋ถ€)์˜ 80ํฌํŠธ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ 80ํฌํŠธ๋กœ ํฌ์›Œ๋”ฉํ•œ๋‹ค๋Š” ์˜๋ฏธ
graph LR
    External[์™ธ๋ถ€]
    Host[ํ˜ธ์ŠคํŠธ ๋จธ์‹ <br/>localhost:80]
    Container[Nginx ์ปจํ…Œ์ด๋„ˆ<br/>๋‚ด๋ถ€ ํฌํŠธ 80]

    External -->|"domain.com:80"| Host
    Host -->|ํฌ์›Œ๋”ฉ| Container

    style Host fill:#f9f,stroke:#333
    style Container fill:#bbf,stroke:#333
Loading
  • expose: ๋„์ปค ๋„คํŠธ์›Œํฌ ๋‚ด๋ถ€์—์„œ ๊ฐ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋…ธ์ถœํ•˜๋Š” ํฌํŠธ ๋ฒˆํ˜ธ. ํ•ด๋‹น ํฌํŠธ ๋ฒˆํ˜ธ์™€ ๊ฐ ์ปจํ…Œ์ด๋„ˆ์˜ ์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์†Œํ†ต์„ ํ•œ๋‹ค
  • ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„: ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ ์ž์ฒด๊ฐ€ ์ž๋™์œผ๋กœ DNS ์ด๋ฆ„์ด ๋˜๊ธฐ์— ํ•ด๋‹น ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค

์ด๊ฐ™์€ ๋„์ปค์˜ ํŠน์„ฑ์„ ์ž˜ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ nginx ์„ค์ •์ด๋‹ค


nginx์˜ ํ”„๋ก์‹œ ํŒจ์Šค ์„ค์ •์„ ํ†ตํ•œ ํŠธ๋ž˜ํ”ฝ ๊ด€๋ฆฌ

์šฐ๋ฆฌ ํŒ€์—์„œ ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” nginx๋‹ค

upstream backend {
    server server:3000;
}

server {
    listen 80;
    
    # ๊ณตํ†ต ํ—ค๋” ์„ค์ •
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    location / {
        proxy_pass http://client:5173;
    }

    location /api {
        proxy_pass http://backend;
    }
}

์ด์ค‘ ์ค‘์š”ํ•˜๊ฒŒ ๋ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์€ ํŠธ๋ž˜ํ”ฝ ๊ด€๋ฆฌ์—์„œ๋Š” ์—ญ์‹œ๋‚˜ proxy_pass๋‹ค

  • ์•„๊นŒ ๋งํ–ˆ๋“ฏ์ด ์ผ๋‹จ ๊ธฐ๋ณธ์ ์œผ๋กœ 80์—์„œ ์™ธ๋ถ€ ๋ธŒ๋ผ์šฐ์ €์˜ ์š”์ฒญ์„ ๋ฐ›์•„์˜จ๋‹ค.
  • ๊ทธ ์ดํ›„ ๊ฐ๊ฐ์˜ ์š”์ฒญ์— ๊ด€ํ•˜์—ฌ location์—์„œ ์ง€์ •ํ•œ ํŒจํ„ด๊ณผ ๋งž๋Š” ์š”์ฒญ์„ ์•ˆ์—๋‹ค๊ฐ€ ์ •์˜ํ•œ DNS+ํฌํŠธ ์—๋‹ค๊ฐ€ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ์„ ํ•˜๋Š” ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค
  • ์˜ˆ๋ฅผ ๋“ค์–ด ์š”์ฒญ๋ฌธ์— http://mydomain.com/api/users ์ด๋Ÿฐ ์‹์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์€ proxy_pass๋ฅผ ํ†ตํ•˜์—ฌ http://server:3000์œผ๋กœ ์ „๋‹ฌ์ด ๋œ๋‹ค โ†’ ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด container name ์ž์ฒด๊ฐ€ DNS๊ฐ€ ๋˜๋Š”๊ฒŒ ์—ฌ๊ธฐ์„œ ๋‚˜์˜จ๋‹ค!!!!!

์š”์ฒญ์„ ๋ฐ›๊ณ  ํ๋ฆ„์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •

์šฐ์„  ๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญ์„ ๋ณด๋‚ผ๋•Œ์—๋Š” ํฌ๊ฒŒ IP ์ฃผ์†Œ์™€ ํฌํŠธ ๋„˜๋ฒ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค

  • IP ์ฃผ์†Œ๋Š” ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ๋ฐ
    • ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ผ ๊ฒฝ์šฐ: localhost๋กœ ๋””ํดํŠธ ์„ค์ •
    • ๋ฐฐํฌ ํ™˜๊ฒฝ์ผ ๊ฒฝ์šฐ: cloud instance์˜ public ip๋กœ ์„ค์ •
  • ํฌํŠธ ๋„˜๋ฒ„ ๋˜ํ•œ ์š”์ฒญ์— ๋”ฐ๋ผ ๋””ํดํŠธ๋กœ ์„ค์ •์ด ๋˜์–ด์žˆ๋‹ค (๋ธŒ๋ผ์šฐ์ €์— ์ž…๋ ฅ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋จ)
    • http: 80
    • https: 443

ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ผ์‹œ, docker ํ™˜๊ฒฝ์—์„œ๋Š” ์ด๋ฏธ ports 80:80 ์„ค์ •์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ˜ธ์ŠคํŠธ๋จธ์‹ ์˜ 80 ํฌํŠธ์™€ nginx์˜ 80 ํฌํŠธ๊ฐ€ ๋™์ผ์‹œ ๋˜์–ด์žˆ๋‹ค

์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์€ default.d์—์„œ ์„ค์ •ํ•œ proxy_pass์— ์•Œ๋งž๊ฒŒ ๋ฐฐ๋ถ„์ด ๋˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค

graph TD
    Browser[์™ธ๋ถ€ ๋ธŒ๋ผ์šฐ์ €]
    
    subgraph "ํ˜ธ์ŠคํŠธ ๋จธ์‹  (EC2/๋‚ด ์ปดํ“จํ„ฐ)"
        HostPort[ํ˜ธ์ŠคํŠธ ํฌํŠธ:80]
        
        subgraph "Docker Network"
            subgraph "์™ธ๋ถ€์—์„œ๋Š” 80ํฌํŠธ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ"
                Nginx[Nginx:80]
            end
            
            subgraph "๋‚ด๋ถ€ ๋„คํŠธ์›Œํฌ"
                Client[Client:5173]
                Backend[Backend:3000]
            end
            
            Nginx -->|5173| Client
            Nginx -->|3000| Backend
        end
    end
    
    Browser -->|"80<br/>(localhost ๋˜๋Š” Public IP)"| HostPort
    HostPort -->|ํฌํŠธ ํฌ์›Œ๋”ฉ| Nginx
    
    classDef external fill:#f96;
    classDef host fill:#f9f;
    class Browser external;
    class HostPort host;
Loading

inear

๊ธฐ์ˆ  ๊ณต์œ 

๐Ÿš€ ffmpeg๋Š” stderr๋กœ ๋””๋ฒ„๊น…์„ ํ•˜๋Š” ์ด์œ 
๐Ÿš€ HLS ํ”„๋กœํ† ์ฝœ์— ๊ด€ํ•œ ์ •๋ฆฌ ๋ฐ FFmpeg ์‚ฌ์šฉ๊ธฐ
๐Ÿš€ ๋น„ํŠธ๋Š” tsconfig.json์ด ์„ธ ๊ฐœ?
๐Ÿš€ NestJS ๊ธฐ๋ณธ ๊ฐœ๋… - Modules
๐Ÿš€ Socket.io ์ตœ(๊ฐ•)์ ํ™”
๐Ÿš€ ๋„์ปค์™€ nginx์˜ ์‚ฌ์šฉ๊ธฐ
๐Ÿš€ ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์ž

๊ฐœ๋ฐœ ์ผ์ง€

๐Ÿš€ FSD ์‚ฌ์šฉ๊ธฐ, ๊ทผ๋ฐ ์ด์ œ ๋‚˜๋งŒ์˜ ๊ทœ์น™์„ ๊ณ๋“ค์ธ
๐Ÿš€ CICD ๊ตฌ์กฐ ์ˆ˜์ •
๐Ÿš€ ์•จ๋ฒ” ๋‹จ์œ„๋กœ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•˜๊ธฐ (with HLS)
๐Ÿš€ HLS๋กœ ์Œ์•… ์ฃผ๊ณ ๋ฐ›๊ธฐ
๐Ÿš€ vite + react + typescript ํ™˜๊ฒฝ์—์„œ path alias ์„ค์ •
๐Ÿš€ React Scan์ด ๋ญ์ฃ ?
๐Ÿš€ ๋กœ์ปฌ ํ™˜๊ฒฝ ๊ฐœ๋ฐœ ๋ชจ๋“œ ๋ฐฐํฌ
๐Ÿš€ ์•จ๋ฒ” ์ „์ฒด๋ฅผ ์ŠคํŠธ๋ฆฌ๋ฐํ•œ๋‹ค๊ณ ? (with HLS)
๐Ÿš€ ์ฝ”๋“œ์˜ ์•ˆ์ •์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž

ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

๐Ÿš€ ์ƒˆ๋กœ๊ณ ์นจ ์‹œ HLS ERROR
๐Ÿš€ input ํƒœ๊ทธ์— ํ•œ๊ธ€ ์ž…๋ ฅ ํ›„, Enter๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋‘๋ฒˆ ํ˜ธ์ถœ๋˜๋Š” ์˜ค๋ฅ˜
๐Ÿš€ nginx proxy pass๋ฅผ ๋ฐ”๊ฟจ๋”๋‹ˆ ์ƒ๊ธด ์—๋Ÿฌ - ์Šค์›จ๊ฑฐ ์ธ์‹ ๋ฌธ์ œ
๐Ÿš€ ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ WS handshake
๐Ÿš€ ๋ Œ๋”๋ง ๋ฒ”์ธ์€ ํ•˜๋‚˜!

ํ˜‘์—… ๊ทœ์น™

๐ŸŒˆ ๊ทธ๋ผ์šด๋“œ ๋ฃฐ
๐Ÿฅ” ํŒ€์› ์†Œ๊ฐœ
๐Ÿ”Ž ์ฝ”๋“œ & ๊นƒ ์ปจ๋ฒค์…˜
๐ŸŒณ ๊นƒ branch ์ „๋žต
๐Ÿ“Œ ๋…ธ์…˜ ๋ฌธ์„œ ์ €์žฅ์†Œ

ํ”„๋กœ์ ํŠธ ๊ธฐํš

๐ŸŽจ ํ”ผ๊ทธ๋งˆ
๐Ÿง‘โ€๐Ÿ’ป ๊ธฐํš ๊ณต์œ  ๋ฐœํ‘œ ์ž๋ฃŒ
๐ŸŽค 2์ฃผ์ฐจ ๋ฐœํ‘œ ์ž๋ฃŒ
๐Ÿ˜Ž ๋ฐฑ๋กœ๊ทธ

๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ

๐Ÿ“ 1์ฃผ์ฐจ
๐Ÿ“ 2์ฃผ์ฐจ
๐Ÿ“ 3์ฃผ์ฐจ
๐Ÿ“ 4์ฃผ์ฐจ
๐Ÿ“ 5์ฃผ์ฐจ

์ฃผ๊ฐ„ ๊ณ„ํš์„œ

๐Ÿ—“๏ธ 1์ฃผ์ฐจ
๐Ÿ—“๏ธ 2์ฃผ์ฐจ
๐Ÿ—“๏ธ 3์ฃผ์ฐจ
๐Ÿ—“๏ธ 4์ฃผ์ฐจ
๐Ÿ—“๏ธ 5์ฃผ์ฐจ

๊ทธ๋ฃน ํšŒ๊ณ 

โœจ 1์ฃผ์ฐจ
โœจ 2์ฃผ์ฐจ
โœจ 3์ฃผ์ฐจ
โœจ 4์ฃผ์ฐจ


view

Clone this wiki locally