Nginx+Tomcat集群与负载均衡

Nginx+Tomcat集群与负载均衡

架构描述

前端一台nginx服务器做负载均衡器,后端放N台tomcat组成集群处理服务,通过nginx转发到后面(注:没做动静分离,静态动态全部都转给tomcat)
优点:实现了可弹性化的架构,在压力增大的时候可以临时添加tomcat服务器添加到这个架构里面去.

先修改nginx.conf配置

如想了解Nginx负载均衡策略,请查阅Nginx负载均衡策略.

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 500502503504 /50x.html;
		location = /50x.html {
			root /usr/share/nginx/html;
		}}}

修改tomcat配置

找到TOMCAT_HOME/conf/server.xml,在host标签处加入Context标签
注意:如果是单机启动多tomcat需要改变对应的port,否则会启动不了,多机tomcat无此情况

<ContextdocBase="/opt/tomcat/cluster01/webapps/ROOT"path=""/>

到此重启tomcat与nginx服务即可.


你可能感兴趣的:(nginx,tomcat集群)