【nginx】负载均衡例子

来源:互联网 发布:mysql 查询结果存为表 编辑:程序博客网 时间:2024/05/17 09:04

使用nginx+flask做了一个小的负载均衡例子。

由于机器有限,只是基于端口做的,用的是同一台机器。

flask:

server1,

from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():print("server1")return "Hello World!"if __name__ == "__main__":    app.run(host='0.0.0.0', port=50000, debug=True)

server2

from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():print("server2")return "Hello World!"if __name__ == "__main__":    app.run(host='0.0.0.0', port=50001, debug=True)
然后配置nginx的conf文件。MacOS的位置在:/usr/local/etc/nginx/nginx.conf。

主要添加http节点下的upstream节点,用于指定负载均衡的机器ip。

   upstream www.test123.com{        server 127.0.0.1:50010;        server 172.16.33.163:50009;    }
然后修改server节点

   server {        listen       50011;        server_name  www.test123.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass http://www.test123.com;            root   html;            index  index.html index.htm;        }

完整的文件:

#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       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  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    upstream www.test123.com{        server 127.0.0.1:50010;        server 172.16.33.163:50009;    }    server {        listen       50011;        server_name  www.test123.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass http://www.test123.com;            root   html;            index  index.html index.htm;        }        #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;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    include servers/*;}

运行nginx启动服务器,同时sudo python server1.py,sudo python server2.py启动flask。

此时如果直接访问www.test123.com:50011,是不行的,因为我们的域名没有注册,DNS查不到,所以需要在本机的hosts文件里面添加域名,本地的DNS缓存。

位置在/etc/hosts文件:


### Host Database## localhost is used to configure the loopback interface# when the system is booting.  Do not change this entry.##127.0.0.1localhost255.255.255.255broadcasthost::1             localhost 74.125.237.1dl-ssl.google.com127.0.0.1www.test123.com
添加一条映射即可。

最后访问www.test123.com:50011,即可在flask后端请求是轮询转发的。


阅读全文
0 0
原创粉丝点击