nginx的作用
Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。
Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成 集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,
实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾
Nginx要实现负载均衡需要用到proxy_pass代理模块配置
Nginx负载均衡与Nginx代理不同地方在于
Nginx代理仅代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池
Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用。
upstream配置
在nginx.conf > http 区域中
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默认按照轮训方式负载,每个请求按时间顺序逐一分配到后端节点。
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,反复刷新
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
来源:oschina
链接:https://my.oschina.net/u/4295888/blog/3340375