Configure Server Load balancer in nginx + tomcat

Source: Internet
Author: User

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();}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.