Using nginx as the front-end server and tomcat as the back-end server can achieve the tomcat cluster through some simple configuration. Generally, you only need to configure nginx in the nginx configuration file nginx. conf as follows.
http { upstream tomcat-host{ server 192.168.1.201:8080 weight=3; server 192.168.1.202:8080; ip_hash; } server { listen 80; server_name www.domain.com; location / { proxy_pass http://tomcat-host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; } }}
Upstream is used to configure the backend server list and load balance weights. ip_hash can assign multiple requests from the same client to a fixed backend server to solve the session problem to a certain extent, to solve the post-cluster session problem perfectly, you can use memcached. In the server Segment configuration, proxy_pass is used, and the prxoy_pass destination address is the upstream name set above. Note that"Http: // ", proxy_set_header processes the client's IP information. Because the front end is nginx, request is used in tomcat java applications. getRemoteAddr (). The obtained ip address is no longer the Client ip address, but the nginx Server ip address. The solution is to modify the java application. You can use the following code:
String ip = request.getHeader("x-forwarded-for");if (ip == null || "".equals(ip.trim())) {ip = request.getRemoteAddr();} else {String[] ars = ip.split(",");ip = ars[ars.length-1].trim();}