Build Nginx+java Environment (turn)

Source: Internet
Author: User

1.8.4 Build Nginx+java Environment

Apache support for Java is flexible, and they are highly associative, such as Apache+tomcat and Apache+resin, to support Java applications. Apache typically uses a built-in module to deal with Java application servers. Compared with Apache, Nginx has a low degree of coupling with Java application servers, and it can only support Java application Server through its own reverse proxy function. But this is exactly the one of the advantages of nginx, the reduction of coupling, can make nginx and Java server interaction to minimize.

Next, we will explain the support of Nginx to Java by Nginx+tomcat's example. Tomcat handles dynamic requests in highly concurrent environments with low performance and is more vulnerable to handling static pages. Although the latest version of Tomcat supports Epoll, it is much better to handle static pages through Nginx than with Tomcat.

Nginx can be coupled with Tomcat in the following two ways:

The static page request is given to Nginx, and the dynamic request is given back-end tomcat processing.

Handle all requests to the backend Tomcat server while using Nginx's own load balancing function to load balance multiple tomcat servers.

The two ways to implement Nginx-tomcat coupling are described in the following two configuration examples.

1. Instances of dynamic pages separated from static pages

This assumes that the Tomcat server has an IP address of 192.168.12.130, while the Tomcat server has an open server port of 8080. Nginx related configuration code is as follows:

  1. server {
  2. Listen 80;
  3. server_name www.ixdba.net;
  4. root/web/www/html;
  5. location/img/{
  6. alias/web/www/html/img/;
  7. }
  8. Location ~ (\.jsp) | (\.do) $ {
  9. Proxy_pass http://192.168.12.130:8080;
  10. Proxy_redirect off;
  11. Proxy_set_header Host $host;
  12. Proxy_set_header X-real-ip $remote _addr;
  13. Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
  14. Client_max_body_size 10m;
  15. Client_body_buffer_size 128k;
  16. Proxy_connect_timeout 90;
  17. Proxy_send_timeout 90;
  18. Proxy_read_timeout 90;
  19. Proxy_buffer_size 4k;
  20. Proxy_buffers 4 32k;
  21. Proxy_busy_buffers_size 64k;
  22. Proxy_temp_file_write_size 64k;
  23. }
  24. }

In this example, a virtual host www.ixdba.net is defined first, and then the static files under the/web/www/html/img/directory are given to Nginx via the location command. The last location directive gives all files that end in. JSP,. Do, to the Tomcat server's port 8080 for processing, which is http://192.168.12.130:8080.

It is important to note that after using a regular expression in the location directive, the proxy path after Proxy_pass cannot contain an address link, that is, cannot be written as http://192.168.12.130:8080/, or something like HTTP/ The form of 192.168.12.130:8080/jsp. There is no such restriction when the location directive does not use regular expressions.

2. Instances of multiple Tomcat load balancing

This assumes that there are 3 tomcat servers, each opening different ports, with the following address:

    1. 192.168.12.131:8000
    2. 192.168.12.132:8080
    3. 192.168.12.133:8090

The relevant configuration code for Nginx is as follows:

  1. Upstream Mytomcats {
  2. Server 192.168.12.131:8000;
  3. Server 192.168.12.132:8080;
  4. Server 192.168.12.133:8090;
  5. }
  6. server {
  7. Listen 80;
  8. server_name www.ixdba.net;
  9. Location ~* \. (Jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar) $ {
  10. root/web/www/html/;
  11. }
  12. Location/{
  13. Proxy_pass Http://mytomcats;
  14. Proxy_redirect off;
  15. Proxy_set_header Host $host;
  16. Proxy_set_header X-real-ip $remote _addr;
  17. Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
  18. Client_max_body_size 10m;
  19. Client_body_buffer_size 128k;
  20. Proxy_connect_timeout 90;
  21. Proxy_send_timeout 90;
  22. Proxy_read_timeout 90;
  23. Proxy_buffer_size 4k;
  24. Proxy_buffers 4 32k;
  25. Proxy_busy_buffers_size 64k;
  26. Proxy_temp_file_write_size 64k;
  27. }
  28. }

In this example, a Load balancer group is defined by upstream, the group name is Mytomcats, and the members of the group are the 3 tomcat servers specified above Then, a www.ixdba.net virtual host is defined by the Server directive, and then the file of the specified type is given to Nginx by the location instruction in the form of a regular expression, and all other requests are finally handed over to the Load Balancer group for processing.

It is also important to note that Nginx is not supported if you use the alias command after the location instruction uses the regular expression.

Build Nginx+java Environment (turn)

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.