Integration experience of nginx + Tomcat

Time:2020-6-29

Nginx is a lightweight web server / reverse proxy server and email (IMAP / POP3) proxy server. In the web architecture of Java, Tomcat and nginx are usually used to cooperate. As the reverse proxy server, nginx can balance the load of the Tomcat server in the background, and can also let nginx process the static page request and Tomcat process the JSP page request to achieve the purpose of dynamic static separation.

About nginx

Nginx (“engine X”) is a high-performance HTTP and reverse proxy server. It is also an IMAP / POP3 / SMTP server. Igor sysoev is the second most visited server in Russia Rambler.ru Site development. Its characteristics are less memory and concurrent capability. In fact, nginx’s concurrent capability is better in the same type of web server. Chinese mainland users use Baidu website: Baidu, Jingdong, Sina, NetEase, Tencent, Taobao, etc.

As of December 31, 2014, nginx has become the second largest web server software after Apache, and its usage proportion in the world’s busiest top 10000 websites is as high as 42.7%. Its development speed and popularity has far exceeded other similar software, and has become the first choice of large-scale websites and high concurrency websites.

Nginx is composed of a kernel and a series of modules. The kernel provides the basic functions of Web services, such as enabling network protocol, creating running environment, receiving and distributing client requests, and handling the interaction between modules. Various functions and operations of nginx are implemented by modules. The modules of nginx are divided into core module, basic module and third-party module.

Core modules: http module, event module and mail module
Basic modules: http access module, HTTP fastcgi module, HTTP proxy module and HTTP Rewrite Module
Third party module: http upstream request hash module, notice module, HTTP access key module and user developed module

This design makes nginx easy to develop and expand, which makes nginx so powerful. Nginx modules are compiled into nginx by default. If you need to add or delete modules, you need to recompile nginx, which is not as convenient as Apache’s dynamic loading module. If you need to load the module dynamically, you can use Tengine, a web server initiated by Taobao. It adds many advanced features to nginx and is fully compatible with nginx. It has been adopted by many websites in China.

Construction of development environment

Let’s get down to business and integrate nginx and Tomcat. The first choice is to build the development environment, including JDK, Tomcat and nginx. The experimental environment of this paper is Ubuntu.

JDK installation

Download the JDK and unzip it to the specified directory.

[email protected]:~$ wget http://120.52.72.24/download.oracle.com/c3pr90ntc0td/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz
[email protected]:~$ tar zxvf jdk-7u79-linux-x64.tar.gz 
[email protected]:~$ sudo mv jdk1.7.0_79/ /usr/local/jdk

Configure environment variables and add Java in / etc / profile_ Home and other paths.

export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

Load environment variables.

[email protected]:~$ source /etc/profile

Check whether the configuration is successful.

java -version

If the following results are displayed, the configuration is successful:

Integration experience of nginx + Tomcat

Tomcat installation

The second step is to install Tomcat.

Download Tomcat and extract it to the specified directory.

[email protected]:~$ wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz
[email protected]:~$ tar zxvf apache-tomcat-8.5.9.tar.gz 
[email protected]:~$ sudo mv apache-tomcat-8.5.9/ /usr/local/tomcat

The configuration of Tomcat and the setting of ordinary users will not be mentioned here. Start Tomcat directly.

[email protected]:~$ sudo /usr/local/tomcat/bin/startup.sh

If the following prompt appears, Java cannot be found_ Path to home

Integration experience of nginx + Tomcat

At this time, you need to use the catalina.sh Add the following information:

Integration experience of nginx + Tomcat

Start Tomcat again, successful

Integration experience of nginx + Tomcat

You can open it now http://localhost : 8080, see Tomcat’s home page.

Integration experience of nginx + Tomcat

Nginx installation

Next comes nginx.

Download nginx, unzip and install to the specified directory.

[email protected]:~$ wget http://labfile.oss.aliyuncs.com/nginx-1.7.9.tar.gz
[email protected]:~$ tar zxvf nginx-1.7.9.tar.gz
[email protected]:~$ cd nginx-1.7.9
[email protected]:~/nginx-1.7.9$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
[email protected]:~/nginx-1.7.9$ # make && make install

Switch to the installation directory of nginx and start nginx.

[email protected]:~$ cd /usr/local/nginx/
[email protected]:/usr/local/nginx$ sudo ./nginx-1.7.9/objs/nginx

Access at this time http://localhost You will see the welcome interface for nginx. (because I don’t access it directly on the nginx machine, I use the IP address of the nginx server: 192.168.6.86. The following part of this article is like this. )

Integration experience of nginx + Tomcat

Nginx configuration

Nginx can optimize the performance by adjusting the parameters of the configuration file. This configuration file is nginx.conf 。

nginx.vim

Digression, due to the use of nginx, often need to edit nginx.conf We can use nginx.vim Make nginx.conf The grammar is clearer and clearer.

  • reach http://www.vim.org/scripts/sc… download nginx.vim 。

  • Move to / usr / share / VIM / vim74 / syntax / directory

  • In / usr / share / VIM / vim74/ filetypr.vim Add:

    au BufRead,BufNewFile /usr/local/nginx/conf/* set ft=nginx

Reopen nginx.conf , has syntax highlighted:

Integration experience of nginx + Tomcat

nginx.conf

nginx.conf There are many places to adjust and optimize. Here are a few common parameters of HTTP module. This part of the content refers to the experimental building tutorial.

As shown in the picture, it’s mine nginx.conf Configuration of

Integration experience of nginx + Tomcat

Briefly introduce some configuration parameters in the red box.

In the first box are the settings for the log:

  • log_ Format defines the log format

  • access_ Log set whether to save the access log. If off is set, the disk IO can be reduced and the speed can be increased.

In the second box are some basic settings:

  • Sendfile points to the sendfile() function. Sendfile () copies data between disk and TCP port (or any two file descriptors). Sendfile() reads data directly from the disk to the operating system buffer, so it is more efficient.

  • tcp_ Nopush configures nginx to send all header files in a package instead of sending them one by one.

  • tcp_ Nodelay configures nginx not to cache data, but to send small data quickly.

  • keepalive_ Timeout specifies the timeout for the keep alive link to the client. The server will close the link after this time.

In the third box is the setting of the compression function:

  • Gzip turns on compression to reduce the amount of data that needs to be sent.

  • gzip_ Disable disables the gzip feature for the specified client.

  • gzip_ Proxied allows or disallows compression based on requests and responses. Set to any to gzip all requests.

  • gzip_ comp_ Level sets the level of data compression. Levels can be any value from 1 to 9, where 9 represents the slowest but highest proportion of compression.

  • gzip_ Types sets the type for gzip.

Next, take a look at the sub module server in the HTTP module and the configuration of the sub module location in the server

Integration experience of nginx + Tomcat

Among them:

  • Listen indicates the port on which the current proxy server is listening. The default is port 80.

  • server_ Name indicates where to go after listening, localhost indicates to go to local, that is, directly to nginx folder.

  • Location represents the matching path.

  • Root means to find files in the specified file path, which can be used for static files.

  • Index indicates the default home page. You can specify multiple pages and search them in order.

  • Deny and allow are access control settings that prohibit or allow access to an IP or an IP segment. You can also specify UNIX to allow socket access.

  • limit_ rate_ After sets the response size for transmission without speed limit. When the transmission capacity is greater than this value, the excess part will be limited speed transmission.

  • limit_ Rate limits the rate at which responses are delivered to clients. The unit of parameter is byte / second, setting to 0 will turn off the speed limit. For example, the non speed limit in the picture is 3M, and the speed limit is 20K / s after 3M.

For more explanation of nginx configuration, please refer to this blog, which is very detailed.

Note that after modifying the configuration file, you need to restart nginx to take effect. Of course, if your nginx is installed with apt get, you can use the nginx – s reload command to reload the configuration file.

Nginx+Tomcat

Let’s move on to the topic of this article and integrate nginx with Tomcat. This part of the work is actually revision nginx.conf In the configuration.

The JSP page request is handed over to Tomcat for processing

Specifically, in nginx.conf Create a new location in, and use regular expressions to match all JSP requests to the location:

Integration experience of nginx + Tomcat

The core is “proxy”_ pass http://localhost : 8080; “this configuration will forward all matching requests to Tomcat for processing.

Other configurations:

  • proxy_ set_ Header host $host; the back-end web server can obtain the user’s real IP through x-forward-for.

  • client_ max_ body_ Size 10m; the maximum number of single file bytes allowed for client requests.

  • client_ body_ buffer_ Size 128K; the maximum number of bytes that the buffer agent buffers client requests.

  • proxy_ connect_ Timeout 90; the timeout for nginx to connect to the back-end server.

  • proxy_ read_ Timeout 90; the response time of the back-end server after the connection is successful.

  • proxy_ buffer_ Size 4K; set the buffer size for the proxy server to save the user header information.

  • proxy_ buffers 6 32k; proxy_ Buffers buffer.

  • proxy_ busy_ buffers_ Size 64K; buffer size under high load.

  • proxy_ temp_ file_ write_ Size 64K; set cache folder size.

Access at this time http://localhost/index.jsp , you will find that you have jumped to the Tomcat page. But you will find that the page is like this:

Integration experience of nginx + Tomcat

This is because although JSP requests are forwarded to tomcat, static files such as images and CSS cannot be found. So next we need to configure the path of static files to complete dynamic static separation.

Dynamic and static separation

For static file requests, we also create a new location to match common images, CSS, JS and other requests to the location

Integration experience of nginx + Tomcat

As shown in the figure, the configuration is very simple. Through the root keyword, all the matching requests can be found directly in the Tomcat / webapps / root directory. However, expires 30d means using the expires cache module to cache to the client for 30 days.

Restart nginx after configuration. Reenter http://localhost/index.jsp , you will find that the Tomcat page has been displayed normally. We have completed the separation of JSP request and static file request. But in fact, this page is only displayed. When you click other links on the page, 404 will be displayed. This is obvious because we only have the root directory configured.

Integration experience of nginx + Tomcat

If you still find that you can’t read the static file after configuration, take a look at the access http://localhost/tomcat.png Whether 403 Forbidden is displayed. If so, it is because of the permission problem. Here is a simple solution nginx.conf Set the user in the first line as root:

Integration experience of nginx + Tomcat

Of course, if you don’t want to run with root, you can solve the 403 problem by modifying the directory access rights, but you can’t put the directory under the root user’s host directory, put it in any location and give it 755, or change its owner’s identity to be consistent with nginx through chown.

load balancing

Next, let’s experiment with load balancing.

stay nginx.conf By configuring upstream, you can easily configure the load balancing of the background server

Integration experience of nginx + Tomcat

Specifically, the address of the background Tomcat server can be configured in upstream. Here, I have configured two servers, namely 192.168.6.86 and 192.168.6.32, respectively. Tomcat8 is installed on the local machine and tomcat7 is installed on the other server to show the difference. Then, in location, the request is forwarded to the configured upstream for processing. In order not to conflict with the previous configuration, I have newly configured a server to monitor port 8888.

Then, visit http://localhost :8888。 Refresh several times, you will find that the request is forwarded to a different server:

Integration experience of nginx + Tomcat

Integration experience of nginx + Tomcat

As you can see, the displayed pages are the home pages of tomcat7 and tomcat8 respectively, which proves that the request is successfully forwarded.

In addition, you can also add a weight after the server. The greater the weight, the greater the access opportunity. The default value is 1.

Integration experience of nginx + Tomcat