Nginx the configuration tutorial for the Tomcat server as a reverse proxy _nginx

Source: Internet
Author: User
Tags java web tomcat server

The server on the Web is called Web server, but there is a different division of labor.

Nginx is often used to do static content services and proxy servers (not you FQ that agent), directly to the external request forwarding to the subsequent application services (Tomcat,django), Tomcat more used to do an application container, let Java Web App running inside the things, corresponding to the same level of jboss,jetty and other things.

But there is no absolute, Nginx can also provide the application function through the module development, Tomcat also can provide the HTTP service directly, usually uses in the intranet and does not need the flow control and so on small service scene.

Apache is less used, and generally has more overlap with nginx functions.

Strictly speaking, Apache/nginx should be called "http server", while Tomcat is a "application server", or, more accurately, a "servlet/jsp" application container (Ruby/python and other language development applications can not run directly on Tomcat.

An HTTP Server is concerned with the HTTP protocol level of transmission and access control, so on the apache/nginx you can see the agent, load balancing and other functions. The client accesses the resources stored on the server (HTML files, picture files, and so on) through the HTTP server. With CGI, the processed content can also be distributed through HTTP server, but an HTTP server always transmits the files on the server to the client through the HTTP protocol.

And the application server is a container for application execution. It first needs to support the Runtime of the development language (for Tomcat, Java) to ensure that the application works on the application server. Second, you need to support the application of relevant specifications, such as class libraries, security features. For Tomcat, it is necessary to provide jsp/sevlet operation needs of the standard class library, Interface, and so on. For convenience, application servers tend to integrate the functions of HTTP server, but not as powerful as the professional HTTP server, so the application server is often running behind the HTTP server, executing applications, converting dynamic content to static content, via HTTP Ser Ver is distributed to clients.

When it comes to reverse proxies, many people may have heard of it, but what is the reverse agent, many people are not clear. Pick a section on Baidu Encyclopedia Description: Reverse proxy (Reverse proxy) means that the proxy server to accept connection requests on the Internet, and then forward the request to the internal network of servers, and the results from the server returned to the Internet requesting the connection of the client, At this point, the proxy server behaves as a server.
It's a straightforward speech. The reverse proxy approach is actually a forwarding proxy server, seemingly acting as a real server, but not in fact, the proxy server only acts as a forwarding function, and from the real server to get the returned data. That said, in fact, Nginx finished is such a job. We let Nginx listen to a port, such as Port 80, but we actually forwarded to Tomcat at Port 8080, which handles the real request, and when the request completes, Tomcat returns, but the data is not returned directly at this time, but directly to Nginx, which is returned by Nginx. We would have thought it was nginx, but it was Tomcat that actually handled it. Speaking of the above way, perhaps a lot of people will remember, this can be static files to Nginx to deal with. Yes, a lot of nginx are used as static servers, which makes it easy to cache static files, such as css,js,html,htm files. Gossip is not much to say, we directly look at how nginx use.
1 to use the software of course to download it. To Nginx's official website next. Http://nginx.org/en/download.html can come down here. I now use the version is 1.1.7, but basically later versions are compatible, and we use does not involve too low-level, should not have any changes. Here, because I am windows, of course, the Windows version. The first thing to start after the end. Enter into the Nginx folder, direct start Nginx OK. For example I downloaded after D:\software\developerTools\server\nginx-1.1.7, directly cmd after CD D:\software\developerTools\server\ nginx-1.1.7, some are not accustomed to the command line may be strange, it did not proceed to that folder. Windows does not jump between partitions unless you specify it yourself. So we have to direct D: as follows:

And then we go straight to start nginx, and here you may see a window flashing through, and according to our experience with Tomcat, if we flash past it, it turns out to be wrong, right? But not really.

When we open Task Manager, we can see two nginx.exe there. This means that we have started, as to why two, we do not delve here.

Now that we've started Nginx, Tomcat can be started, and you can directly access Tomcat by thinking about direct access to http://localhost. First of all, let's take a look at the Nginx after startup. Direct access http://localhost can see:

We can see that Nginx started successfully, now access is directly into the Nginx directory. So where are these actually configured. This involves nginx an important configuration file nginx.conf.
2 we can see the Nginx folder has a Conf folder, which has several files, others first, we open nginx.conf, you can see a paragraph:

This code in the server, the equivalent of a proxy server, of course, can be configured more than one. Let's take a closer look at the following: Listen: Represents the port that the current proxy server listens on, and the default is to listen on port 80. Note that if we have more than one server configured, the listen will be configured differently, or we won't be sure where to turn. SERVER_NAME: Say where you need to go when you hear the monitor, and then we go directly to the local, and then directly to the Nginx folder. Location: Indicates a matching path, at which time configuration/means that all requests are matched to here root: This is configured with root, which means that when matching the path of this request, it will look for the corresponding file in this folder, which is useful for our static file servo. Index: When no home page is specified, the specified file is selected by default, it can have multiple, and is loaded in order, if the first does not exist, the second is found, and so on. The following error_page is the page that represents the error, here we do not need to, regardless of it.
So we know the specifics of the configuration, how to get it to go to Tomcat when it accesses localhost. Two places were actually modified:

server_name localhost:8080;  
Location/{  Proxy_pass http://localhost:8080} 

We have modified the top two places, my Tomcat in 8080 ports, can be modified according to their own needs. Here is a new element, Proxy_pass, that represents the proxy path, which is equivalent to forwarding, rather than having to specify a folder as previously said Root. At this time we modified the file, is not meant to have to close the nginx before restarting, in fact, Nginx can reload the file. We run directly: Nginx-s Reload
Too early to be happy, we found a mistake:

What come, 45 line found error, do not want to find in that line, so we look carefully, found that we joined the Proxy_pass very strange, no; the end of the number, this is the problem, directly modify, and then run again, found no error, OK. If you do not want to load directly, but just want to see your profile has no problem, you can directly enter: NGINX-T
This can check for errors in the configuration file. All of our modifications below assume that we have run nginx-s reload to reload the configuration file after the modification is complete, please note.
Everything's okay, and then we reopen http://localhost, and we see the following page:

At this time, we found that it is not just the Welcome page, but Tomcat management page, no matter what we click on the link is no problem, the equivalent of direct access to http://localhost:8080.
3 above we directly tried a small example, let Nginx forward, that is, the so-called reverse proxy. But in fact our requirements will not be the case, we need to filter the file type, such as JSP directly to Tomcat processing, because Nginx is not a servlet container, can not handle JSP, and html,js,css these do not need to deal with, directly to Nginx cache. Let's take a look at the configuration, let the JSP page directly to Tomcat, and html,png, such as some pictures and JS, such as directly to the Nginx cache. At this time the main use of the location this element, and involves a part of the regular, but not difficult:

Location ~. jsp$ {    proxy_pass http://localhost:8080;}  
Location ~. (html|js|css|png|gif) $ {  root d:/software/developertools/server/apache-tomcat-7.0.8/webapps/root;} 
Location ~. jsp$ {    proxy_pass http://localhost:8080;} 
Location ~. (html|js|css|png|gif) $ {  root d:/software/developertools/server/apache-tomcat-7.0.8/webapps/root;} 

First we need to remove the location/to avoid all requests being intercepted. And then we'll take a look at http://localhost.

When we do not specify the JSP page, it will appear not found, because there is no corresponding location match, so there will be 404 errors, then jumped to the Nginx custom error page. And when we use http://localhost/index.jsp to visit, we see the familiar page:

And the pictures are all normal, because the picture is PNG, so directly in the Tomcat/webapps/root directory to find directly, of course, if we click on the manager application how-to this link, we found:

It is still not found, why? Because this is an HTML page, but it is not in the root directory, but in the Docs directory, but when we match the HTML, we go down to the root directory, so we still can not find this page.
In general, if we need to use Nginx for static file servo, usually all static files, HTML,HTM,JS,CSS, etc. are placed under the same folder, so there will be no tomcat such situation, because Tomcat is under the different projects, There's no way we can do that.
3 Some people will say, these will only find a server, but if we want to be in a server to hang, automatically to find another one, this How to do? This is actually nginx to consider. At this point, we used the Proxy_pass will have a large use. We put the first example, that is, all the agents of the modification: the last modification is as follows:

Upstream Local_tomcat {  server localhost:8080;}  
server{    location/{      proxy_pass http://local_tomcat;    }  
  ##...... Other ellipsis
}  
upstream Local_tomcat {  server localhost:8080;} 
server{    location/{      proxy_pass http://local_tomcat;    } 
  #...... Other ellipsis
} 

We added a upstream outside the server and used the Http://+upstream name directly inside the Proxy_pass. We still directly to http://localhost, or the first and the same effect, all links are no problem, indicating that we are configured correctly. The server element in the upstream must be aware that it cannot be added http://, but it must be added in Proxy_pass. We just said that you can connect to another one when a server hangs up, so how do you do that? In fact, it's very simple to configure one more server in the Local_tomcat in upstream. For example, I now get more than one jetty, port in 9999, so we configure the following:

Upstream Local_tomcat {  server localhost:8080;  Server localhost:9999;}  
Upstream Local_tomcat {  server localhost:8080;  Server localhost:9999;} 

At this point, we shut down Tomcat and only open jetty. Let's run http://localhost look at the effect: we saw it request to the Jetty page, but due to the jetty mechanism, this time does not show Jetty homepage, this we first ignore. But we are automatically using another function in the case of a server hanging.
But sometimes we don't want it to go to the other, but just want a server to access more than another, this can be at the end of the server with a weight= number to specify, the larger the number, indicating the greater the chance of the request.

Upstream Local_tomcat {  server localhost:8080 weight=1;  Server localhost:9999 weight=5;}  
Upstream Local_tomcat {  server localhost:8080 weight=1;  Server localhost:9999 weight=5;} 

At this time we gave jetty a higher weight, let it have more access to, in fact, when we refresh http://localhost access to find jetty access is much greater, Tomcat almost no chance to visit, in general, if we have to use this, do not relate too much, Lest one server load too much. Of course, there are other elements of the server, such as down that temporarily do not need to the server and so on. These can refer to Nginx's wiki. Perhaps wrote a lot of, some people will have the question, that nginx how closes? This is a problem, in fact, directly run Nginx-s stop can be closed.

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.