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管理
- 詳盡的日志功能
- 高度可定制(記憶體大小限制,服務一定次數后重啟等)
# 安裝使用
# centos安裝uwsgi前先install gcc 和 python3-devel
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 也可查看配置是否成功
會很明顯的列出錯誤 格式錯誤改格式 (會列出第幾行錯誤)
埠占用改埠......
反正就是哪里錯都會清楚的列出來,看著改就好了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/179272.html
標籤:Python
上一篇:Python_字典方法
下一篇:dispatch
