web项目分布式部署整体框架方案,web部署整体框架


项目分布式部署整体框架方案

以下有两种方案和一个备选方案:
	方案1 :	client-->Nginx(openresty)-->gunicorn(通过wsgi启动托管)--?django(web应用服务)
	方案2 :	Nginx(openresty,upstream)-->supervisor(monitor)-->gunicorn(同uwsgi)-->django(web应用服务)
	
	备选方案:	按照原来的http启动方式
		nohup python manage.py runserver 0.0.0.0:9001
	&	Nginx(openresty,upstream)-->supervisor(monitor)HTTP--?django(HTTP)


使用如下框架和工具
	1.反向代理负载均衡服务器: Nginx(openresty)
	2.gunicorn(代替uwsgi)
	3.django
	4.supervisor(监控并拉起失败服务)

gunicorn安装

gunicorn简介:
	gunicorn是一个python wsgi http server,只支持Unix系统上运行,来源于ruby的unicorn.Gunicorn使用prefork master-worker模型,能够与各种的web框架进行协作
	
1.	pip install gunicorn -i https://pypi.douban.com/simple
2.	启动gunicorn命令:
	具体chdir是项目的basedir目录
	nohup gunicorn --chdir /home/zhouguangyou/artproject/ artproject.wsgi:application  --bind 0.0.0.0:9000  --workers=2 &

配置Nginx服务器

在nginx服务器配置中修改配置目录,将nginx.conf修改为:

# user  nobody;
# user nginx;
worker_processes  2;        #cat /proc/cpuinfo
# worker_cpu_affinity       0001 0010 0100 1000;

error_log  logs/error.log  notice;
# error_log  logs/error.log  debug;

pid  logs/nginx.pid;

events
{
    use   epoll;
    multi_accept  on;
    accept_mutex_delay  50ms;
    worker_connections  65535;    #每个worker支持最大网络连接数
}    

http
{
    include  mime.types;
    # default_type  application/octet-stream;
     default_type   text/html;

# log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"'
#                  '"$upstream_addr" "$upstream_status" "$upstream_response_time" '
#                  '$request_time -- $http_cookie -- $cookie_pin';


# access_log  logs/access.log  main;

sendfile  on;
tcp_nopush  on;

keepalive_timeout  0;

gzip  on;
gzip_min_length  1k;
gzip_buffers 48k;
gzip_http_version  1.1;
gzip_types  text/plain application/x-javascript text/css  text/shtml application/xml;

proxy_intercept_errors  on;
charset  utf-8;

######################
include  conf.d/*.conf;
######################

}

创建目录conf.d, 并添加如下信息到project.conf:

proxy_next_upstream  error;

server
{
    listen 8000;       #nginx提供对外的端口是8000, 通过路由 / 转发到127.0.0.1:9000/art/index 服务,而9000端口是上述gunicorn提供的端口
    server_name   localhost;

    client_max_body_size  50M;

    #配置项目静态资源目录
    location /static/ {
        
        root /home/zhouguangyou/artproject/art;
	}
  

    location / {
        proxy_pass   http://127.0.0.1:9000;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
     }

    # error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
     error_page  500  502  503  504 /50x.html;
     location = /50x.html {
         root  html;
      }

}

最后,重启nginx以使服务生效

以上配置基本完成了nginx+gunicorn+django 的配置

接下来:

安装supervisor服务

supervisor简介

supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当做supervisor的子进程来进行启动,因此,我们只需要将管理进程的可执行文件的路径添加到supervisor的配置文件中就好了.此时,被管理进程就被视为supervisor的子进程,若该子进程(被管理进程)发生异常,父进程(supervisor)可以及时获取到子进程的异常信息,通过读取配置文件中的配置了实现对子进程进行管理,比如设置autostart=true,可以实现子进程的异常中断-->自动重启

supervisor安装

linux(Ubuntu) 下执行:sudo apt install supervisor

supervisor配置

将supervisor设置为管理启动监控 gunicorn

往supervisor.conf 中添加如下信息

[group:artprojects]
programs=art-1, art-2  


[program:art-1]
command=gunicorn --chdir /home/zhouguangyou/artproject/ artproject.wsgi:application  --bind 0.0.0.0:9001  --workers=2
directory=/home/zhouguangyou/artproject
user=zhouguangyou
autorestart=true
redirect_stderr=true
stdout_logfile=log/art1.log
loglevel=info
stopsignal=INT


[program:art-2]
command=gunicorn --chdir /home/zhouguangyou/artproject/ artproject.wsgi:application  --bind 0.0.0.0:9002 --workers=2
directory=/home/zhouguangyou/artproject
user=zhouguangyou
autorestart=true
redirect_stderr=true
stdout_logfile=log/art2.log
loglevel=info
stopsignal=INT

[supervisord]
nodaemon=false
logfile=log/supervisord.log
pidfile=log/supervisord.pid
loglevel=info

此外,创建log文件夹存放日志文件

在nginx配置文件project.conf中加入如下信息

upstream artprojects{
    #ip_hash;  or  轮询(默认) or url_hash
    server 127.0.0.1:9001;
    server 127.0.0.1:9002;
 }

 location / {
       #(2) method2: use proxy_pass upstream to the supervisor who manage the gunicorn
      
      proxy_pass  http://artprojects;
  }

添加好上述配置信息后

(1)启动nginx

(2)通过supervisor启动gunicorn

supervisord -c supervisor.conf

页面通过http://127.0.0.1:8000/*/进行页面访问,观看效果。

综合上述,nginx做反向代理和负载均衡,将请求upstream转发给一个Supervisor监管的Gunicorn进程,而Gunicorn进程拖管了Django工程代码。

至此,Nginx + Gunicorn + Supervisor + Django 线上环境部署都已经完成!

相关内容

    暂无相关文章