Dango + uWSGI + Nginx


๐Ÿ’ก uWSGI

Web์„œ๋ฒ„๊ฐ€ ๋ฐ›์€ ํ˜ธ์ถœ์„ Python ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—๊ฒŒ ์ „๋‹ฌํ•˜๊ณ  ์‘๋‹ต๋ฐ›๊ธฐ ์œ„ํ•œ ํ˜ธ์ถœ์กฐ์•ฝ(Calling Convention)

uWSGI <-> Django

  • uWSGI.ini (์„ค์ •ํŒŒ์ผ)
[uwsgi]

# ์žฅ๊ณ  ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ
chdir = /Users/giljun/gilbert/drf_todo

# wsgi ๋ชจ๋“ˆ ๊ฒฝ๋กœ
module = drf_todo.wsgi:application

# ๊ฐ€์ƒํ™˜๊ฒฝ ๊ฒฝ๋กœ
home = /Users/giljun/opt/anaconda3/envs/django

# 
env = DJANGO_SETTINGS_MODULE=drf_todo.settings

# pid๋ฅผ ์ฃฝ์—ฌ๋„ worker๋ฅผ ์‚ด๋ ค์ค€๋‹ค.
master = True

# 
enable-threads = True

# ์ตœ๋Œ€ ์š”์ฒญ
max-requests=5000

# ๋ฒ„ํผ ํฌ๊ธฐ 
buffer-size=32768


chmod-socket = 666

# ์†Œ์ผ“
socket = /tmp/drf_todo.sock

# ๋กœ๊ทธ 
logger = file:/tmp/uwsgi.log

# ํ”„๋กœ์„ธ์Šค ์•„์ด๋”” ๋ฒˆํ˜ธ๊ฐ€ ์ ํž˜
pidfile = /tmp/drf_todo.pid

# ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์†Œ์ผ“, ๋กœ๊ทธ, pid๋ฅผ ์‚ญ์ œํ•ด์ค€๋‹ค.
vacuum=True

๋” ๋งŽ์€ ์„ค์ •๋“ค์ด ์žˆ๋Š”๋ฐ ํ•„์š”ํ•œ ์„ค์ •์ด ์žˆ์„ ๋•Œ ์ฐพ์•„๋ณด๋ฉด์„œ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.

์‹คํ–‰ ๋ช…๋ น์–ด๋Š” uwsgi --ini uwsgi.ini ์ด๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ โ€“http : ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ํฌํŠธ์—์„œ ํ™”๋ฉด์ด ์ถœ๋ ฅ๋๋‹ค.

๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด /tmp ๊ฒฝ๋กœ๋กœ ๋“ค์–ด๊ฐ€์„œ tail -f uwsgi.log ๋ผ๊ณ  ์ž…๋ ฅํ•˜๋‹ˆ ๋๋‹ค.



๐Ÿ’ก Nginx

์ด ๋ถ€๋ถ„ ์„ค์ •์ด ๊ต‰์žฅํžˆ ํž˜๋“ค์—ˆ๋‹ค. ์•Œ๊ณ  ์„ค์ •ํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ๋ญ”๊ฐ€ ๋ผ์›Œ ๋งž์ถ˜ ๋Š๋‚Œ..?

Nginx <-socket-> [ uWSGI <-> Django ]


  • drf_todo.conf

๋‚ด๊ฐ€ ๋งŒ๋“  django ํ”„๋กœ์ ํŠธ์˜ nginx ์„ค์ • ํŒŒ์ผ์ด๋‹ค.

sites-available, sites-enabled ํด๋”๋Š” ์—†์–ด์„œ ์ง์ ‘ ๋งŒ๋“ค์—ˆ๊ณ , ๊ฐ๊ฐ์˜ ํด๋”์— drf_todo.conf๋ฅผ

์ž‘์„ฑํ•ด์„œ ์ €์žฅํ•ด๋’€๋‹ค. ์‹ฌ๋ณผ๋ฆญ๋งํฌ๋ฅผ ํ•ด์ค€๋‹ค๋Š”๋ฐ ์ด๋ถ€๋ถ„์€ ๊ณต๋ถ€๋ฅผ ๋” ํ•˜๋ฉด์„œ ์ด์œ ๋ฅผ ์•Œ์•„๋ด์•ผ๊ฒ ๋‹ค.

์œ„์น˜ : /opt/homebrew/etc/nginx/sites-enabled

# upstream(proxy) ์„ค์ •
upstream django {

    # Nginx์™€ uWSGI๋ฅผ ์ด์–ด์ฃผ๋Š” ์†Œ์ผ“.
    server unix:///tmp/drf_todo.sock;

}

# configuration of the server
server {

    # client๊ฐ€ ๋“ค์–ด์˜ค๋Š” port
    listen      8001;
    
    # client๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์„œ๋ฒ„์˜ IP์ฃผ์†Œ or Domain
    server_name localhost;

    charset     utf-8;

    # log settings.
    access_log  /opt/homebrew/Cellar/nginx/1.21.4/log/access.log;
    error_log   /opt/homebrew/Cellar/nginx/1.21.4/log/error.log;

    # max upload size
    client_max_body_size 75M;

    # Django media ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ (alias๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์— / ๋„ฃ๊ธฐ.)
    location media/  {
        alias /Users/giljun/gilbet/drf_todo/media/;
    }

    # Django static ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ
    location static/ {
        alias /Users/giljun/gilbet/drf_todo/static/;
    }

    # media์™€ static์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์š”์ฒญ์„ upstream์œผ๋กœ ๋ณด๋‚ธ๋‹ค.
    location / {

        # uwsgi_pass (์œ„์— upstream์œผ๋กœ ์„ค์ •ํ•œ block์˜ ์ด๋ฆ„)
        uwsgi_pass  django;

        # uwsgi_params์˜ ๊ฒฝ๋กœ
        include uwsgi_params;
    }
}

์œ„์— ์ •์˜ํ•œ ํŒŒ์ผ์€ ๋‚ด ํ”„๋กœ์ ํŠธ์˜ nginx ์„œ๋ฒ„์— ๋Œ€ํ•œ ์„ค์ •์„ ํ•ด์ค€ ํŒŒ์ผ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด์ œ ์ด ์„ค์ • ํŒŒ์ผ (drf_todo.conf)์„ nginx๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ

๋งˆ์ € ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. (์ด ๋ถ€๋ถ„์—์„œ ์‹œ๊ฐ„์„ ๋งŽ์ด ํ—ˆ๋น„ํ–ˆ๋‹ค.)


  • niginx.conf

nginx๋ฅผ ๋‹ค์šด๋ฐ›์œผ๋ฉด ์ƒ๊ธฐ๋Š” default ํŒŒ์ผ.

์œ„์น˜ : /opt/homebrew/etc/nginx/nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {

    โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹ ํ”„๋กœ์ ํŠธ์˜ nginx์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•œ๋‹ค. โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹
    include       /opt/homebrew/etc/nginx/sites-enabled/drf_todo.conf;
    โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹โ‹
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

    }
}

์ด๋ ‡๊ฒŒ ์„ค์ •์„ ๋‹คํ•˜๊ณ  itersm์—๋‹ค๊ฐ€ nginx๋ฅผ ์‹œ์ž‘ํ•ด์ฃผ๊ณ , uwsgi๋„ ์‹œ์ž‘ํ•ด์คฌ๋‹ค.

nginx

uwsgi โ€“ini uwsgi.ini

๊ทธ๋ฆฌ๊ณ  localhos:8001 ๋กœ ๋“ค์–ด๊ฐ€๋‹ˆ ์—ฐ๋™์ด ์ž˜๋œ๋“ฏ ๋ณด์˜€๋‹ค.


  • ) ์ฒ˜์Œ์— static ํŒŒ์ผ ์œ„์น˜ ์„ค์ •์œผ๋กœ ์‹œ๊ฐ„์ด ์ซŒ ๊ฑธ๋ ธ๋Š”๋ฐ

์˜ˆ๋ฅผ ๋“ค์–ด location /static/ { root /var/www/app/static; }์˜ ๊ฒฝ์šฐ

๊ฒฝ๋กœ๋Š” var/www/app/static/static์ด ๋œ๋‹ค.

๋ฐ˜๋ฉด, location /static/ { alias /var/www/app/static/;}์˜ ๊ฒฝ์šฐ

๊ฒฝ๋กœ๋Š” var/www/app/static/์ด ๋œ๋‹ค.



๐Ÿ“š Reference