nginx 负载均衡
(1)准备三台虚拟机,IP地址分别为172.31.108.114,172.31.108.115,172.31.108.116。114服务器安装ngnix服务器,115,116安装tomcat服务器。
(2)nginx配置文件主要配置信息如下:
#user nobody; # 允许生成的进程数,默认为1 worker_processes 1; #全局错误日志 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #ngnix运行id #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 65; #gzip on; #负载均衡配置 upstream mystr{ server 172.31.108.115:8090; server 172.31.108.200:8090; } server { listen 80; #监听端口 server_name localhost; #监听地址 #charset koi8-r; #access_log logs/host.access.log main; #请求的url地址过滤 location / { 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; } #请求的url地址匹配正则表达式 location ~ \.jsp$ { proxy_pass http://mystr; } } }
(3)主要修改ngnix配置信息如下即可实现负载均衡。
upstream mystr{ server 172.31.108.115:8090; server 172.31.108.116:8090; } location ~ \.jsp$ { proxy_pass http://mystr; }
(4)分为修改一下115,116 tomcat服务器下默认首页index.jap页面,目录 /webapp/ROOT/index.jsp,访问两次http://172.31.108.114/index.jsp进行测试,显示如下: