django + uwsgi + nginx 實現高並發環境部署 及 報錯處理


uwsgi

uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。

  1. WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通信的一種規范。
  2. uwsgi是一種線路協議而不是通信協議,在此常用於在uWSGI服務器與其他網絡服務器的數據通信。
  3. 而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
  4. uwsgi協議是一個uWSGI服務器自有的協議,它用於定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。

 

uWSGI的主要特點如下

 

  1. 超快的性能
  2. 低內存占用
  3. 多app管理
  4. 詳盡的日志功能
  5. 高度可定制(內存大小限制,服務一定次數后重啟等)

 

# 安裝使用
# 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 也可查看配置是否成功

會很明顯的列出錯誤  格式錯誤改格式   (會列出第幾行錯誤)   

          端口占用改端口......

反正就是哪里錯都會清楚的列出來,看着改就好了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM