nginx的作用
Nginx是一款自由的、開源的、高性能的HTTP服務器和反向代理服務器;同時也是一個IMAP、POP3、SMTP代理服務器;Nginx可以作為一個HTTP服務器進行網站的發布處理,另外Nginx可以作為反向代理進行負載均衡的實現。
Web服務器,直接面向用戶,往往要承載大量並發請求,單台服務器難以負荷,我使用多台WEB服務器組成 集群,前端使用Nginx負載均衡,將請求分散的打到我們的后端服務器集群中,
實現負載的分發。那么會大大提升系統的吞吐率、請求性能、高容災
Nginx要實現負載均衡需要用到proxy_pass代理模塊配置
Nginx負載均衡與Nginx代理不同地方在於
Nginx可以配置代理多台服務器,當一台服務器宕機之后,仍能保持系統可用。
upstream django { server 10.0.0.10:8000; server 10.0.0.11:9000; }
在nginx.conf > http 區域 > server區域 > location配置中
添加proxy_pass
location / { root html; index index.html index.htm; proxy_pass http://django; }
此時初步負載均衡已經完成,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
一、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
二、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
三、配置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;
四、訪問lb01節點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服務端** **性能低下,單進程,單線程**
通過unzip,解壓項目文件
1,如果安裝了virtualenvwrapper工具可以直接workon + 虛擬環境名 直接激活 2,如果沒有,就需要進入到虛擬環境的安裝目錄找到,找到bin文件下的 activate 文件,使用source + activate 激活虛擬環境
新建一個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文件
(以參數形式運行項目),(還有以配置文件形式運行,把運行的參數寫入到一個文件里面,基於這個文件運行) 命令如下 uwsgi --http :8088 --module mysite.wsgi --module 找到django項目的第二層里面的wsgi.py文件 #在django第一層里運行 #uwsgi默認不支持靜態文件解析,使用nginx去解析靜態文件,不能加載靜態文件
uwsgi --http :9000 --module NBcrm.wsgi --py-autoreload=1 #不用手動重啟服務端,就會自己檢測出改動並重啟
# 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
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
退出虛擬環境,在物理環境下安裝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