Nginx Reverse Proxy forwarding Tomcat

Source: Internet
Author: User
Tags tomcat server nginx reverse proxy

Recently contacted Nginx, on the Internet to check the relevant information, see the most describe Nginx word is reverse proxy, what is the reverse agent. has been not very understanding, so in the online check the reverse agent, the following gives Baidu Encyclopedia of the interpretation of the reverse agent.


Baidu Encyclopedia explains: Reverse proxy (Reverse proxy) means to accept a connection request on the Internet with a proxy server, then forward the request to a server on the internal network and return the results from the server to the client requesting the connection on the Internet, At this point, the proxy server behaves as a reverse proxy server.


Through the above explanation it is easy to understand the meaning of Nginx reverse proxy, that is to say, Nginx is equivalent to proxy server. The client makes the request, not directly by the Tomcat server to receive processing, but first after Nginx,nginx to the Tomcat server, Tomcat processing completed return results, but also after the Nginx,nginx back to the client. Throughout the process, Nginx seemingly acted as a real server, in fact, is not, nginx just played the role of forwarding.


According to the characteristics of the reverse agent, we can not need server processing of static resources (HTML,JS,CSS, etc.) directly let the nginx processing, JSP and so let the server processing, so as to reduce the pressure on the server.


Here's an example of using the Nginx agent to forward Tomcat:

1. Environment configuration

Installation Nginx, installation steps can refer to: Linux system under the Nginx installation

Installation Tomcat,tomcat installation is very simple, direct compression package decompression on the line, here is no longer elaborate.


2. Operating Environment

After the Nginx installation is complete and started, you can access the IP address of the Linux system directly in the browser, as follows:


3. Configuration resolution

See this is not very curious, why direct input IP can enter Nginx, where is configured. Look at the Nginx configuration file, the general default in the Nginx folder has a Conf folder, which has a nginx.conf, open found inside a section of the following configuration:


This code is in the configuration file in the server, a server equivalent to a proxy server, you can configure more than one server.

The meaning of several attributes inside is:

Listen: Represents the access port number for the current proxy server, and the default is port 80. If you are configuring multiple servers, the default port here needs to be changed, or the system will not know which agent service to enter.

SERVER_NAME: Represents the address that the proxy service needs to forward, and the default is localhost.

Location: Indicates the path where the client sends the request, where "/" can match all of the requested paths.

Root: When the request is not matched, the file is found within this folder, and root is important for the subsequent processing of static resources.

Index: If the agent does not specify a home page, it will default to the index configuration to find the home page, you can configure multiple, the first home page cannot be found, access the second, and so on.

Error_page: Represents the related error page that was entered after the error, and the following location is also a related configuration for handling errors.


PS: Just said nginx.conf path in the installation can be configured through the –conf-path=, I installed the time specified the "/etc/nginx/nginx.conf" this path. But I also find the configuration file under the default path, but I do find it, but the Nginx service does not use this, but the configuration file I specified when I installed it. So in the back of the operation on the tragedy, how to change the configuration file did not respond to the beginning with why the local configuration of the wrong, wasted hours of time, so here to remind you. Off the topic, the next thing to say.


3. Configure forwarding Tomcat

The forwarding tomcat configuration is simple enough to modify two places on the original configuration, as follows:


The Proxy_pass property configured here represents the proxy path, and Nginx can be forwarded directly to the link.

PS: Only one server is configured, server_name is optional and the system does not load the configuration. With multiple server scenarios, server_name must be configured, and the Nginx service will match the configuration.

After modifying the configuration, do not focus on nginx service, how. There's something to configure. Of course not, nginx support hot deployment, that is, after modifying the configuration file, do not need to restart the service to take effect, just let nginx reload it, it is convenient. If you want to check the configuration file for syntax errors before loading the configuration, you can enter "Nginx-t" to check for errors. As follows:


Next refresh the page that just visited Nginx, found that the page has become the Tomcat we want to forward, the page is as follows:


The page effect is the same as the effect we entered directly into the "http://172.16.21.92:8080/".


4. Dynamic and static resource separation

It said that according to the Nginx reverse proxy characteristics, you can achieve client access to static resources, do not request a Tomcat server, reduce server pressure. To implement this functionality the same needs to be configured for the NGINX.CONF server configuration, as follows:


The code above adds a location configuration that filters static files, and when the client requests these static files, Nginx forwards them to the "/usr/local/root/" folder (the ROOT folder is copied from the Tomcat's WebApps), You do not need to request a server.

Then refresh access to Nginx page, found that the page did not change, but click on the tomcat Home "Security Considerations HOW-TO" connection, found the report 404 error, the following figure:


Why, then. The reason is that this HTML page is under the Docs folder in Tomcat's WebApps, but we configured the static resource forwarding path to the root folder, Nginx couldn't find the HTML file, so we reported 404 errors. It also verifies that the configuration of the static resources and server separation is not a problem.

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.