哇,首先容许我感叹一声,nginx是真的恐怖,反向代理、负载均衡、web缓存等等内容的实现只需要修改下配置,谁能想到这一切会如此简单,不得不让人感叹,果然偷懒才是人类进步的源泉~~(笑)
先说下什么是web服务器的负载均衡,简单来说就是就是将多个请求分摊到多个服务器上,从而提高网络的可用性。大致架构应该是下面这样的灵魂图片(这次展示的内容实际上是同一个client的多次请求):
下面是具体步骤:
首先需要启动两台tomcat服务器,我偷了下懒,页面用的就是tomcat启动时默认页面。
顺便说一下在一台电脑上配置多个tomcat的方法:
1.增加环境变量CATALINA_HOME01,值为新的tomcat的地址。
2.增加环境变量CATALINA_BASE01,值为新的tomcat的地址。
3.修改新的tomcat中的startup.bat,把其中的CATALINA_HOME改为CATALINA_HOME01。
4.修改新的tomcat中的catalina.bat,把其中的CATALINA_HOME改为CATALINA_HOME01,CATALINA_BASE改为CATALINA_BASE01。
5.修改conf/server.xml文件,有以下三个部分的端口号需要修改:
<Server port="8005" shutdown="SHUTDOWN"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />(PS:如果出现 tomcat invalid command 'GET / HTTP/1.1' 这么个蛋疼的问题,那么可能是你这三处配置的端口号有冲突。)
(PPS:我两个server的端口号在上面三处的配置依次是18088,18080,18081和28005,28080,28009,随意的。)
记得启动之后访问下localhost:端口号验证是否启动成功。
此外,如果在tomcat启动过程中出现异常,但是运行startup.bat却又一闪而过的时候,可以如下图修改文件最后几行,这样就能看到异常内容了:
call "%EXECUTABLE%" run %CMD_LINE_ARGS% pause :end
接着,修改nginx配置文件nginx.conf,直接上超大的带注释的内容,应该不是那么难理解:
#运行用户 #user nobody; #开启进程数 <=CPU数 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 { #Linux下打开提高性能 #use epoll; #每个进程最大连接数(最大连接=连接数*进程数) 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; #设定请求缓冲 #client_header_buffer_size 1k; #large_client_header_buffers 4 4k; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成of。 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #客户端上传文件大小控制 #client_max_body_size 8m; #打开gzip压缩 #gzip on; #设定负载均衡的服务器列表 #服务器集群名字 upstream aiwome.com { #服务器配置 weight是权重的意思,权重越大,分配的概率越大。 #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。 #同一机器在多网情况下,路由切换,ip可能不同 #集群中的服务器列表,请求最终会发到这里执行 server 127.0.0.1:18080 weight=1; server 127.0.0.1:28080 weight=2; } server { #监听端口 listen 80; #当前服务的域名 server_name localhost; #设置字符集 #charset koi8-r; #本虚拟server的访问日志 #access_log logs/host.access.log main; #同样可以设置日志文件输出格式 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #location / { # root html; # index index.html index.htm; #} #更改根目录,因为现在做的是负载均衡。根据上面的配置,此时如果请求是localhost:80,就会交给aiwome.com集群来处理,也就是上面配置的服务器集群名称 location / { proxy_pass http://aiwome.com; proxy_redirect default; } #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; # } #} }
重要的是这两个部分:
#设定负载均衡的服务器列表 #服务器集群名字 upstream aiwome.com { #服务器配置 weight是权重的意思,权重越大,分配的概率越大。 #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。 #同一机器在多网情况下,路由切换,ip可能不同 #集群中的服务器列表,请求最终会发到这里执行 server 127.0.0.1:18080 weight=1; server 127.0.0.1:28080 weight=2; }
server{ ...... #更改根目录,因为现在做的是负载均衡。根据上面的配置,此时如果请求是localhost:80,就会交给aiwome.com集群来处理,也就是上面配置的服务器集群名称 location / { proxy_pass http://aiwome.com; proxy_redirect default; } ...... }
最后,启动nginx,在浏览器访问http://localhost/index.jsp,成果如下图所示。
多次刷新,会发现处理请求的是不同的服务器。(为了区别两个服务器,我对他们的index.jsp加了点小内容用以区分。不要在意打码的内容。)
以上,就是nginx结合tomcat实现负载均衡的一个小的demo啦~~