Installation and configuration of NGINX+TOMCAT consolidation under Windows

Source: Internet
Author: User

Original posts: http://zyjustin9.iteye.com/blog/2017394

Believe that many people have heard nginx, this small thing slowly swallowing Apache and IIS share. So what does it do? Many people may not know.

Speaking of reverse proxy, many people may have heard, but the specific what is the reverse proxy, many people estimate is unclear. Pick a section of the Baidu Encyclopedia description:

HTML code
    1. The reverse proxy method refers to a proxy server that accepts connection requests on the Internet, then forwards the request to a server on the internal network and returns the results from the server to the client requesting the connection on the Internet, Reverse. At this point the proxy server is represented as a server externally.

This is a straightforward speech. The reverse proxy mode is actually a proxy server responsible for forwarding, seemingly acting as a real server function, but in fact not, the proxy server just acts as a forwarding role, and from the real server to obtain the returned data. In fact, this is the kind of work that Nginx has done. We let Nginx listen to a port, such as 80 port, but we actually forwarded to Tomcat on port 8080, which handles the real request, and when the request is complete, Tomcat returns, but the data is not returned directly, but directly to Nginx, returned by Nginx, here, We'll assume that it's nginx, but actually we're dealing with Tomcat.

Speaking of the above way, perhaps a lot of people will remember, so that the static files can be referred to nginx for processing. Yes, a lot of the use of Nginx is a static server, so it is convenient to cache those static files, such as css,js,html,htm and other files.

1) Website address: http://nginx.org/en/download.html can go down here.

Here, because my is windows, of course under the Windows edition. The first thing to do is start it. Into the Nginx folder, the direct start nginx OK.

For example I downloaded after the D:\software \developertools\server\nginx-1.1.7, direct cmd after the CD D:\software\developerTools \server\ nginx-1.1.7, some are not accustomed to the command line may be strange, it did not go to that folder. Windows does not jump between partitions unless you specify it yourself. So we want direct d: as follows:

Then we directly start Nginx, here maybe you will see a window flashed past, according to our experience of using Tomcat, if a flash past, prove that there is a mistake, right? But not really.

When we open the Task Manager, we can see two nginx.exe there. This shows that we have started, as for why two, we do not delve into.

Now, we have started the nginx, to see how the Nginx after the startup. Direct access to http://localhost can be seen:

We can see the Nginx boot success, now access is directly into the Nginx directory.

So where are these actually configured? This involves an important configuration file for Nginx nginx.conf.

2) We can see the Nginx folder has a Conf folder, which has several files, the other first, we open nginx.conf, we can see a paragraph:

This code in the server, the equivalent of a proxy server, of course, can be configured multiple.

Let's take a closer look at the following:

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.

The following error_page is a page that represents the error, which we do not use here for the moment, regardless of it.

So we know the exact configuration and how to get it to localhost when it goes to Tomcat. In fact, two places were modified:

Java code
    1. server_name localhost:8080;
    2. Location/{
    3. Proxy_pass http://localhost:8080;
    4. }

We have modified the above two places, my tomcat on the 8080 port, can be modified according to their needs. Here is a new element, Proxy_pass, which 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 shut down the nginx and then restarted, in fact, do not have, Nginx can reload the file.

We run directly:

HTML code
    1. Nginx-s Reload

If you do not want to load directly, but just want to see their own configuration file there is no problem, you can enter directly:

XML code
    1. Nginx-t

This can check for errors in the configuration file. All of our modifications below assume that we have modified to run the Nginx-s reload to reload the configuration file, please note.

Everything is fine, and then we reopen the http://localhost, and we see the following page:

At this point, we found that it is not just the Welcome page, but the Tomcat management page, no matter what we click on the link is no problem, equivalent to 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 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:

XML code
    1. Location ~ \.jsp$ {
    2. Proxy_pass http://localhost:8080;
    3. }
    4. Location ~ \. (html|js|css|png|gif) $ {
    5. Root D:/software/developertools/server/apache-tomcat-7.0.8/webapps/root;
    6. }

We need to get rid of the previous location/to avoid all requests being intercepted.

And then we'll look at http://localhost.

When we do not specify the JSP page, it will appear not found, because, there is no corresponding location matching, 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 pictures of those are shown normal, because the picture is PNG, so directly in the Tomcat/webapps/root directory directly find, of course, if we click on the manager application how-to this link, we found:

It still can't be 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 to the root directory to find, so still can't find this page.

In general, if we need to use Nginx static file servo, the general will be all static files, HTML,HTM,JS,CSS, etc. are placed in the same folder, so there will be no case of Tomcat, because Tomcat under a different project, There's no way we can do that.

3) Some people will say that these will only find a server, but if we want to be in a server hangs, automatically go to find another, what to do? This is actually what Nginx has to consider.

At this time, the proxy_pass we used earlier had a big use.

We have modified the first example, that is, all agents:

The last modification is as follows:

XML code
    1. Upstream Local_tomcat {
    2. Server localhost:8080;
    3. }
    4. server{
    5. Location/{
    6. Proxy_pass Http://local_tomcat;
    7. }
    8. #...... Other omitted
    9. }

We added a upstream outside the server, and directly use the name of Http://+upstream in Proxy_pass.

We still come straight to the http://localhost, or the same effect as the first one, all the links are fine, we have the correct configuration.

The server element in upstream must be aware that it cannot be added http://, but must be added in Proxy_pass.

We just said we can connect to another one when one of the servers is hung up, so how do we get it?

In fact, it is very simple to configure more than one server in the Local_tomcat in upstream. For example I now make one more jetty, port at 9999, so we configure as follows:

XML code
    1. Upstream Local_tomcat {
    2. Server localhost:8080;
    3. Server localhost:9999;
    4. }

At this point, we turn off Tomcat and only open jetty. Let's run http://localhost to see the effect:

We saw it request to the Jetty page, but because of jetty mechanism, this time does not show Jetty home page, this we do not care. But our automatic use of another feature in the case of a server hangs is implemented.

But sometimes we don't want it to hang when visiting another, but just want a server to access the opportunity than the other one, this can be specified at the end of the server with a weight= number, the larger the number, indicates the greater the chance of the request.

XML code
    1. Upstream Local_tomcat {
    2. Server localhost:8080 weight=1;
    3. Server localhost:9999 weight=5;
    4. }

At this point we gave jetty a higher weight, giving it a more chance to access, actually when we refresh http://localhost access to find that jetty access is much more likely, Tomcat almost no chance to access, generally, if we have to use this, do not relate too much, So that a server load is too large.

Of course, the server also has some other elements, such as down means temporarily not to the server and so on. These can be referred to the Nginx wiki. Perhaps a lot of writing, some people will have a problem, that nginx how to close it? This is a problem, in fact, directly run Nginx-s stop can be closed.

Basically nginx usage is this, in-depth later if we use to learn again.

Installation and configuration of NGINX+TOMCAT consolidation under Windows

Related Article

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.