nginx + uWSGI 提高 Django的併發性(看着不錯)

1. uWSGI :
uWSGI是一個web服務器,實現了WSGI協議、uwsgi協議、http協議等。
uWSGI的主要特點是:
超快的性能
低內存佔用
多app管理
詳盡的日誌功能(可以用來分析app的性能和瓶頸)
高度可定製(內存大小限制,服務一定次數後重啓等)
uWSGI服務器自己實現了基於uwsgi協議的server部分,我們只需要在uwsgi的配置文件中指定application的地址,uWSGI就能直接和應用框架中的WSGI application通信。
2. nginx :
Nginx 是一個高性能的負載均衡HTTP和反向代理服務器,Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件代理服務器。
特點是佔有內存少,併發能力強。
結構與擴展:一個主進程和多個工作進程。工作進程是單線程的,且不需要特殊授權即可運行;
3. nginx和uWSGI的關係:
nginx相當於是服務器,負責接收請求
uwsgi是服務器和服務端應用程序的通信協議,規定了怎麼把請求轉發給應用程序和返回
2個基本概念:
服務器(接收請求),應用程序(處理請求並返回)
通信過程:
客戶端發送一個http請求,被nginx服務器接收,nginx服務器將請求轉發給uwsgi,uwsgi將請求轉發給實現uwsgi
協議的應用程序(flask,gunicorn等等)
4. uWSGI的安裝:
# pip3 install uwsgi
2、創建django項目並配置uWSGI配置文件:
cd 進入到 django 的主目錄
vi test-uwsgi.ini # 添加以下配置文件,按需修改即可:
[uwsgi]
# 對外提供 http 服務的端口
http = :9000
#用於和 nginx 進行數據交互的端口
socket = 127.0.0.1:8001
# django 程序的主目錄。
chdir = /project/django
# Django's wsgi file
wsgi-file = /project/django_test/django_test/wsgi.py
# 最大的工作進程數
processes = 4
#在每個進程中的最大線程數
threads = 2
# 通過該端口可以監控 uwsgi 的負載情況
stats = 127.0.0.1:9999
# 清理環境出口
vacuum = true
# 後臺運行,並輸出日誌
daemonize = /var/log/uwsgi.log
3、通過 uwsgi 配置文件啓動 django 程序
uwsgi test-uwsgi.ini # 在瀏覽器中 通過訪問 http://ip:9000 可以看到發佈的 django 程序
如果uwsgi沒有寫入環境變量,需要寫絕對路徑/usr/local/python3/bin/uwsgi
查看uwsgi的啓動進程狀態
netstat -lnpt | grep uwsgi
4、安裝nginx並配置 :
yum install nginx
配置 nginx 的配置文件
在 django 的主目錄下創建下面的 nginx 配置文件,然後做軟連接到 nginx 的配置文件目錄,或者直接在 nginx 配置文件中添加內容
默認配置文件路徑在/etc/nginx/nginx.conf
配置內容
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
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 /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 10.1.62.123; # substitute your machine's IP address or FQDN
charset utf-8;

# max upload size
client_max_body_size 75M; # adjust to taste

# Django media
#location /media {
# alias /path/to/your/mysite/media; # your Django project's media files - amend as required
#}

location /static {
alias /opt/jira_platform/static; # django項目靜態文件的路徑
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 127.0.0.1:8001; #注意這個ip地址要和uwsgi配置文件裏的對應
include uwsgi_params; # the uwsgi_params file you installed
}
}
}

5.2 重啓nginx 服務
[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload
[root@crazy-acong django_test]# netstat -lnpt | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/nginx
這個時候就可以通過 http://ip:8000 訪問 django 程序了,如果發現靜態文件貌似沒讀取到,需要查看靜態文件目錄路徑是否正確
可以通過下面的方法解決靜態文件的問題
在 django 程序的 settings.py 文件中添加以下內容
STATIC_ROOT = os.path.join(BASE_DIR, "static_all") #static文件的目錄
# 然後通過執行該命令,將靜態文件整合到一個目錄中
[root@crazy-acong django_test]# python3 manage.py collectstatic
最後重啓 nginx 服務即可

來源:https://www.cnblogs.com/slqt/p/10825771.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章