架構介紹? ? ? ??
????????linux一般采用nginx + uwsgi部署django,在Windows下,可以取代uwsgi的選項包括Waitressa、Daphnea、Hypercoma和Gunicorna(通過WSLa 運行)。windows服務器一般采用nginx + waitress 部署django,,他們的關系如下
????????django是WEB應用框架,Nginx是web服務器,uwsgi或者waitress 是實現wsgi協議的東西。
????????django當然也可以python manage.py runserver 0.0.0.0:80來讓外部訪問,在生產環境中不推薦使用 Django 的 runserver
命令,主要有以下幾個原因:
-
安全性問題:
-
runserver
是為開發和調試設計的,沒有經過安全審計或性能測試。 -
它默認綁定在 0.0.0.0 地址上,對外網完全開放,可能會受到未授權訪問的風險。
-
不支持 HTTPS,無法保證數據傳輸的安全性。
-
-
性能問題:
-
runserver
使用單線程處理請求,無法應對高并發場景。 -
它會為每個請求重新加載和執行 Django 應用程序,導致性能低下。
-
-
功能限制:
-
缺乏生產環境中需要的負載均衡和容錯機制。
-
不支持靜態文件的高效服務,通常需要借助專門的靜態文件服務器(如 Nginx)。
-
-
替代方案:
-
在生產環境中,推薦使用專業的 Web 服務器(如 Nginx 或 Apache)搭配 WSGI 服務器(如 Gunicorn 或 uWSGI)來部署 Django 應用。
-
例如,可以使用 Nginx 作為反向代理服務器,將請求代理給 Gunicorn 來處理 Django 應用
-
windows下使用nginx + waitress 部署django
下載安裝nginx和配置文件
1、下載
下載鏈接:nginx: download
一直都只在linux中使用nginx,還從未在windows中使用,感覺在windows中使用nginx更為簡單
2、安裝
下載的是一個壓縮包,找個目錄解壓即可,無需安裝,解壓出來的內容為:
其中nginx.exe
是入口程序,不考慮系統命令的情況下,cd到當前目錄,即可使用nginx的命令了:
Options:
? -?,-h ? ? ? ? : this help
? -v ? ? ? ? ? ?: show version and exit
? -V ? ? ? ? ? ?: show version and configure options then exit
? -t ? ? ? ? ? ?: test configuration and exit
? -T ? ? ? ? ? ?: test configuration, dump it and exit
? -q ? ? ? ? ? ?: suppress non-error messages during configuration testing
? -s signal ? ? : send signal to a master process: stop, quit, reopen, reload
? -p prefix ? ? : set prefix path (default: NONE)
? -e filename ? : set error log file (default: logs/error.log)
? -c filename ? : set configuration file (default: conf/nginx.conf)
? -g directives : set global directives out of configuration file
如果把nginx設置到環境變量中,即可在全局使用nginx
命令。
3、配置文件
和linux環境下配置一樣,這里貼一份基礎配置,主要是修改nginx目錄下的conf/nginx.conf
:
worker_processes 2;error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;sendfile on;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost 121.199.1.144;location / {proxy_set_header Host $host;proxy_pass http://127.0.0.1:8000;}location /static {alias C:\inetpub\wwwroot\sanxue\static;}location /media {alias C:\inetpub\wwwroot\sanxue\media;}
下載waitress和使用它
1、下載
pip install waitress
2、使用
waitress
的使用太簡單了,國內使用的人也非常少,在django項目的根目錄創建run.py
(文件名隨意),內容如下:
from waitress import serve
from sanxue.wsgi import applicationserve(app=application,host='127.0.0.1',port=8000
)
然后使用命令行python run.py
即可啟動django的服務了,比IIS
或apache
的簡單太多了,跑個中小項目都不成問題。
如果想把以上的命令加到windwos服務中,可參考下面的第3點。
參考
1、waitress
官方文檔https://docs.pylonsproject.org/projects/waitress/en/stable/index.html
2、如何在python web項目中使用waitress?Run Python WSGI Web App with Waitress | DevDungeon
3、如何把python項目構建成windows服務?Run Python Script as Windows Service | DevDungeon