uwsgi uWSGI是一個Web伺服器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI伺服器進行交換。 WSGI是一種Web伺服器網關介面。它是一個Web伺服器(如nginx,uWSGI等伺服器)與web應用(如用Flask框架寫的 ...
uwsgi
uWSGI是一個Web伺服器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI伺服器進行交換。
- WSGI是一種Web伺服器網關介面。它是一個Web伺服器(如nginx,uWSGI等伺服器)與web應用(如用Flask框架寫的程式)通信的一種規範。
- uwsgi是一種線路協議而不是通信協議,在此常用於在uWSGI伺服器與其他網路伺服器的數據通信。
- 而uWSGI是實現了uwsgi和WSGI兩種協議的Web伺服器。
- uwsgi協議是一個uWSGI伺服器自有的協議,它用於定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。
uWSGI的主要特點如下
- 超快的性能
- 低記憶體占用
- 多app管理
- 詳盡的日誌功能
- 高度可定製(記憶體大小限制,服務一定次數後重啟等)
# 安裝使用 pip install uwsgi # test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"asdf"] #運行 uwsgi --http :8000 --wsgi-file test.py #用uwsgi 啟動django uwsgi --http :8000 --module mysite.wsgi #可以把參數寫到配置文件里 xxx-uwsgi.ini [uwsgi] http = :9000 #the local unix socket file than commnuincate to Nginx socket = 127.0.0.1:8001 # abs path chdir = project path # Django's wsgi file wsgi-file = xxx/wsgi.py # maximum number of worker processes processes = 4 #thread numbers startched in each worker process threads = 2 #monitor uwsgi status stats = 127.0.0.1:9191 # clear environment on exit vacuum = true #啟動 which uwsgi # check installed path path crazye-uwsgi.ini
Nginx
sudo apt install nginx
path start 同uwsgi
如圖配置 ,粗心在這卡了許久,千萬不要寫錯了!!!
1 upstream django # the upstream component nginx needs to connect to 2 3 server 127.0.0.1:xxxx; # for a web port socket (we'll use this first) 4 5 server # configuration of the server 6 7 listen # the domain name it will serve for 8 9 server_name # substitute your machine's IP address or FQDN 10 11 client_max_body_size # adjust to taste 12 13 location /media { 14 alias /path/to/your/mysite/media; # your Django project's media files - amend as required 15 } 16 17 location /static { 18 alias /path/to/your/mysite/static; # your Django project's static files - amend as required 19 } 20 21 # Finally, send all non-media requests to the Django server. 22 location / { 23 uwsgi_pass django; 24 include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed 25 } 26 }description
圖中的params如圖
在nginx 的ennabled文件中創建配置的軟連接
sudo ln
-
s ~
/
path
/
to
/
your
/
mysite
/
mysite_nginx.conf
/
etc
/
nginx
/
sites
-
enabled
/
python manage.py collectstatic
集中靜態文件
在項目的settings.py 中添加
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
啟動nginx和uwsgi 即可實現高併發
報錯其實只要根據他給的提示進行操作即可
Job for nginx.service failed because the control process exited with error code. Job for nginx.service failed because the control process exited with error code. See "syste.......
輸入命令查看報錯信息
nginx -t 也可查看配置是否成功
會很明顯的列出錯誤 格式錯誤改格式 (會列出第幾行錯誤)
埠占用改埠......
反正就是哪裡錯都會清楚的列出來,看著改就好了