Build Nginx+java Environment

Source: Internet
Author: User

Build nginx+JAVAEnvironment

Apache's support for Java is flexible, and their degree of integration is high, such as Apache+tomcat and Apache+resin, which enable Java applications to be supported. Apache typically uses a built-in module to deal with Java application servers. Compared to Apache, Nginx in conjunction with the Java application Server, the coupling is very low, it can only through its own reverse proxy function to achieve with the Java Application Server support, this is the nginx one of the advantages, the reduction of coupling degree, The interaction between Nginx and Java server can be minimized.

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 Tmcat supports Epoll, it is much better to handle static pages through Nginx than with Tomcat.

    • Nginx can be used in two ways to achieve the coupling with Tomcat.
    • 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. Separate instances of dynamic pages from static pages

This assumes that the Tomcat server has an IP address of 192.168.12.130, while the Tomcat server opens the service port for the 8080,nginx-related configuration code as follows:

server {listen; server_name www.ixdba.net; root/web/www/html; location/img/{alias /web/www/html/img/; } location ~ (\.jsp) | (\.do) $ {proxy_pass http://192.168.12.130:8080; proxy_redirect off; Proxy_set_header host  $host; Proxy_set_header X-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Client_max_body_size 10m; Client_body_buffer_size 128k; Proxy_connect_timeout 90; Proxy_send_timeout 90; Proxy_read_timeout 90; Proxy_buffer_size 4k; Proxy_buffers 4 32k; Proxy_busy_buffers_size 64k; Proxy_temp_file_write_size 64k; } } 

In this example, a virtual host www.ixdba.net is defined first, then the static files under the/web/www/html/img/directory are given to Nginx via the location command, and the last location command will all be. JSP,. Do end files are handed to the Tomcat server on port 8080 for processing, i.e. 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. Multiple Tomcat Load Balancer instances

This assumes that there are three Tomcat servers, each opening different ports, respectively:

The relevant configuration code for Nginx is as follows:

Upstream Mytomcats {server 192.168.12.131:8000; server 192.168.12.132:8080; server 192.168.12.133:8090;} server {Listen 80; server_name www.ixdba.net; Location ~* \. (jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|Zip|rar) $ {root/web/www/html/;} location/{Proxy_pass http://mytomcats; proxy_redirect off; Proxy_set_header Host $host; Proxy_set_header X-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Client_max_body_size 10m; Client_body_buffer_size 128k; Proxy_connect_timeout 90; Proxy_send_timeout 90; Proxy_read_timeout 90; Proxy_buffer_size 4k; Proxy_buffers 4 32k; Proxy_busy_buffers_size 64k; Proxy_temp_file_write_size 64k; } }

In this example, a Load Balancer group is defined first through upstream, and the group name is Mytomcats, and the members of the group are the three 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 disposed to the Load Balancer group.

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

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.