How to use tomcat with nginx

王林
Release: 2023-05-21 13:07:06
forward
1914 people have browsed it

Summary of using tomcat combined with nginx

I believe many people have heard of nginx. This small thing is slowly eating up the share of apache and IIS. So what exactly does it do? Maybe many people don't understand it.

Speaking of reverse proxy, many people may have heard of it, but many people may not know what a reverse proxy is. Take a description from Baidu Encyclopedia:

  1. The reverse proxy method refers to using a proxy server to accept connection requests on the Internet, then forwarding the requests to the server on the internal network, and returning the results obtained from the server to the client requesting a connection on the Internet. At this time, the proxy server appears as a server to the outside world.

## It’s very straightforward here. The reverse proxy method is actually a proxy server responsible for forwarding. It seems to act as a real server, but in fact it is not. The proxy server only acts as a forwarder and obtains the returned data from the real server. In this way, nginx actually completes this kind of work. We let nginx listen to a port, such as port 80, but in fact we forward it to tomcat on port 8080, which handles the real request. When the request is completed, tomcat returns, but the data is not returned directly at this time, but directly Give it to nginx, and nginx will return it. Here, we will think that nginx is processing it, but in fact it is tomcat that is processing it.

Speaking of the above method, many people may think of it again, so that static files can be processed by nginx. Yes, many places where nginx is used are used as static servers, which can easily cache static files, such as CSS, JS, html, htm and other files.

Without further ado, let’s take a look at how to use nginx.

1) Of course you need to download the software you want to use. Go to the nginx official website for the next one. The version I am using now is 1.1.7, but basically all future versions are compatible, and what we are using does not involve too low-level aspects, so there should be no changes.

Here, since mine is Windows, of course I download the Windows version. After downloading, you need to start it first. Enter the nginx folder and start nginx directly.

For example, after downloading, I put it in D:\software\developerTools\server\nginx-1.1.7, directly cmd and then cd D:\software\developerTools\server\nginx-1.1.7. Some people who are not used to the command line may be surprised. It doesn't proceed to that folder. Windows will not jump between partitions unless you specify it yourself. So we have to directly d: as follows:

How to use tomcat with nginx

At this time we open the task manager and can see two nginx.exe there. This shows that we have already started. As for why, we will not delve into it here.

How to use tomcat with nginx

At this point we open the task manager and can see the two nginx.exe processes running fine there. This shows that we have already started. As for why, we will not delve into it here.

How to use tomcat with nginx

Now that we have started nginx, we can start tomcat. If we want to directly access http://localhost, we can directly access tomcat.

Don't worry, let's take a look at what nginx looks like after starting. Directly access http://localhost to see:

How to use tomcat with nginx

We can see that nginx started successfully, and now the access is directly into the nginx directory.

So where are these actually configured. This involves nginx.conf, an important configuration file of nginx.

2) We can see that there is a conf folder in the nginx folder, which contains several files. Regardless of the others, we open nginx.conf and can see a section of

How to use tomcat with nginx

This code is in the server and is equivalent to a proxy server. Of course, multiple ones can be configured.

Let’s analyze it carefully:

listen: Indicates the port that the current proxy server listens to. The default is to listen to port 80. Note that if we configure multiple servers, the listener must be configured differently, otherwise we will not be able to determine where to go.

server_name: Indicates where to go after listening. At this time, we go directly to the local area. At this time, it is directly to the nginx folder.

location: Indicates the matching path. At this time, / is configured to indicate that all requests are matched here

Root: Root is configured in it, which means that when the path of this request is matched, the corresponding file will be found in this folder. This is very useful for our subsequent static file serving.

index: When no home page is specified, the specified file will be selected by default. There can be multiple files and they will be loaded in order. If the first one does not exist, the second one will be found, and so on.

The error_page below is the page that represents the error. We will not use it here for the time being, so we will ignore it for now.

Then we know the specific configuration, how to make it switch to tomcat when accessing localhost. In fact, only two places have to be modified:

1 server_name localhost:8080;   
2   
3 location / {   
4     proxy_pass http://localhost:8080   
5 }
Copy after login

We have modified the above two places. My tomcat is on port 8080. You can modify it according to your own needs. There is a new element proxy_pass here, which represents the proxy path, which is equivalent to forwarding, unlike the root that must be specified before.

Now that we have modified the file, does it mean that we must shut down nginx and then restart it? In fact, it is not necessary, nginx can reload the file.

We run directly:

How to use tomcat with nginx

It was too early to rejoice, we found an error:

How to use tomcat with nginx

What happened? An error was found on line 45. We didn’t want to find } on that line, so we looked carefully and found that the proxy_pass we added was strange. It didn’t end with a ; sign. This was the problem. We modified it directly and then ran it again. We found that there was no Error, OK.

If you don’t want to load it directly, but just want to see if there is any problem with your configuration file, you can directly enter:

How to use tomcat with nginx

This checks the configuration file for errors. All our modifications below assume that we run nginx -s reload to reload the configuration file after the modification is completed. Please note.

Everything is fine. Then we reopen http://localhost and we see the following page:

How to use tomcat with nginx

At this time, we found that it was not the welcome page just now, but the tomcat management page. No matter what link we click, there will be no problem, which is equivalent to directly accessing http://localhost:8080.

3) Above we directly tried a small example to let nginx forward, which is the so-called reverse proxy. But in fact our needs will not be like this. We need to filter by file type. For example, jsp is directly processed by tomcat, because nginx is not a servlet container and cannot process JSP, while html, js, and css do not need to be processed. Yes, cache it directly to nginx.

Let's configure it so that the JSP page is directly sent to tomcat, and some pictures and JS such as html and png are directly cached by nginx.

At this time, the most important thing to use is the location element, and it involves some regular rules, but it is not difficult:

1 location ~ \.jsp$ {   
2         proxy_pass http://localhost:8080;   
3 }   
4            
5 location ~ \.(html|js|css|png|gif)$ {   
6     root D:/software/developerTools/server/apache-tomcat-7.0.8/webapps/ROOT;   
7 }
Copy after login

We first need to remove the previously configured location / to prevent all requests from being intercepted.

Then let’s take a look at http://localhost

How to use tomcat with nginx

When we do not specify the jsp page, it will not be found because there is no corresponding location match at this time, so there will be a 404 error, and then it will jump to the nginx custom error page.

And when we use http://localhost/index.jsp to visit, we see the familiar page: How to use tomcat with nginx

And the pictures are displayed normally, because the pictures are png, so we can directly search them in the tomcat/webapps/ROOT directory. Of course, if we click on the Manager Application HOW-TO link, we find:

                How to use tomcat with nginx

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 html, we go to the ROOT directory to find it, so we still can't find the page.

Under normal circumstances, if we need to use nginx to serve static files, we will generally put all static files, html, htm, js, css, etc. in the same folder, so that there will be no such situation as tomcat. Because the ones under tomcat belong to different projects, there is nothing we can do about this.

3) Some people will say that these will only find one server, but what if we want to automatically find another server when one server hangs up? This is actually taken into consideration by nginx.

At this time, the proxy_pass we used before will be of great use.

Let’s modify the first example before, that is, all agents:

The final modification is as follows:

 1 upstream local_tomcat {   
 2     server localhost:8080;   
 3 }   
 4   
 5 server{   
 6         location / {   
 7            proxy_pass http://local_tomcat;   
 8         }   
 9         #......其他省略   
10 }
Copy after login

We added an upstream outside the server and used it directly in proxy_pass using the name of http:// upstream.

我们还是直接来http://localhost,还是和第一个一样的效果,所有链接都没问题,说明我们配置正确。

upstream中的server元素必须要注意,不能加http://,但proxy_pass中必须加。

我们刚才说可以在一个服务器挂了的情况下连到另外一个,那怎么弄呢?

其实很简单,在upstream中的local_tomcat中配置多一个server。比如我现在弄多一个jetty,端口在9999,所以我们配置如下:

1 upstream local_tomcat {   
2     server localhost:8080;   
3     server localhost:9999;   
4 }
Copy after login

此时,我们关闭tomcat,而只开jetty。我们来运行http://localhost看看效果:

How to use tomcat with nginx

我们看到它请求到了jetty的页面,但由于jetty的机制,这时没有显示jetty主页,这个我们先不管。但我们的在一个服务器挂的情况下自动使用另外一个的功能实现了。

但有时我们就不想它挂的时候访问另外一个,而只是希望一个服务器访问的机会比另外一个大,这个可以在server最后加上一个weight=数字来指定,数字越大,表明请求到的机会越大。

1 upstream local_tomcat {   
2     server localhost:8080 weight=1;   
3     server localhost:9999 weight=5;   
4 }
Copy after login

这时我们给了jetty一个更高的权值,让它更有机会访问到,实际上当我们刷新http://localhost访问的时候发现jetty访问机率大很多,tomcat几乎没机会访问,一般情况下,如果我们必须这样用,不要相关太大,以免一个服务器负载太大。

当然,server还有一些其他的元素,比如down表示暂时不用到该服务器等等。这些可以参考nginx的wiki。也许写了一大堆,有人会有问题,那nginx怎么关闭呢?这倒是个问题,其实直接运行nginx -s stop就可以关闭了。

The above is the detailed content of How to use tomcat with nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template