您的位置:首页 > 理论基础 > 计算机网络

[置顶] nginx负载均衡教程之从不用root编译开始! + flask + uwsgi 部署高并发网络服务!

2016-08-07 23:44 471 查看
编译使用nginx without root!

之前阅读了很过国内的博客,对nginx在linux下的使用都是一笔带过,这个给后面的使用造成了很大的麻烦!尤其是在不用root怎么安装这块!本部分详细的讲解,供新人使用!此处参考了一个国外人的[wiki](https://gist.github.com/simonw/92481),个别的时候需要翻墙才能看啊!
本文的所有的参考链接: https://www.nginx.com/resources/admin-guide/installing-nginx-open-source/ https://gist.github.com/simonw/92481 http://nginx.org/en/docs/http/load_balancing.html[/code] 
1,###此package不用install,只要解压即可
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz tar -zxf pcre-8.39.tar.gz

2,###同上
wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz tar -zxf openssl-1.0.2f.tar.gz

3,###同上
wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxf zlib-1.2.8.tar.gz

4,###安装nginx
wget "http://nginx.org/download/nginx-1.10.1.tar.gz"
tar -zxvf nginx-1.10.1.tar.gz
mkdir -p nginx_tool
cd nginx-1.10.1
./configure --prefix=../nginx_tool/ --with-pcre=(刚才解压的pcre的绝对路径) --with-zlib=(刚刚解压的zlib路径) --with-openssl=(刚才解压的openssl的绝对路径)
make
make install


当完成上面这些操作的时候,nginx就会安装在给定的nginx_tool路径下,install完成以后,可以查看下nginx_tool中都有哪些directions。一般情况下,如下所示:
$ls nginx_tool/
conf  html  logs  sbin
#恭喜你,sbin中的nginx就是编译好的可执行的工具,conf中的nginx.conf就是默认的配置文件,这个文件在后面会有很大的用处!


使用flask 做个简单的demo进行nginx尝试

第一步,就是用flask做个简单的demo;

如果您没有安装flask,或者没有任何对flask使用的经验,请预先安装flask!

方法是:

pip install flask --user   #方法,可以不用root进行安装


mkdir -p test_flask
cd test_flask
vim test_flask.py


在test_flask.py中输入以下内容:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
return 'Hello Python 1' #此处的数字是后面用来验证负载均衡使用的!

if __name__ == '__main__':
app.run(host="此处换成您机器的ip,如果不知道怎么获取,请查询ifconfig命令使用方法",port=9090)


执行python test_flask.py ,当你看到下面提示的时候,表示你的第一个flask demo就完成了;

* Running on http://yourip:9090/ (Press CTRL+C to quit)


第二步,就是用nginx,把咱们的flask demo部署上来了

在niginx conf directions下新建一个文件,叫做test_nginx.conf

输入以下内容

worker_processes 20;
events {
worker_connections  1024;
}
http {

upstream back {
#least_conn;
server 127.0.0.1:8597 weight=1;
server 127.0.0.1:8598 weight=1;
server 127.0.0.1:8599 weight=1;
}
server {
listen  9876;
server_name  1.1.1.3:9876; #公网地址,此处应该将公网地址绑定为您计算机对外网公开的地址,例如10.109.108.134;

location / {
proxy_pass   http://back;  # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
}
}
}


在后台启动3个test_flask.py的进程,地址都是127.0.0.1,端口号分别是8597,8598,8599,每个脚本中的“hello world ”字符串中加入不同的数字,为了后面验证负载均衡使用;

而后按照下面的步骤执行;

cd nginx/sbin
./nginx -c ../conf/test_nginx.conf


而后,打开您的浏览器,输入ip:9876您就会看到“hello world 1”,“hello world 2”,“hello world 3”,在您的python web server上来回切换了!更多的使用nginx负载均衡的文档,详见文章开头提供的链接!

第二步,就是标准化,用uwsgi规范化服务器部署

# uwsgi使用教程

- 什么是uwsgi

from flask import Flask,request
app = Flask(__name__)
@app.route('/welcome',methods = ["GET"])
def hello_world():
return 'Hello World!'
@app.route('/answer',methods=["POST"])
def ans():
data = request.form.get("data")
return "From server: " + data
if __name__ == '__main__':
app.run(host="ip",port=8970)


怎么看这段code都是简陋的,存在着下面的几个问题:

1,链接超时怎么办?这个server是否还会稳定?

2,如果这个server我想限制链接的数量又该怎么办?

3,限制链接的最大时长呢?

4,更改外部的链接协议呢?

5,and so on ……

从安装uwsgi开始

pip install uwsgi --user


托管flask app

$uwsgi -s 10.101.167.107:8990 -w main:app --buffer-size=32768  --protocol http --harakiri 60 --limit-post 65536 -p 1
*** Starting uWSGI 1.4.9 (64bit) on [Mon Aug 22 19:58:49 2016] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-4) on 05 August 2016 22:14:14
os: Linux-3.10.0-327.ali2000.alios7.x86_64 #1 SMP Tue Dec 29 19:54:05 CST 2015
nodename: rs1d07474.et2sqa
machine: x86_64
clock source: unix
pcre jit disabled


此时在浏览器中打开 http://ip:8970/welcome , 同样会显示hello world!终于,您的flask app正式的托管在了uwsgi这个server上,走出了正式化的一步!

这个时候,您就可以使用将第二步中的简陋的python webserver ,用uwsgi托管之后在使用nginx负载均衡了!

uwsgi的参数的介绍文档1:uwsgioption.html

uwsgi的快速开始文档1:WSGIquickstart.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: