Nginx + Tomcat server Load balancer Configuration

Source: Internet
Author: User

Nginx + Tomcat is currently the mainstream Java Web architecture. How can we make nginx + Tomcat work at the same time? Can we also say how to use nginx to reverse proxy Tomcat backend balancing? Let's take a look at it in detail.

Nginx + Tomcat is currently the mainstream Java Web architecture. How can we make nginx + Tomcat work at the same time? Can we also say how to use nginx to reverse proxy Tomcat backend balancing? Directly install the configuration as follows:


1. install Java JDK:

 
 
  1. # Download the appropriate JDK software package, and then unzip the installation, here I package name: jdk-7u25-linux-x64.tar.gz
  2. Tar-xzf jdk-7u25-linux-x64.tar.gz; mkdir-P/usr/Java/; MV jdk1.7.0 _ 25 // usr/Java.
  3. # Configure the environment variable so that JDK can be referenced anywhere. The configuration is as follows:
  4. # Add the following statement to the end of VI/etc/profile:
  5. Export java_home =/usr/Java/jdk1.7.0 _ 25
  6. Export classpath = $ classpath: $ java_home/lib: $ java_home/JRE/lib
  7. Export Path = $ java_home/bin: $ java_home/JRE/bin: $ path: $ homr/bin
  8. # Source/etc/profile # make the environment variable take effect immediately
  9. # JAVA -- version # Check the Java version. If JDK 1.7.0 _ 25 is displayed, the Java JDK is successfully installed.


2. nginx installation:

 
 
  1. Wget http://nginx.org/download/nginx-1.2.6.tar.gz
  2. Useradd WWW
  3. Tar zxvf nginx-1.2.6.tar.gz
  4. CD nginx-1.2.6/
  5. ./Configure -- user = WWW -- group = WWW -- prefix =/usr/local/nginx
  6. \ -- With-http_stub_status_module -- with-http_ssl_module
  7. Make & make install
  8. # After nginx is installed, run the command:/usr/local/nginx/sbin/nginx-t to test OK, which indicates that nginx is successfully installed.
  9. /Usr/local/nginx/sbin/nginx press enter to start nginx. You can access http: // ip/to view the default nginx page.


3. tomcat installation:

 
 
  1. # Download Tomcat 6.0.30 or another version from the official website:
  2. Apache-tomcat-6.0.30.tar.gz CD/usr/src & tar xzf
  3. # Directly decompress the package and run the package. Copy two Tomcat servers and name them tomcat1 tomcat2.
  4. Mv apache-Tomcat-6.0.30/usr/local/tomcat1
  5. CP/usr/local/tomcat1/usr/local/tomcat2-R
  6. # Modify the tomcat1 and tomcat2 ports respectively. The three ports must be modified as follows:
  7. Shutdown port: 8005 is mainly used to start and close.
  8. AJP port: 8009 is mainly used for AJP balancing (usually used for Apache and tomcat integration)
  9. Http port: 8080 can be directly accessed through web pages (nginx + tomcata integration)
  10. # Note * If the three port numbers of tomcat1 are 8005 8009 8080, port 8006 + 1, that is, port 8010 8081
  11. # On a server, the ports cannot be repeated; otherwise, an error is reported.
  12. # After modifying the port, start two Tomcat servers and run the following command:
  13. # How to prompt whether the file does not exist or the permission is insufficient? The bin directory of Tomcat needs to grant the execution permission to the sh file: chmod o + x *. Sh
  14. /Usr/local/tomcat1/bin/startup. Sh
  15. /Usr/local/tomcat2/bin/startup. Sh
  16. # After the instance is started, use netstat-tnl to view the six ports, that is, tomcat1 tomcat2 is successfully started. You can use http: // ip: 8080 http: // ip: 8081 to access the default Tomcat page.


# If you need to modify the Tomcat release directory as your own directory, you need to make the following adjustments to create two release directories:

 
 
  1. mkdir -p /usr/webapps/{www1,www2}


Edit VI/usr/local/tomcat1/CONF/server. xml and add the following content to the last line

 
 
  1. <Context path="" docBase="/usr/webapps/www1" reloadable="false"/>


2. Edit VI/usr/local/tomcat2/CONF/server. xml and add the following content to the line before the last

 
 
  1. <Context path="" docBase="/usr/webapps/www2" reloadable="false"/>


3. The release directory of tomcat1 is as follows:

 
 
  1. <body>
  2. <%=new java.util.Date()%>
  3. </body>


4. tomcat2 release directory content:

 
 
  1. <body>
  2. <%=new java.util.Date()%>
  3. </body>


Access http: // ip: 8080 and 8081 to view the test content.


5. nginx + Tomcat integration:


The integration mainly involves modifying the nginx. conf configuration and providing a complete nginx. conf online configuration. Some parameters can be modified as needed:

 
 
  1. User WWW;
  2. Worker_processes 8;
  3. Pid/usr/local/nginx. PID;
  4. Worker_rlimit_nofile 102400;
  5. Events
  6. {
  7. Use epoll;
  8. Worker_connections 102400;
  9. }
  10. HTTP
  11. {
  12. Include mime. types;
  13. Default_type application/octet-stream;
  14. Fastcgi_intercept_errors on;
  15. Charset UTF-8;
  16. Server_names_hash_bucket_size 128;
  17. Client_header_buffer_size 4 K;
  18. Large_client_header_buffers 4 32 K;
  19. Client_max_body_size 300 m;
  20. Sendfile on;
  21. Tcp_nopush on;
  22. Keepalive_timeout 60;
  23. Tcp_nodelay on;
  24. Client_body_buffer_size 512 K;
  25. Proxy_connect_timeout 5;
  26. Proxy_read_timeout 60;
  27. Proxy_send_timeout 5;
  28. Proxy_buffer_size 16 K;
  29. Proxy_buffers 4 64 K;
  30. Proxy_busy_buffers_size 128 K;
  31. Proxy_temp_file_write_size 128 K;
  32. Gzip on;
  33. Gzip_min_length 1 K;
  34. Gzip_buffers 4 16 K;
  35. Gzip_http_version 1.1;
  36. Gzip_comp_level 2;
  37. Gzip_types text/plain application/X-JavaScript text/CSS application/XML;
  38. Gzip_vary on;
  39. ### 2012-12-19 change nginx logs
  40. Log_format main '$ http_x_forwarded_for-$ remote_user [$ time_local] "$ request "'
  41. '$ Status $ body_bytes_sent "$ http_referer "'
  42. '"$ Http_user_agent" $ request_time $ remote_addr ';
  43. Upstream web_app {
  44. Server 127.0.0.1: 8080 Weight = 1 max_fails = 2 fail_timeout = 30 s;
  45. Server 127.0.0.1: 8081 Weight = 1 max_fails = 2 fail_timeout = 30 s;
  46. }
  47. #### Chinaapp.sinaapp.com
  48. Server {
  49. Listen 80;
  50. SERVER_NAME chinaapp.sinaapp.com;
  51. Index index. jsp index.html index.htm;
  52. # Release directory/data/WWW
  53. Root/data/WWW;
  54. Location/
  55. {
  56. Proxy_next_upstream http_502 http_504 error timeout invalid_header;
  57. Proxy_set_header host $ host;
  58. Proxy_set_header X-real-IP $ remote_addr;
  59. Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
  60. Proxy_pass http: // web_app;
  61. Expires 3D;
  62. }
  63. }
  64. }


# Note * The web_app defined in the server segment proxy_pass must be consistent with the web_app defined in upstream. Otherwise, the server cannot find a balance.


# In the above configuration, nginx + Tomcat Reverse Proxy Server Load balancer configuration is complete. If you want to perform dynamic/static separation, you only need to add the following configuration in nginx.

 
 
  1. # Configure nginx dynamic/static Separation
  2. Location ~ . * \. (HTML | HTM | GIF | JPG | JPEG | BMP | PNG | ICO | TXT | JS | CSS) $
  3. {
  4. Root/data/WWW;
  5. # Expires defines that the user's browser cache time is 3 days. If the static page is not updated frequently, you can set a longer period, which can save bandwidth and relieve server pressure.
  6. Expires 3D;
  7. }

Nginx + Tomcat server Load balancer Configuration

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.