Nginx server load balancer 1,
1. Do not install it in the same directory of the source code during installation. make will report an error.
The code is as follows: |
Copy code |
./Configure -- prefix =/opt/nginx Make & make install |
2. Modify the configuration file/opt/nginx/conf/nginx. conf.
Add the following before the server element: (assume that both tomcat ports are 8080)
The code is as follows: |
Copy code |
Upstream www.111cn.net { Server 192.168.1.13: 8080; Server 192.168.1.14: 8080; } |
Change server_name under the server element to the actual domain name, for example:
The code is as follows: |
Copy code |
Server_name www.111cn.net
|
Add a row under the location element:
The code is as follows: |
Copy code |
Proxy_pass www.111cn.net |
3. Start the stop Command
Start:/opt/nginx/sbin/nginx
Quick stop:/opt/nginx/sbin/nginx-s stop
Complete and ordered stop:/opt/nginx/sbin/nginx-s quit
Reload:/opt/nginx/sbin/nginx-s reload
Nginx server load balancer 2,
Suppose we have three servers with IP addresses:
192.168.0.1/192.168.0.2/192.168.0.3
We use 192.168.0.1 as the front-end master server, and 192.168.0.2 and 192.168.0.3 as the backend servers of server load balancer.
The following describes how to configure Nginx on the master server 192.168.0.1:
The code is as follows: |
Copy code |
Worker_processes 1; Events { Worker_connections 1024; } Http { Upstream serverlist { Server 192.168.0.2: 8000 weight = 3 max_fails = 3 fail_timeout = 20 s; Server 192.168.0.3: 8000 weight = 7 max_fails = 3 fail_timeout = 20 s; } Server { Listen 80; Server_name www.111cn.net; Location /{ Proxy_pass http: // serverlist; Proxy_set_header HOST $ host; Proxy_set_header X-Real-IP $ remote_addr; Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; } } } |
Configuration instructions:
Upstream serverlist {} can be understood as a server pool. By using the proxy_pass http: // serverlist command, requests for accessing www.111cn.net can be allocated to the servers in the pool 192.168.0.2 and 192.168.0.3.
Weight = 3 and weight = 7 are server weights. The higher the weight, the more requests are allocated.
Max_fails = 3 and fail_timeout = 20 s indicate that when the number of request failures reaches 3, the server will be temporarily offline for 20 seconds.
Tip: The master server is responsible for static content and allocating all php requests to backend servers for processing.