Tomcat cluster Load Balancing Based on Nginx in Ubuntu

Source: Internet
Author: User
Tags superuser permission nginx load balancing
Nginx is an HTTP and reverse proxy server. You can search for it on the Internet. Here, we will record the Nginx installation process and how to configure Nginx to achieve Load Balancing for Tomcat clusters. The basic idea is that we now have a Web search server implemented in Java. Users can enter keywords on the Web page, and the search server can process the search request and display the search results to users. If the user traffic is high, our search server will be under great pressure.

Nginx is an HTTP and reverse proxy server. You can search for it on the Internet. Here, we will record the Nginx installation process and how to configure Nginx to achieve Load Balancing for Tomcat clusters.

Basic Ideas

If we have a Web search server implemented in Java, you can enter keywords on the Web page, and the search server processes the search requests and displays the search results to users. If the user traffic is high, the search server will be under great pressure, probably because the processing capacity of the search server has reached the upper limit, you cannot process new user requests at a certain time. Therefore, we will consider distributing the pressure on user requests, that is, deploying the same search server program on multiple servers, and then using a load balancing policy, the request pressure is distributed across multiple search servers. In this way, when the user request volume is large, the problem that a single server cannot process requests is well solved.

Our idea is to use a server as a proxy, use Server Load balancer software to forward requests, and forward users' requests to multiple search servers for processing, the server Load balancer of multiple search servers can be achieved without a single server processing all requests.

Assume that we have three machines and each server is as follows:

  1. Search server: 192.168.0.174 RHEL 5
  2. Search server: 192.168.0.181 Win 7
  3. Proxy Server: 192.168.0.184 Ubuntu 11.04.1

Install Nginx on 192.168.0.184 as a reverse proxy. The other two servers are search servers, and Tomcat Web server software is installed. The search server program is deployed in Tomcat. The server 192.168.0.184 receives the search request, www.linuxidc.com forwards the request to the two search servers through Nginx for processing, and then returns the result, which is returned through the Nginx proxy.

Configure Resources

The software configuration and application port of each server are described as follows:

Server IP: Port

Software Configuration

192.168.0.174: 8080 OpenJDK 1.6.0 _ 22, apache-tomcat-7.0.22.tar
192.168.0.181: 8080 Sun JDK 1.6.0 _ 17, apache-tomcat-6.0.20.exe
192.168.0.184: 8888 Nginx-1.0.8.targz, pcre-8.13.tar.gz

Installation and configuration process

  • Nginx Installation

Download nginx-1.0.8.tar gz, The pcre-8.13.tar.gz of the two installation packages, and decompress to the directory/home/shirdrn/tools, and then the installation process is as follows:

  1. Cd/home/shirdrn/tools
  2. Tar-xvf pcre-8.13.tar.bz2
  3. Tar-xzvf nginx-1.0.8.tar.gz
  4. Cd/home/shirdrn/tools/nginx-1.0.8
  5. ./Configure -- with-http_stub_status_module-- Prefix=/Home/ubuntu/servers/nginx-- With-pcre=/Home/ubuntu/tools/pcre-8.13
  6. Make
  7. Make install

To execute the above command, you need to use the superuser permission to install our Nginx under the/home/shirdrn/servers/nginx-1.0.8, since -- with-pcre =/home/ubuntu/tools/pcre-8.13 is specified, that is, the source code path of pcre, compile and install pcre during installation, and then start configuring and installing Nginx.

To verify whether the installation is successful, you only need to enter http: // 192.168.0.184: 8888/in the browser. By default, Nginx uses port 80, where port 80 is occupied, so modify it to 8888 (the basic configuration of Nginx is described below ).

  • Ngin Server Load balancer Configuration

Next, let's take a look at the configuration of implementing Nginx load balancing. The configuration file is conf/nginx. conf, because of our proxy configuration, by using a separate proxy configuration file conf/proxy. conf in conf/nginx. introduce the proxy configuration in conf.

The configuration of conf/proxy. conf is as follows:

  1. Proxy_redirect off;
  2. Proxy_set_header Host $ host;
  3. Proxy_set_header X-Real-IP $ remote_addr;
  4. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  5. Client_max_body_size 10 m;
  6. Client_body_buffer_size 128 k;
  7. Proxy_connect_timeout 300;
  8. Proxy_send_timeouts 300;
  9. Proxy_read_timeout 300;
  10. Proxy_buffer_size 4 k;
  11. Proxy_buffers 4 32 k;
  12. Proxy_busy_buffers_size 64 k;
  13. Proxy_temp_file_write_size 64 k;

For more information about the meaning of each configuration item, see the relevant documentation. For details about the configuration of conf/nginx. conf in www.linuxidc.com, refer to the following figure:

  1. User root; # users and user groups of Nginx
  2. Worker_processes 3; # Number of dynamic worker Processes
  3. # Error_log logs/error. log;
  4. # Error_log logs/error. log notice;
  5. # Error_log logs/error. log info;
  6. Pid logs/nginx. pid; # Nginx process ID
  7. Events {
  8. Worker_connections 1024;
  9. }
  10. Http {
  11. Include mime. types;
  12. Default_type application/octet-stream;
  13. # Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
  14. # '$ Status $ body_bytes_sent "$ http_referer "'
  15. # '"$ Http_user_agent" "$ http_x_forwarded_for "';
  16. # Access_log logs/access. log main;
  17. Sendfile on;
  18. # Tcp_nopush on;
  19. # Keepalive_timeout 0;
  20. Keepalive_timeout 65;
  21. # Gzip on;
  22. Upstream localhost {# the request sent to localhost is forwarded to the server that actually processes the request through Nginx.
  23. Server 192.168.0.181: 8080Weight=1;
  24. Server 192.168.0.184: 8080Weight=1;
  25. }
  26. Server {
  27. Listen 8888; # The Nginx listening port. The default value is 80.
  28. Server_name localhost; # Name of the host where Nginx is located
  29. # Charset koi8-r;
  30. # Access_log logs/host. access. log main;
  31. Location /{
  32. Root html/solr; # path of the requested resource (AGENT:/home/ubuntu/servers/nginx/tml/solr/, there is no data in this directory)
  33. Index index.html index.htm;
  34. Proxy_pass http: // localhost; # Proxy: proxy for requests sent to localhost
  35. Include proxy. conf; # introduce proxy. conf configuration
  36. }
  37. # Error_page 404/404 .html;
  38. # Redirect server error pages to the static page/50x.html
  39. #
  40. Error_page 500 502 503 x.html;
  41. Location=/50x.html {
  42. Root html;
  43. }
  44. # Proxy the PHP scripts to Apache listening on 127.0.0.1: 80
  45. #
  46. # Location ~ \. Php $ {
  47. # Proxy_pass http: // 127.0.0.1;
  48. #}
  49. # Pass the PHP scripts to FastCGI server listening on Fig: 9000
  50. #
  51. # Location ~ \. Php $ {
  52. # Root html;
  53. # Fastcgi_pass 127.0.0.1: 9000;
  54. # Fastcgi_index index. php;
  55. # Fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name;
  56. # Include fastcgi_params;
  57. #}
  58. # Deny access to. htaccess files, if Apache's document root
  59. # Concurs with nginx's one
  60. #
  61. # Location ~ /\. Ht {
  62. # Deny all;
  63. #}
  64. }
  65. # Another virtual host using mix of IP-, name-, and port-based configuration
  66. #
  67. # Server {
  68. # Listen 8000;
  69. # Listen somename: 8080;
  70. # Server_name somename alias another. alias;
  71. # Location /{
  72. # Root html;
  73. # Index index.html index.htm;
  74. #}
  75. #}
  76. # HTTPS server
  77. #
  78. # Server {
  79. # Listen 443;
  80. # Server_name localhost;
  81. # Ssl on;
  82. # Ssl_certificate cert. pem;
  83. # Ssl_certificate_key cert. key;
  84. # Ssl_session_timeout 5 m;
  85. # Ssl_protocols SSLv2 SSLv3 TLSv1;
  86. # Ssl_ciphers HIGH :! ANULL :! MD5;
  87. # Ssl_prefer_server_ciphers on;
  88. # Location /{
  89. # Root html;
  90. # Index index.html index.htm;
  91. #}
  92. #}
  93. }
Related Article

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.