用戶(瀏覽器) 請求網站資源 -> 直接定位到django後臺(全部的請求壓力,都直接給了後臺)css
django默認對併發性 不好,而且處理網頁的靜態資源,效率不好html
10萬個併發請求 -> 後臺應用python
用戶 > nginx(自然併發性很高,而且處理靜態資源css,js.jpg) ,靜態資源,nginx直接從磁盤上返回 >nginx
server{ listen 80; server_name localhost.com; location / { root /opt/django_crm; index index.html; } }
咱們在/opt/django_crm中 ,放入一個圖片 crm2.pngweb
這是一個靜態資源算法
localhost.com/crm.png ,若是找不到資源直接返回404數據庫
若是我發送了一個動態請求,(對數據庫進行交互,必須有編程語言的支撐)django
localhost.com/login/ -> django 後臺,django這個編程語言框架,就能夠對login請求處理編程
實驗環境準備,2臺機器vim
用戶: 瀏覽器發起請求, 獲得請求
192.168.15.71 nginx反向代理服務器(房屋中介代理)
192.168.15.73 真實資源服務器 (有房源的房東)
分別在 2 臺機器上,安裝nginx
先配置真實資源服務器,
192.168.15.73 打開這個機器的頁面是吃雞的網遊頁面
配置反向代理服務器
修改 **nginx.conf ** 以下
虛擬主機的加載,自上而下的加載,若是是訪問的ip地址,永遠訪問第一個
#咱們這個nginx服務器,再也不是用做虛擬主機了 #而是直接轉發別人的請求,是一個代理身份 server { listen 80; server_name s16chiji.com; location / { #root /opt/s16chiji; #index index.html; #當個人請求是 s16chiji.com的時候,這個nginx不作處理,直接轉發請求給另外一臺機器 proxy_pass http://192.168.15.73; } #經過這個參數,定義錯誤頁面的文件 ,當狀態碼是 404 400 401 時,返回40x.html頁面 error_page 404 401 400 403 /40x.html; }
nginx 腳本命令
nginx 直接輸入是啓動 nginx -s stop 中止 nginx -s reload 平滑重啓,從新讀取配置文件
如何配置nginx,支持負載均衡
環境準備,準備3臺機器
服務器1 nginx負載均衡器 192.168.15.71
服務器2 web應用資源1 192.168.15.73
服務器3 web應用資源2 39.96.68.102
配置負載軍器 ,192.168.15.71 機器
nginx.conf,修改添加以下參數
這個參數,應該寫在http{}內部,寫在server{}以上 upstream s16backup { server 192.168.15.75; server 192.168.15.118; }
第一個虛擬主機,修改配置以下
server { listen 80; server_name s16chiji.com; location / { #root /opt/s16chiji; #index index.html; #當個人請求是 s16chiji.com的時候,這個nginx不作處理,直接轉發請求給另外一臺機器 #proxy_pass http://192.168.15.73; #這個參數,轉發給地址池 proxy_pass http://s16backup; } #經過這個參數,定義錯誤頁面的文件 ,當狀態碼是 404 400 401 時,返回40x.html頁面 error_page 404 401 400 403 /40x.html; }
分別啓動負載均衡器的nginx服務,以及兩個資源服務器
nginx負載均衡算法
默認是輪訓方式,你一次我一次
權重算法: upstream django { server 192.168.15.73 weight=2; server 192.168.15.118 weight=8; } ip 哈希算法: ip哈希和權重不得公用 upstream django { server 192.168.15.73 ; server 192.168.15.118 ; ip_hash; }
在進行項目部署的時候,若是報錯
解決辦法: no application not found 就是由於你的uwsgi沒找到django的wsgi.py應用文件
爲何要用nginx uwsgi
由於用戶只想訪問 域名,不帶有任何端口 經過nginx反向代理,用戶直接訪問 s16chiji.com ,可是nginx直接轉發給了django,咱們其實看到的頁面是django uwsgi是支持併發的 python web服務器,讓你的django,併發性更高,可是uwsgi不支持靜態文件的處理,靜態文件會丟失
用nginx處理靜態文件,uwsgi處理動態請求
項目部署實驗步驟
建立新的虛擬環境,且解決crm的環境依賴
在虛擬環境下安裝uwsgi
pip3 install uwsgi
學習uwsgi命令,如何啓動python應用
啓動python web文件 建立一個test.py寫入以下代碼
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3
用uwsgi啓動一個python web文件
指定8000端口啓動 http服務
指定wsgi文件
uwsgi --http :8000 --wsgi-file test.py
用uwsgi啓動django項目
uwsgi --http :9000 --module Alibab_crm.wsgi
uwsgi加上熱加載命令
uwsgi --http :8000 --module Alibab_crm.wsgi --py-autoreload=1
使用uwsgi配置文件去啓動項目
(alicrm) [root@s16ds Alibab_crm]# cat uwsgi.ini mysite_uwsgi.ini file
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) #指定django的項目目錄,第一層 chdir = /opt/django_crm # Django's wsgi file #找到django的wsgi文件 #這裏須要寫項目的第二層目錄Alibab_crm module = django_crm.wsgi # the virtualenv (full path) #填寫虛擬環境的絕對路徑 home = /root/Envs/DjangoCRM # process-related settings # master master = true # maximum number of worker processes processes = 5 # the socket (use the full path to be safe #指定socket協議,運行django,只能與nginx結合時使用 #指定socket協議,運行django,只能與nginx結合時使用 #指定socket協議,運行django,只能與nginx結合時使用 socket = 0.0.0.0:8080 #若是你沒用nginx,只想本身啓動一個http界面,用這個 # http = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
經過配置文件啓動uwsgi
uwsgi --ini uwsgi.ini
收集django crm的靜態文件
編輯crm的settings.py配置文件 寫入以下代碼
定義django的靜態資源根目錄,便於用命令收集資源,存放的地兒
STATIC_ROOT="/opt/crm_static" STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
用命令收集靜態文件
python3 manage.py collectstatic
配置nginx,反響代理django服務器,且解析靜態文件
proxy_pass 僅僅是請求轉發的參數,與uwsgi結合,還有跟高級的協議參數
修改nginx配置文件以下
server { listen 80; server_name s16chiji.com; location / { root /opt/s16chiji; index index.html; # 使用uwsgi_pass 轉發基於uwsgi協議的一個請求 uwsgi_pass 192.168.15.71:8000; include /opt/nginx112/conf/uwsgi_params; } #配置一個url的入口,告訴django靜態文件在哪裏去找 #當請求url是 s16chiji.com/static/的時候 #就進行別名,nginx去/opt/crm_static下尋找js文件 location /static { alias /opt/crm_static/; } #經過這個參數,定義錯誤頁面的文件 ,當狀態碼是 404 400 401 時,返回40x.html頁面 error_page 404 401 400 403 /40x.html; error_page 500 502 503 504 /50x.html; }
此時nginx結合uwsgi 已經完成
192.168.15.71
記住這裏推出虛擬環境,使用物理環境去運行