Home >Backend Development >PHP Tutorial >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
gzip_min_length 1000
;The second port modification:
Xml code
"18081"protocol
="HTTP/1.1"
connectionTimeout ="20000"
CRedirectport"8009" protocol = "ajp/1.3 1.3 "
redirectPort=<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 Nginx
Nginx 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
openssl via apt-get install Install
The dependent software packages are installed, below To compile Nginx:
Shell code
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
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教程有兴趣的朋友有所帮助。