Nginx的負載均衡和項目部署

nginx的做用

Nginx是一款自由的、開源的、高性能的HTTP服務器和反向代理服務器;同時也是一個IMAP、POP三、SMTP代理服務器;Nginx能夠做爲一個HTTP服務器進行網站的發佈處理,另外Nginx能夠做爲反向代理進行負載均衡的實現。html

 

Web服務器,直接面向用戶,每每要承載大量併發請求,單臺服務器難以負荷,我使用多臺WEB服務器組成 集羣,前端使用Nginx負載均衡,將請求分散的打到咱們的後端服務器集羣中,
實現負載的分發。那麼會大大提高系統的吞吐率、請求性能、高容災前端

Nginx要實現負載均衡須要用到proxy_pass代理模塊配置node

Nginx負載均衡與Nginx代理不一樣地方在於python

Nginx代理僅代理一臺服務器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池linux

Nginx能夠配置代理多臺服務器,當一臺服務器宕機以後,仍能保持系統可用。nginx

 

upstream配置

在nginx.conf > http 區域中web

upstream django { server 10.0.0.10:8000; server 10.0.0.11:9000; }

在nginx.conf > http 區域 > server區域 > location配置中算法

添加proxy_passdjango

location / { root html; index index.html index.htm; proxy_pass http://django;
}

此時初步負載均衡已經完成,upstream默認按照輪訓方式負載,每一個請求按時間順序逐一分配到後端節點。json

upstream分配策略

weight 權重

upstream django {
      server 10.0.0.10:8000 weight=5;
      server 10.0.0.11:9000 weight=10;#這個節點訪問比率是大於8000的
}

ip_hash

每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器
upstream django {
    ip_hash;
      server 10.0.0.10:8000;
      server 10.0.0.11:9000;
}

backup

在非backup機器繁忙或者宕機時,請求backup機器,所以機器默認壓力最小

upstream django {
      server 10.0.0.10:8000 weight=5;
      server 10.0.0.11:9000;
      server node.oldboy.com:8080 backup;
}

負載均衡實驗環境規劃

角色           ip                   主機名
lb01       192.168.119.10       lb01    
web01       192.168.119.11       web01
web02       192.168.119.12       web02

關閉防火牆

iptables -F
sed -i 's/enforcing/disabled/' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld

1、web01服務器配置nginx,建立index.html

server {
      listen       80;
      server_name 192.168.119.11;
      location / {
      root /node;
          index index.html index.htm;
      }
}

mkdir /node
echo 'i am web01' > /node/index.html

#啓動NGINX
./sbgin/nginx

2、web02服務器配置nginx,建立index.html

server {
  listen       80;
  server_name 192.168.119.12;
  location / {
      root /node;
      index index.html index.htm;
}

mkdir /node
echo 'i am web02...' > /node/index.html
#啓動nginx
./sbing/nginx

3、配置lb01服務器的nginx負載均衡

1.檢查lb01的 nginx.conf

http {
  include       mime.types;
  default_type application/octet-stream;
  sendfile       on;
  keepalive_timeout 65;
  upstream node {
    server 192.168.119.11:80;
    server 192.168.119.12:80;
}
  server {
      listen       80;
      server_name 192.168.119.10;
      location / {
        proxy_pass http://node;
        include proxy_params; #須要手動建立
      }
  }
}

2.手動建立proxy_params文件,文件中存放代理的請求頭相關參數

[root@lb01 conf]# cat /opt/nginx/conf/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

4、訪問lb01節點nginx,反覆刷新

 

nginx負載均衡調度算法

調度算法    概述
輪詢     按時間順序逐一分配到不一樣的後端服務器(默認)
weight    加權輪詢,weight值越大,分配到的訪問概率越高
ip_hash    每一個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個後端服務器
url_hash   按照訪問URL的hash結果來分配請求,是每一個URL定向到同一個後端服務器
least_conn 最少連接數,那個機器連接數少就分發

#1.輪詢(不作配置,默認輪詢)

#2.weight權重(優先級)

#3.ip_hash配置,根據客戶端ip哈希分配,不能和weight一塊兒用x

項目部署

**django若是經過python3 manage.py runserver形式運行,內部調用的是wsgiref模塊,運行的socket服務端**
**性能低下,單進程,單線程**

使用nginx+ uwsgi進行項目部署

1.準備django項目 NB_crm

經過unzip,解壓項目文件

2.安裝虛擬環境,在虛擬環境下,安裝uwsgi,進行部署

1,若是安裝了virtualenvwrapper工具能夠直接workon + 虛擬環境名 直接激活 2,若是沒有,就須要進入到虛擬環境的安裝目錄找到,找到bin文件下的 activate 文件,使用source + activate 激活虛擬環境

3.利用uwsgi運行一個python web腳本文件(瞭解)

新建一個py腳本文件,寫入以下內容 def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 啓動命令以下 uwsgi --http :8000 --wsgi-file test.py --http參數意思是,基於http協議運行 在 8000端口 --socket --wsgi-file  找到wsgi.py文件

4.利用uwsgi運行django項目

(以參數形式運行項目),(還有以配置文件形式運行,把運行的參數寫入到一個文件裏面,基於這個文件運行) 命令以下 uwsgi --http :8088 --module mysite.wsgi --module 找到django項目的第二層裏面的wsgi.py文件 #在django第一層裏運行 #uwsgi默認不支持靜態文件解析,使用nginx去解析靜態文件,不能加載靜態文件 

5.熱加載django項目,uwsig自動重啓django

uwsgi --http :9000 --module NBcrm.wsgi   --py-autoreload=1 #不用手動重啓服務端,就會本身檢測出改動並重啓

6.基於配置文件的形式,運行nbcrm(重要)

uwsgi.ini 建立在虛擬環境文件夾內

# uwsgi的配置文件 [uwsgi] # Django-related settings # the base directory (full path) #項目的絕對路徑,定位到nbcrm的第一層 chdir = /opt/NBcrm # Django's wsgi file
# 找到項目第二層的wsgi文件 module = NBcrm.wsgi # the virtualenv (full path) # 找到虛擬環境的絕對路徑 home = /root/Envs/nbcrm # process-related settings # master # 主進程 master = true # maximum number of worker processes # 開啓uwsgi的多進程數,根據cpu核數來定義 processes = 16 # the socket (use the full path to be safe # 基於socket連接運行crm,只有與nginx結合的時候,才使用socket形式 socket = 0.0.0.0:8000 # 當你沒用nginx,調試項目的時候,使用http形式 #http =  0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket    = 664 # clear environment on exit vacuum = true #指定一個參數,日誌放在哪 #若是你使用了supervisor,請註釋掉這個參數 #守護進程在後臺運行,且將日誌信息,輸出到uwsgi.log日誌中 #daemonize = uwsgi.log

啓動配置文件的命令

/root/Envs/nbcrm/bin/uwsgi   --ini uwsgi.ini

7.配置nginx,結合uwsgi,以及處理靜態文件的配置

nginx.conf請求轉發配置以下

 server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 0.0.0.0:8000; } } nginx處理crm的靜態文件方式 1.修改django的settings.py靜態文件 添加以下參數 # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/
        STATIC_ROOT='/opt/s20static' STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR,'statics'), ] 2.執行命令,收集crm的靜態文件 python3 /opt/NBcrm/manage.py collectstatic 3.配置nginx的location路徑匹配,找到crm這些靜態文件 在nginx.conf中找到server{}標籤,添加以下參數 #當個人請求url是 192.168.16.142:80/static/xxxxxxxx location /static { alias /opt/s20static/; } 4.啓動nginx,訪問nginx的80,是否能夠轉發到crm

8.使用supervisor進程管理工具,管理你的項目(?)

其實,supervisor就是在幫你執行命令而已 使用supervisor管理進程,這個進程不得在後臺運行,

退出虛擬環境,在物理環境下安裝supervisor 1.安裝命令 pip3 install -i https://pypi.douban.com/simple supervisor
2.建立supervisor的配置文件 echo_supervisord_conf > /etc/supervisor.conf 3.編輯配置文件,寫入管理nbcrm的任務參數 [program:s20nbcrm] command=/root/Envs/nbcrm/bin/uwsgi --ini uwsgi.ini stopasgroup=true ;默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程 killasgroup=true ;默認爲false,向進程組發送kill信號,包括子進程 4.啓動supervisor,去管理uwsgi supervisord -c /etc/supervisor.conf #指定配置文件,啓動這個服務 5.經過supervisorctl管理命令,管理uwsgi supervisorctl -c /etc/supervisor.conf 命令以下 status all start s20nbcrm stop s20nbcrm stop all

配置文件的格式

配置文件形式 nginx.conf my.cnf my.ini uwsgi.ini *.xml *.json

 

 

 

 

相關文章
相關標籤/搜索