【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
- nginx负载均衡例子
- 【nginx】负载均衡例子
- nginx做简单的负载均衡例子
- nginx负载均衡【nginx】
- nginx作为反向代理实现负载均衡的例子
- nginx 做为反向代理实现负载均衡的例子
- nginx做为反向代理实现负载均衡的例子
- nginx做为反向代理实现负载均衡的例子
- nginx做为反向代理实现负载均衡的例子
- nginx 作为反向代理实现负载均衡的例子
- nginx 作为反向代理实现负载均衡的例子
- nginx做为反向代理实现负载均衡的例子
- nginx做为反向代理实现负载均衡的例子 .
- nginx 作为反向代理实现负载均衡的例子
- nginx 作为反向代理实现负载均衡的例子
- nginx 作为反向代理实现负载均衡的例子
- Nginx负载均衡与反向代理的例子(图文)
- 转:nginx 作为反向代理实现负载均衡的例子
- 以撩妹为例,5分钟让你秒懂深度学习!
- 用javascript实现回到顶部效果
- 哪些因素会对mysql数据库服务器性能造成影响
- 数学在计算机科学中的重要性
- 大数据到底有多大,人工智能到底有多能
- 【nginx】负载均衡例子
- 世界上十大数据中心,全球数字经济"顶梁柱"
- 如何解读决策树和随机森林的内部工作机制?
- 漫画 | 小白也能看懂的量子物理漫画终于来了!
- 蒙特卡罗方法入门
- CodeForces
- 我们为什么要学数学?这里给你一个答案。
- 为何美洲蝉中意17这个质数?
- 成为Google认证机器学习工程师,零基础只需要2步?