Nginx and Tomcat combine to build Web services

Source: Internet
Author: User

Part of the content transferred from http://www.cnblogs.com/naaoveGIS/

1. Web Services

Nginx is a common Web server used to obtain static resources, similar servers and Apache.

Tomcat is a Java servlet-based Web container for accessing dynamic resources.

Generic Web Services Architecture: Front-end deployment nginx, back-end deployment Tomcat. The user accesses Nginx, the static resource Nginx returns directly, the request of the dynamic resource is Nginx forwarded to the Tomcat,tomcat to return the finished structure to Nginx, then to the browser.

2.Nginx Introduction

Nginx ("Engine X") is a high-performance HTTP and reverse proxy server as well as a IMAP/POP3/SMTP server. Nginx was developed by Igor Sysoev for the second rambler.ru site of Russian traffic.

What is a reverse proxy? That is, the proxy server accepts the connection request on the Internet, then forwards the request to the server on the internal network, and returns the results from the server to the client requesting the connection on the Internet, where the proxy server appears as a server externally.

And Nginx is the main completion of the work is reverse proxy, for example, we send a request to a server 80 port, the request is first monitored by nginx, it received the request content and then forwarded to other servers, the other server processing and then send the results to Nginx, Finally, the nginx is returned uniformly to the initial request side.

Nginx is the most common way to use as a static resource server, such as the css,js,html,htm files such as cache processing.

3. Environment Construction 3.1 Downloads

First download the required version in the official website: http://nginx.org/en/download.html. The version I am currently using is 1.9.15 in the Windows environment.

After the download is complete, unzip.

3.2 Open Nginx

There are two ways to open nginx, one is a direct click to open, one is named line open.

A. Click to open:

B. Name Line open:

When successful, you can see in resource management:

3.3 FAQ-Startup failure

A boot failure issue, typically a port conflict issue, such as IIS under Win7, has been started and occupies 80 ports. Locate the nginx.conf in the Config directory and open the file with the following modifications:

4. Initial use of 4.1 test whether to start successfully with Web page

Where the configuration is:

After listening to the 8010 port request, it will first go to the HTML folder to look for the index.html page.

4.2 Explanation of part of the parameter meaning

Listen: Indicates the port on which the current proxy server listens, and the default is to listen on port 80. Note that if we have more than one server configured, this listen will be configured differently, or we will not be sure where to go.

SERVER_NAME: to indicate where to go when the supervisor hears, we go directly to the local, then directly to the Nginx folder.

Location: Represents the matching path, at which point all requests are configured/indicated to be matched here

Root: The root is configured in this means that when matching the path of this request, will be in this folder to find the corresponding file, here is useful for our static file servo.

Index: When the home page is not specified, the specified file is selected by default, it can have multiple, and is loaded sequentially, if the first one does not exist, then the second one is found, and so on.

4.3 Configuring the Tomcat jump

The request forwarding address is configured via Proxy_pass. That is, when you access port 8010 on localhost, the request jumps to port 8080 on localhost. After modifying the Nginx configuration, do not need to restart Nginx, enter the following command:

The test is as follows:

5. Further use of 5.1 static file separation configuration

Above we directly tried a small example, let Nginx forward, that is, the so-called reverse proxy. But in fact our needs will not be so, we need to sub-file types to filter, such as JSP directly to Tomcat processing, because Nginx is not a servlet container, no way to handle JSP, and html,js,css these do not need to deal with, directly to Nginx cache.

Next, let's configure the JSP page directly to Tomcat, and html,png and so on some pictures and JS and so on directly to the Nginx cache.

At this point the most important is the location of this element, and involves a part of the regular, but not difficult, the specific configuration is as follows:

When you enter localhost:8010 directly, you will report 404 errors. But entering localhost:8010/index.jsp, you can jump to the Tomcat home page as well.

5.2 Load Configuration

When a server fails, we need to automatically turn the request to another server, how is this requirement configured? The use of Nginx is also more convenient to achieve, the specific configuration is as follows:

Note that the above also adds a weight property, which represents the weight that each server is accessing, and the higher the probability of weight access.

5.3 Other configurations

5.4 Close Nginx

Nginx and Tomcat combine to build Web services

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.