nginx+uwsgi负载均衡部署Flask项目

uwsgi和Nginx的安装已经在上一篇部署django项目中介绍过了,这里就不在另行赘述,不清楚的可以参考上一篇文章,这里直接开始部署项目:

这里我们的项目目录为/OSAOP,首先在项目目录下创建一个script文件放置uwsgi配置文件和日志

mkdir /OSAOP/script

然后按照以下步骤进行

一、配置uwsgi,为了配置负载均衡我们这里准备两个uwsgi的配置文件,启动2个wsgi进程

vim uwsgi.ini

写入如下配置信息:

[uwsgi]
socket=192.168.179.131:8000
#http=192.168.179.131:8000
chdir=/report_tool
wsgi-file=main.py
callable=app
processes=5
threads=4
master=True
pidfile=/report_tool/script/uwsgi.pid
daemonize=/report_tool/log/uwsgi.log
buffer-size  = 51200
touch-reload=/report_tool
二、配置另外一个uwsgi文件

vim uwsgi2.ini

然后写入如下配置信息:

[uwsgi]
socket=192.168.179.131:8000
#http=192.168.179.131:8000
chdir=/report_tool
wsgi-file=main.py
callable=app
processes=5
threads=4
master=True
pidfile=/report_tool/script/uwsgi2.pid
daemonize=/report_tool/log/uwsgi2.log
buffer-size  = 51200
touch-reload=/report_tool
三、到此处两个uwsgi的配置文件已经配置完成,下面开始配置Nginx

打开nginx的配置文件:

 vim /etc/nginx/nginx.conf 

输入如下信息:

user root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

#include的作用是可以将server的配置信息写到conf.d目录下面的conf文件

   include /etc/nginx/conf.d/*.conf;
   upstream OSAOP {

       server 192.168.92.134:8000;

       server 192.168.92.134:8001;

}
 } 

然后保存信息后打开default.conf配置server信息

vim  /etc/nginx/conf.d/default.conf

可以将文件的原内容全部清空,下入以下信息:

server {
    listen         80; 
    server_name    192.168.92.134 
    charset UTF-8;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;
 
    client_max_body_size 75M;
 
  # 设置媒体文件目录
    location /media  {
        alias /OSAOP/media/; 
    }
    # 设置静态文件目录
    location /static {
        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /OSAOP/staticfiles/;
     }

location / {
        root /OSAOP/templates/;
        index index.html index.htm;
      }

   location /osaop {
            include uwsgi_params;
            uwsgi_pass OSAOP;
        }
 }
到此已经完成了所有配置,并实现了对两个端口的负载均衡,下面启动uwsgi和nginx

uwsgi --ini uwsgi.ini

uwsgi --ini uwsgi2.ini

/usr/sbin/nginx -c /etc/nginx/nginx.conf 

uwsgi停止命令:

uwsgi --stop /OSAOP/script/uwsgi.pid

uwsgi --stop /OSAOP/script/uwsgi2.pid

nginx重启命令:

/usr/sbin/nginx -s reload

启动之后可以查看uwsgi和nginx进程:

ps -ef|grep uwsgi
ps -ef|grep nginx
 

你可能感兴趣的:(flask)