Nginx+tomcat configure cluster
This article is reproduced from: http://os.51cto.com/art/201111/304608.htm
This article introduces the final realization of load balancing through the cluster configuration of nginx and tomcat in Windows xp and ubuntu environments .
AD At the beginning, I had a small question: why not use open source apache and Nginx software loads? F5 equipment often costs hundreds of thousands and is expensive? I had a relatively naive question, and I later understood: F5 operates on the transport layer of the IOS network model, and Nginx and apache are based on the http reverse proxy method and are located in the seventh application layer of the ISO model. To put it bluntly, the difference between TCP UDP and http protocols is that Nginx cannot provide load balancing for applications based on TCP protocols. Understood the differences and application scenarios between the two, and developed a strong interest in Nginx. I read Zhang Yan’s "Practical Nginx" (this young and promising young man in 1985 was envious + jealous), and understood the general principles and Configuration, Ubuntu10.10, tried to configure Nginx+tomcat load balancing under window, forwarded all requests to tomcat, and did not configure static, dynamic separation, image hotlink prevention and other configurations.Nginx Introduction
Nginx (pronounced the same as engine x) is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server, and is released under a BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx’s concurrency capabilities do perform better among web servers of the same type. Currently, the users of nginx websites in mainland China include: Sina, NetEase, Tencent, and other well-known micronets Plurk also uses nginx.The above is an introduction to Nginx, which is basically nonsense. Now let’s get to the point. A combination of pictures and text will show the basic configuration, first the window environment, and secondly the Ubuntu environment (Vbox virtual).
Window xp environment: Nginx+Tomcat6
1. Download address
2. Directory structure
Log directory “ nginx.exe main program
It is extremely simple to install Nginx under the window. Just unzip it to an English directory without spaces (personal habit, worry about problems with Chinese). Double-click nginx to start. Here I installed it to: D:server directory, which is covered below. The tomcat is also installed in this directory.
If you want to stop nginx, run the command in dos environment: nginx -s stop
3. nginx.conf configuration
The Nginx configuration file is in the conf directory by default. The main configuration file is nginx.conf. We install it in D:servernginx-0.8.20, the default main configuration file is D:servernginx-0.8.20nginx.conf. The following is the configuration of nginx as a front-end reverse proxy server.
Nginx.conf code
- #The user and group used by Nginx are not specified under window
- #user niumd niumd;
- #The number of working sub-processes (usually equal to the number of CPUs or 2 times the CPU)
- worker_processes 2;
- #Error log storage path
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- error_log logs/error.log info;
- #Specify pid to store files
- pid Logs/nginx.pid;
- events {
- #When using the network IO model, Linux recommends epoll, FreeBSD recommends kqueue, and it is not specified under window. P #USE EPOLL;
- #Allow the maximum number of connections
- worker_connections 2048
- ;
- #Define log format
- #log_format main
- '$remote_addr - $remote_user [$time_local] $request ' " #
- '" $ Status "$ BODY_BYTES_SENT" $ http_referr "' " #
- '" $ http_user_agent "" $ http_x_Forwardeded_For "' ;
- #access_log off; access_log logs/access.log;
- client_header_timeout 3m;
- client_body_timeout 3m; send_timeout 3m;
- client_header_buffer_size 1k;
- large_client_header_buffers
- 4 4k;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- #keepalive_timeout 75
- 20 ;
- include gzip.conf;
- upstream localhost {
- #Allocate requests to each backend tomcat based on ip calculation. Many people mistakenly believe that it can solve the session problem, but it does not.
- #In the case of multiple networks, the IP may be different when routing is switched
- #ip_hash; }
- server {
- Listen proxy_connect_timeout 3 ;
- proxy_send_timeout
- 30 ;
- proxy_read_timeout 3 0
- ; proxy_pass http://localhost; }
- proxy_set_header Host $host;
- proxy_set_header ;
- proxy_send_timeout
- 300; proxy_read_timeout
- 300; proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- gzip compression related configuration is as follows:
-
Gzip.conf code:
gzip_min_length 1000
;- gzip_types text /plain text/css application/x-javascript;
- 4. Tomcat configuration
- Everyone is familiar with tomcat. You only need to modify the server.xml configuration file. Here we use apache-tomcat-6.0.14 For example, decompress and name them in the server directory: apache-tomcat-6.0.14_1, apache-tomcat-6.0.14_2.
- The first port modification:
- Xml code
- <
- Server port
- = "18006"
- shutdown= "SHUTDOWN">
The second port modification:
Xml code
- < Connectorport
"18081"protocol
="HTTP/1.1"
connectionTimeout ="20000"
CRedirectport- =
- "8443" /& gt;
- Third port modification: java code
"8009" protocol = "ajp/1.3 1.3 "
redirectPort=- "8443"
- />
- Engine element adds jvmRoute attribute: Xml code
<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">
Don’t duplicate the two tomcat ports. To ensure that it can be started, the other tomcat configuration is omitted and Seagate is omitted, and the listening port is 18080. We will upload all the configuration information in the attachment.
5. Verify configuration and test load balancing
First test whether the nginx configuration is correct, test command: nginx -t (default verification: confnginx.conf), you can also specify the configuration file path.
Secondly, verify tomcat and start two tomcats. If there is no port conflict, it is considered successful (I won’t talk nonsense about the java that tomcat relies on and so on);
Finally, verify the configuration of load balancing settings, http:// localhost/ or http://localhost/index.jsp. I modified the index.jsp page and added log output information for easier observation. Note: On the head of the kitten in the upper left corner: access tomcat2, access tomcat1. It means that different tomcats are accessed.
This completes the nginx+tomcat load balancing configuration under window. Regarding tomcat Session, memcached is usually used, or nginx_upstream_jvm_route is used. It is an Nginx extension module used to implement the Cookie-based Session Sticky function. If there are too many tomcats, session synchronization is not recommended. Synchronizing sessions between servers consumes resources, and high concurrency environments can easily cause session storms. Please adopt the session solution reasonably according to your own application situation.
Ubuntu10.10 environment: Nginx+Tomcat6
Let’s briefly talk about how to install and configure under ubuntu10.10
1. Download Nginx
Address: http://nginx.org/ en/download.html, linux version: nginx-0.8.20.tar.. Decompression command:
tar -zxvf nginx-0.8.20.tar.gz
2. Compile and install NginxNginx depends on some other PCRE, openssl (depends on libssl-dev), and my notebook Ubuntu environment has been installed For PCRE, you only need to install the dependent openssl. Let’s briefly talk about how to install PCRE and openssl, etc.
PCRE download address: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
Shell code
- tar zxvf pcre-8.01.tar.gz
- cd pcre-8.01
- sudo ./configure
- sodu make
- sodu make install
openssl via apt-get install Install
- sudo apt-get install openssl
- sudo apt-get install libssl-dev
- //If other packages are missing, please use this method to install. Ubuntu has a dependency prompt
The dependent software packages are installed, below To compile Nginx:
Shell code
- #Copy the window shared directory software to the current working directory
- cp /mnt/fileshare/nginx-0.8.20.tar.gz ./
- #Unzip the software package
- tar zxvf nginx-0.8.20.tar.gz
- cd nginx-0.8.20
- //Compile source code, use nobody by default, specify existing users and groups on this machine, enable nginx-status function, and monitor nginx status. Start debug
- sudo ./configure --user=niumd --group=niumd --with-debug --with-http_stub_status_module
- sudo make
- sudo make install
The compilation and installation ends correctly. Check the default configuration according to the above method under the window, then start nginx under the default configuration, visit http://127.0.0.1
Nginx configuration After success, we will make few modifications to nginx.conf under window, as follows:
Ubuntu nginx.conf code
- #User and group used by Nginx
- user niumd niumd;
- #The number of worker sub-processes (usually equal to the number of CPUs or 2 times the CPU)
- worker_processes 2;
- #Error日志存放路径
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- error_log logs/error.log info;
- #指定pid存放文件
- pid logs/nginx. pid;
- events {
- #Use network IO model Linux recommends epoll, FreeBSD recommends kqueue
- Use epoll;
- #Maximum number of connections allowed
- worker_ connections 2048
- ; }
- html user [$time_local] $request ' " #
- '" $ Status "$ BODY_BYTES_SENT" $ http_referr "' " #
- '" $ http_user_agent "" $ http_x_Forwardeded_For "' ;
- #access_log off;
- access_log logs/access.log;
- client_header_timeout 3m;
- client_body_timeout 3m; send_timeout 3m;
- client_header_buffer_size 1k;
- large_client_header_buffers
- 4 4k;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- #keepalive_timeout
- 75
- 20;
- include gzip.conf;
- upstream localhost {
- #ip_hash
- #ip_hash;
- server localhost:
- 18081; using using using using using out out out out out out of ’ s to ’ ’s ’ ‐ ‐ ‐ ‐ ‐ server { location proxy_connect_timeout 3 ;
- proxy_send_timeout
- 30 ; proxy_read_timeout
- 30 ;
- proxy_pass http://localhost;
- users and groups;
- 3. Configure tomcat
- Please refer to the configuration under window, which is exactly the same.
- 4. Start and stop nginx
- Starting nginx under ubuntu is slightly different from that in window. The general starting and stopping method is as follows.
- Java code
- #nginx目录执行
- sbin/nginx
- 或通过-c 指定配置文件
- sbin/nginx -c usr/local/nginx8.20/conf/nginx/conf
- niumd@niumd-laptop:/usr/local/nginx$ pwd
- /usr/local/nginx
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -t
- the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- configuration file /usr/local/nginx/conf/nginx.conf test is successful
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -v
- nginx version: nginx/0.8.20
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -V
- nginx version: nginx/0.8.20
- built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
- configure arguments: --user=niumd --group=niumd --with-debug --with-http_sub_module
- niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx
- niumd@niumd-laptop:/usr/local/nginx$ ps -ef|grep nginx
- root 5158 1 022:32 ? 00:00:00 nginx: master process sbin/nginx
- niumd 5159 5158 022:32 ? 00:00:00 nginx: worker process
- niumd 5161 1577 022:32 pts/0 00:00:00 grep --color=auto nginx
- niumd@niumd-laptop:/usr/local/nginx$
Shell代码
我们通过ps -ef|grep nginx,看到如下结果:
注意:在启动时linux提示一句警告【warn】……,是因为我们设置的 #允许最大连接数 worker_connections 2048,超过linux默认1024的限制。
停止:kill -信号类型 pid
nginx/logs目录下有个nginx。pid的文件,此文件记录了每次运行的pid,也可以通过ps命令查询。
信号类型如下:
信号类型 描述 RERM.INT 快速关闭 HUP 平滑重启,加载配置 USR1 重新加载日志 USER2 平滑升级执行程序 WINCH 从容关闭工作进程 QUIT 从容关闭 以上就介绍了Nginx+tomcat配置集群,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
