๐Ÿค”WSGI๋Š” ๋ฌด์—‡์ผ๊นŒ?

Han07ยท2021๋…„ 6์›” 3์ผ
1
post-thumbnail

startproject๋ฅผ ์ด์šฉํ•ด ์ƒˆ๋กœ์šด django ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

(์ด๋ฏธ์ง€ ์ถœ์ฒ˜ - django ๊ณต์‹ ํŠœํ† ๋ฆฌ์–ผ)

์˜ค๋Š˜ ์•Œ์•„๋ณผ ๋‚ด์šฉ์€ ์ƒ์„ฑ๋œ ํŒŒ์ผ ์ค‘ wsgi.py์˜ ์—ญํ• ์ด๋‹ค.

1. WSGI?

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.

WSGI ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š” ๋‚ด์šฉ์ด๋‹ค.

WSGI๋Š” python application(python script)์ด ์›น ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ, ํŒŒ์ด์ฌ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.
ํ”„๋กœํ† ์ฝœ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์›น์„œ๋ฒ„์™€ ํŒŒ์ด์ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•œ ๊ทœ์•ฝ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

Nginx, Apache์™€ ๊ฐ™์€ ์„œ๋ฒ„๋“ค์€ django, flask๋กœ ์ž‘์„ฑํ•œ web application์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์ด ๋•Œ WSGI๋ฅผ ์ด์šฉํ•ด ์›น ์„œ๋ฒ„์™€ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. (์™ธ๊ตญ์ธ๊ณผ ๋‚˜ ์‚ฌ์ด์— ๋ฒˆ์—ญ๊ธฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค)

2. WSGI Server(Middleware)

์›น ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋ฉด ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋‹ค.ํ•˜์ง€๋งŒ WSGI ์ž์ฒด๋Š” ์„œ๋ฒ„๋‚˜ ํ”„๋ ˆ์ž„์›Œํฌ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์„œ๋ฒ„๊ฐ€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ํ†ต์‹ ํ•˜๋Š” ๋ช…์„ธ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๊ฐ€ ์ด ๊ธฐ๋Šฅ๋“ค์„ ๋งค๋ฒˆ ๊ฐœ๋ฐœํ•ด์•ผ ํ•œ๋‹ค.
(์ฟ ํ‚ค, ์„ธ์…˜, ์ธ์ฆ, ๋ผ์šฐํŒ… ๋“ฑ)

์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ๋“ฑ์„ ๊ตฌํ˜„ํ•ด ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ ๊ฒƒ์ด WSGI Middleware์ด๋‹ค.

WSGI Middleware๋Š” Web Application ์˜ ์‹คํ–‰ ์ „๊ณผ ํ›„์— ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ , ๊ทธ ์ž์ฒด๋กœ๋„ WSGI Application ์ด๋‹ค.

๋Œ€ํ‘œ์ ์ธ WSGI ์„œ๋ฒ„๋กœ๋Š” Gunicorn, uWSGI, Werkzeug, mod_wsgie๋“ฑ์ด ์žˆ๋‹ค.

3. Django์˜ wsgi.py

์ด์ œ django project์˜ wsgi.py๊ฐ€ ์–ด๋–ค ์ผ์„ ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.

wsgi.py๋Š” django project์™€ web server๋ฅผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค. ์ด ํŒŒ์ผ์€ ๋กœ์ปฌ์—์„œ ํ…Œ์ŠคํŠธํ•  ๋•Œ ์“ฐ์ด๋Š” ๊ฐœ๋ฐœ ์„œ๋ฒ„(runserver)์™€ ํ”„๋กœ๋•์…˜ WSGI ๋ฐฐํฌ์—์„œ ๋ชจ๋‘ ์ด์šฉ๋œ๋‹ค.

wsgi์„œ๋ฒ„๋Š” ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฒฝ๋กœ๋ฅผ ์–ป๋Š”๋‹ค. django์˜ default server(runserver)๋Š” ์ด ๊ฒฝ๋กœ๋ฅผ settings.py์— ์žˆ๋Š” WSGI_APPLICATION์—์„œ ์–ป๋Š”๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์ด ๊ฒฝ๋กœ๋Š” wsgi.py์—์„œ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ”„๋กœ์ ํŠธ๋ช….application.wsgi๋กœ ๋˜์–ด์žˆ๋‹ค.

WSGI ์„œ๋ฒ„๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋กœ๋“œ ํ•  ๋•Œ Django๋Š” ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ •์˜ ๋œ ์„ค์ • ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค. django๋Š” ์ด ์„ค์ • ๋ชจ๋“ˆ์„ DJANGO_SETTINGS_MODULE๋ฅผ ์ด์šฉํ•ด ๊ฐ€์ ธ์˜จ๋‹ค.

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application = get_wsgi_application()

๊ธฐ๋ณธ์ ์œผ๋กœ DJANGO_SETTINGS_MODULE์˜ ์„ค์ •์„ ๋”ฐ๋ฅด์ง€๋งŒ, ๊ฐœ๋ฐœ์ด๋‚˜ ๋ฐฐํฌ๋ฅผ ํ•  ๋•Œ ๋‹ค๋ฅธ ์„ค์ •์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

django app์„ ๋ฐฐํฌํ•  ๋•Œ ์šฐ๋ฆฌ๋Š” wsgi.pyํŒŒ์ผ์„ ์ด์šฉํ•ด ์‹คํ–‰ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด django๋ฅผ gunicorn์„ ์ด์šฉํ•ด heroku์— ๋ฐฐํฌํ•  ๋•Œ Procfile์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

Procile

web: gunicorn ํ”„๋กœ์ ํŠธ๋ช….wsgi --log-file -

์œ„ ์ฝ”๋“œ์˜ ๋œป์€ ํ”„๋กœ์ ํŠธ๋ช….wsgi๋ชจ๋“ˆ์— ์žˆ๋Š” ์„ค์ • ์ •๋ณด๋ฅผ ์ด์šฉํ•ด gunicorn์„ ๊ตฌ๋™์‹œํ‚จ๋‹ค๋Š” ๋œป์ด๋‹ค.


django์˜ wsgi.py๋Š” ์›น์„œ๋ฒ„์™€ django project ์‚ฌ์ด์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐํฌ๋ฅผ ํ•  ๋•Œ wsgi.py์— ์žˆ๋Š” ์„ค์ •์„ ์ด์šฉํ•ด wsgi server๋ฅผ ๊ตฌ๋™ํ•˜๊ณ , nginx์™€ ๊ฐ™์€ ์›น ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•ด ์‹คํ–‰์‹œํ‚จ๋‹ค.


์ฐธ๊ณ  ์‚ฌ์ดํŠธ
django ๊ณต์‹ ๋ฌธ์„œ - WSGI๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•
MDN Docs Django ํŠœํ† ๋ฆฌ์–ผ ํŒŒํŠธ 11 - Django ์›น์‚ฌ์ดํŠธ ๊ณต๊ฐœํ•˜๊ธฐ
Spoaq ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ - Flask๋กœ ๋งŒ๋“ค์–ด ๋ณด๋Š” WSGI ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜

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

comment-user-thumbnail
2022๋…„ 7์›” 28์ผ

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ