架构描述
前端一台nginx服务器做负载均衡器,后端放N台tomcat组成集群处理服务,通过nginx转发到后面(注:没做动静分离,静态动态全部都转给tomcat)
优点:实现了可弹性化的架构,在压力增大的时候可以临时添加tomcat服务器添加到这个架构里面去.
先修改nginx.conf配置
如想了解Nginx负载均衡策略,请查阅Nginx负载均衡策略.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
user nginx; worker_processes 10; worker_rlimit_nofile 100000; error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; events { worker_connections 1024; use epoll; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; gzip on; gzip_static on; gzip_comp_level 5; gzip_min_length 1024; keepalive_timeout 65; limit_conn_zone $binary_remote_addr zone=addr:10m; # Load config files from the /etc/nginx/conf.d directory include /etc/nginx/conf.d/*.conf; upstream www.wp.com { #此处为轮询策略 换成你自己的tomcat地址 #可以根据自己的需求配置多个tomcat,当某个地址无效时,nginx会自动切换 server 192.168.5.206:8081; server 192.168.5.206:8082; } server { #配置对应的端口与域名 listen 80; server_name www.wp.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; #配置上面的server name proxy_pass http://www.wp.com; proxy_set_header X-Real-IP $remote_addr; } location ~ ^/(WEB-INF)/ { deny all; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } } |
修改tomcat配置
找到TOMCAT_HOME/conf/server.xml,在host标签处加入Context标签
注意:如果是单机启动多tomcat需要改变对应的port,否则会启动不了,多机tomcat无此情况
1 |
<Context docBase="/opt/tomcat/cluster01/webapps/ROOT" path="" /> |
到此重启tomcat与nginx服务即可.