In the previous article, we introduced the theory of NGINX+TOMCAT technology in the cluster, and today we are going to complete a simple cluster setup .
Results Preview
Let's look at the results after the configuration:
Configuration steps:
1. Installation
Download the latest version of Nginx (currently 1.9. Version 3) from the Nginx official website download page (http://nginx.org/en/download.html) to install the package, and then copy it to the deployment directory.
2. Start and stop Nginx
Nginx currently only supports command line operations, before operation into the DOS command environment (CMD command), and into the Nginx deployment directory.
Start Nginx:start Nginx
Stop Nginx:nginx-s Stop
Modify configuration after reboot: Nginx-s Reload
These three commands can be made into bat files, placed in the deployment directory for easy follow-up operation.
Start Nginx.bat file contents: Start Nginx
Stop Nginx.bat file contents: Nginx-s stop
Reload Nginx.bat file contents: Nginx-s Reload
3. Reverse proxy configuration
Modify the contents of the nginx.conf file (such as nginx-1.5.13\conf\nginx.conf) in the Conf subdirectory under the deployment directory to adjust the relevant configuration.
example of a reverse proxy configuration:
Location/{ #设置主机头和客户端真实地址 so that the server obtains the client's real IP proxy_set_header Host $host; Proxy_set_header x-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; #禁用缓存 proxy_buffering off; #设置反向代理的地址 proxy_pass http://192.168.1.1; }
4. Load Balancing Configuration
Nginx Upstream By default is a poll-based load balancing, in this way, each request in chronological order to a different back-end server, if the backend server down, can be automatically rejected.
Another way is Ip_hash: Each request is allocated according to the hash result of the access IP, so that each visitor fixed access to a back-end server can solve the session problem.
Sample Load Balancer Configuration
Upstream xvshu.com{ #ip_hash; Server 192.168.121.251 1; Server 192.168.121.252 1; Server 192.168.121.253 1; } server { Listen server_name trffweb; Location/{ #反向代理的地址 proxy_pass http:/xvshu.com; }}
5. Full Configuration Example
Nginx.conf: #Nginx所用用户和组, do not specify #user  NIUMD niumd under window; #工作的子进程数量 (usually equal to the number of CPUs or twice times the CPU) worker_processes 1; #错误日志存放路径 #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #指定pid存放文件 pid logs/nginx.pid; events { #使用网络IO模型linux建议epoll, FreeBSD is recommended to use Kqueue,window not specified. #use Epoll; #允许最大连接数 worker_connections 2048; } http { include mime.types; Default_ Type application/octet-stream; #定义日志格式 #log_format main ' $remote _addr-$r Emote_user [$time _local] $request ' # &NBsp ' $status $body _bytes_sent "$http _referer" ' # ' "$http _user_agent" "$http _x_forwarded_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; #include gzip.conf; Upstream xvshu.cn{ #根据ip计算将请求分配各那个后端tomcat, many people mistakenly think that can solve the session problem, in fact, can not. #同一机器在多网情况下, routing switching, IP may be different #ip_hash; server 192.168.112.250:18080 weight=1; server 192.168.112.251:18080 weight=1; &NBSP,} server { Liste N 80; server_name localhost; #定义server_name localhost requests are called xvshu.cn processing Location/{ #proxy_connect_timeout &NB Sp 3; #proxy_send_timeout 30; #proxy_read_timeout 30;   Proxy_pass http://xvshu.cn;proxy_redirect default; &N Bsp } } }
Problems encountered:
Failed to reload Nginx service, prompt for no corresponding service, can try to restart the server manually
Summarize:
A good tool, not only to achieve a good function, there is a more important point is that there must be simpler business logic, so that users in the use of this tool can be a smooth transition, and nginx this software, for the vast number of developers just meet these two requirements, without heart, users will appreciate, intentions, is to let users do not understand, inseparable from! Isn't that the thinking of Internet products?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the Java cluster optimization--nginx+tomcat cluster configuration-practice, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.