Build a Web Cluster Environment Using Nginx and Tomcat
I have been familiar with tomcat for web servers for a long time, but I am still a little unfamiliar with nginx. Once I see nginx configuration, there is an inexplicable rejection. This is a fear of strangers, today we are playing an interesting role. I never know about nginx, but it takes less than an hour to build clusters with nginx and tomcat.
Looking at my learning achievements in the past hour, it doesn't help you.
Nginx is a lightweight web server from Russia. It is open-source, free, and concise.
Its website is this: http://nginx.org/en/download.html
There are currently three types of downloaded versions, which are easy to understand. One is the current development version (Mainline version) and the other is the latest stable version. For example, the latest version is 1.12.2, the source code package and windows version are available. The third type is the nostalgic stable version, which is not necessarily the latest online environment. It is considerate to consider compatibility.
One reason for the simplicity of nginx is that the installation package is really small enough and the compressed version is less than 1 MB, and the decompressed version is about 7 MB.
[Root @ localhost nginx] # ll
Total 960
-Rw-r --. 1 root 981687 Oct 18 nginx-1.12.2.tar.gz
It is relatively easy to install nginx, such as configure, make, and make install. It is important to note that the installation requires several dependent packages, such as the zlib and PCRE libraries. You can check them in advance.
# Rpm-qa | grep zlib
Zlib-1.2.3-29.el6.x86_64
Zlib-devel-1.2.3-29.el6.x86_64
PCRE-devel is required for the pcre Library. You can use yum-y install pcre-devel to configure the yum source.
# Rpm-qa | grep pcre
Pcre-devel-7.8-7.el6.x86_64
Pcre-7.8-7.el6.x86_64
Summary of the three steps for installation:
./Configure -- prefix =/usr/local/nginx
Make
Make install
To edit ssl, add additional options.
Nginx is easy to start. You can start it directly by using the nginx command. Port 80 is used by default, and a welcome page will be displayed soon.
Of course, we can use fuser to check port 80 or whether port 80 is occupied:
# Fuser-n tcp 80
80/tcp: 21412 21413
Or:
# Netstat-pan | grep-w 80
Tcp 0 0 0.0.0.0: 80 0.0.0.0: * LISTEN 21412/nginx
Tcp 0 0 127.0.0.1: 80 127.0.0.1: 49593 TIME_WAIT-
Tcp 0 0 192.168.253.219: 57492 23.32.3.248: 80 ESTABLISHED 21590/clock-applet
Tcp 0 0 192.168.253.219: 51678 60.221.218.180: 80 TIME_WAIT-
Tcp 1 0: ffff: 192.168.253.21: 39646: ffff: 104.25.106.17: 80 CLOSE_WAIT 12329/java
Tcp 1 0: ffff: 192.168.253.21: 37445: ffff: 104.25.107.17: 80 CLOSE_WAIT 12329/java
If you view nginx-related processes, you will find a master and a worker process.
# Ps-ef | grep nginx
Root 21412 1 0? 00:00:00 nginx: master process./nginx
Nobody 21413 21412 0? 00:00:00 nginx: worker process
Root 21719 15134 0 00:00:00 pts/3 grep nginx
This section is easy to understand through the nginx configuration file. In the nginx. conf file, the start line is the following two lines. It can be seen that there is one worker process and the nobody is configured. Therefore, the owner of the worker process you see is the nobody.
# User nobody;
Worker_processes 1;
This is the nginx architecture. It uses epoll.
Almost no nginx commands need to be re-learned. You can directly use-h to get the help command. Therefore, we can easily find that:./nginx-s stop is the stop command, enabling the configuration file to use the-c option.
In the sbin directory where nginx is located, a complete startup command is:
./Nginx-c/usr/local/nginx/conf/nginx. conf
Then let's take a look at how it works with tomcat. nginx is often used as an http server, reverse proxy, or email server. It is also a natural solution for load balancing. Let's simulate it.
For example, if the current backend server is tomcat, it is natural to use nginx to forward the Server Load balancer. If one of the tomcat servers has a problem, it can also easily meet the fault tolerance.
Therefore, we need to configure several tomcat services for simulation. For example, we use three tomcat services.
Drwxr-xr-x. 9 root 4096 Jan 3 running at1
Drwxr-xr-x. 9 root 4096 Jan 3 tomcat2
Drwxr-xr-x. 9 root 4096 Jan 3 tomcat3
The default port is 8080. In simple packaging, the three tomcat ports are:
18080
28080
38080
To modify the tomcat configuration file server. xml, pay attention to the following port settings:
Tomcat1:
<Server port = "18005" shutdown = "SHUTDOWN">
<Connector port = "18080" protocol = "HTTP/1.1"
<Connector port = "18009" protocol = "AJP/1.3" redirectPort = "8443"/>
Tomcat2:
<Server port = "28005" shutdown = "SHUTDOWN">
<Connector port = "28080" protocol = "HTTP/1.1"
<Connector port = "28009" protocol = "AJP/1.3" redirectPort = "8443"/>
Tomcat3:
<Server port = "38005" shutdown = "SHUTDOWN">
<Connector port = "38080" protocol = "HTTP/1.1"
<Connector port = "38009" protocol = "AJP/1.3" redirectPort = "8443"/>
Then start a simple verification: You can see the kitten.
For the sake of distinction, we can represent tomcat, tomcat 2, and tomcat 3 in webapps/ROOT/index. jsp respectively.
Now tomcat is ready. We can configure Nginx.
The core of nginx configuration is nginx. conf.
Note the red Configuration:
# Gzip on;
Upstream jeanron100.com {
Server 127.0.0.1: 18080 weight = 1;
Server 127.0.0.1: 28080 weight = 2;
Server 127.0.0.1: 38080 weight = 3;
}
Server {
Listen 80;
Server_name localhost;
# Charset koi8-r;
# Access_log logs/host. access. log main;
# Location /{
# Root html;
# Index index.html index.htm;
#}
Location /{
Proxy_pass http://jeanron100.com;
Proxy_redirect default;
}
Start nginx and run the following command:
./Nginx-c/usr/local/nginx/conf/nginx. conf
Enter the IP address and Page name in the browser. We can see that the forwarding has been started at this time, and now it is transferred to Tomcat 2.
Refresh continues and now jumps to Tomcat 3.
Continuously refresh, tomcat and nginx are mapped.
You may also like the following Nginx articles. For more information, see:
Nginx 403 forbidden Solution
Install and configure the Nginx server in CentOS 7
Install the Nginx server on CentOS to redirect virtual hosts and domain names
Install the LNMP environment in CentOS 6.8 (Linux + Nginx + MySQL + PHP)
Install the PHP environment in Linux and configure Nginx to support the php-fpm Module
SSL authentication and htpasswd authentication for Nginx services
Enable encrypted and secure Nginx Web server on Ubuntu 16.04
Install and configure Nginx in Linux
Nginx log filtering using ngx_log_if does not record specific logs
Install and configure Nginx + PHP + MySQL + Memcache cache servers in CentOS 7.2
CentOS6.9 compile and install Nginx1.4.7
Nginx details: click here
Nginx: click here