Nginx Balanced Multi-tomcat environment configuration, and REMOTEIP and servername access in this environment

Source: Internet
Author: User

When a single application is already unable to cope with the increasing volume of traffic, we tend to use distributed deployment, where the next single-Nginx combined with multiple tomcat for single-application deployment:

1, using upstream, in the Nginx nginx.conf file add the following configuration: http {# ..... Other configurations are omitted here upstream  abc.test.com  {server   192.168.0.1:8081 weight=1 max_fails=2 fail_timeout=30s; Server   192.168.0.1:8082 weight=1 max_fails=2 fail_timeout=30s;server   192.168.0.2:8081 weight=1 max_fails= 2 fail_timeout=30s;server   192.168.0.2:8082 weight=1 max_fails=2 fail_timeout=30s;}    # ..... This omits other configuration} 2, adds a server, corresponds to the domain of the application that is visited, such as www.test.comserver{                Listen 80;                server_name www.test.com;        &NB Sp       CharSet utf-8;                location/{    &NBSP ;                   Proxy_pass http://abc.test.com;                                  &NBS P  }         3, 2 tomcat installed on 192.168.0.1 and 192.168.0.2 two servers, ports 8081,80824, restart Nginx, configuration effective nginx-s RELOAD5, the above configuration, the entire system can operate normally, but our program will often have to obtain the user's real IP needs, But based on the above configuration can only get to the intranet address 192.168.0.1 and 192.168.0.2 to get the real IP address, you need to change the configuration of the second step to: Server{listen 80;server_name Www.test.com;charset Utf-8;location/{Proxy_pass http://abc.test.com;    proxy_set_header x-forwarded-for $proxy _add_x_ Forwarded_for;}}   Also in Java applications this gets: Request.getheader ("X-forwarded-for"), 6, at this time, if the system calls Request.getservername (), the return is also the intranet IP, Instead of www.test.com, you need to add the following configuration Proxy_set_header Host $host; Note: This can be returned correctly when Nginx and Tomcat are on a single server.

Nginx Balanced Multi-tomcat environment configuration, and REMOTEIP and servername access in this environment

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.