Not much to say, nginx installation and configuration, as well as load balancing, you can read another article I wrote "nginx Load Balancing in Practice", and about load balancing, you can read another article I wrote Two articles, one is "lvs+keepalived load balancing" and the other is "haproxy+keepalived load balancing". For the difference between the three load balancing, you can take a look at an article I reprinted "Software Level Load Balancer (LVS)" /HAProxy/Nginx), let’s go directly to the configuration steps:
1. System environment
[plain] view
plaincopy
- System version: CentOS release 5.9 (Final) x86 32-bit
- nginx version: 1.2.8
- keepalived version: 1.2 .4
-
- main keepalived: 192.168.207.130
-
- from keepalived:192.168.207.131
-
-
- VIP: 192.168.207.140
- WEB_1:192.168.207.129 Port 80
- WEB_2 :192.168.207.130 8080 port
- WEB_3:192.168.207.131 8080 port
2. Custom nginx configuration file
at 192.168.207 Operating on .130 and 192.168.207.131
[plain] view
plaincopy
- useradd nginx
- vi /usr/local/nginx/conf/nginx.conf
The content is as follows:
[plain] view
plaincopy
- #Run user
#Global error log and PID file error_log logs/error. log notification; epoll; worker_connections 1024; #So The total number of connections supported by nginx is equal to worker_processes * worker_connections }- #Set the http server and use its reverse proxy function to provide load balancing support
- http {
- #Set the mime type
- include mime.types ; #This is about nginx Which multimedia types are supported? You can go to conf/mime.types to check which multimedia are supported.
- default_type application/octet-stream; #Default data type
- #Set log format
- Log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" '
- '"$gzip_ratio"' ; ; ent ' '
- '"$http_referer" "$http_user_agent" '
- '"$http_range" "$sent_http_content_range"';
- large_client_header_buffers 4 4k;
- #Enable gzip module
- #gzip on; gzip_types text/plain;
- #output_buffers 1 32k;
- #postpone_output 1460;
- #Setting access log
- access_log logs/access.log main;
- client_header_timeout 3m;
- client_body_timeout 3m;
- send_timeout 3m;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 65;
- #Set the load balancing server list
-
- upstream mysvr {
- #weigth parameter representation Weight, the higher the weight, the greater the chance of being assigned weight=5; . Server 192.168.207.131:8080 weight = 5;
} server {#This is set to set web service, monitor port 8080 Listen 8080; server_name 192.168.207.131; #This changes according to the system ip ‐ ’ s ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ page 500 502 503 504 /50x.html; # location = /50x.html { html; -
#Set up virtual host A Server_name 192.168.207.140;#Here is VIP
-
#Charset GB2312;
-
#Set the visits log of this virtual host
-
access_log Logs/Threeb.access.log Main;
- #If you access /img/*, /js/*, /css/* resources, fetch local files directly, not through squid The cache effect is better l #Location ~^/(IMG | JS | CSS)/{
-
#root/data3/html;
-
#Expires 24h;
-
#}#"/" Enable load balancing
- local/{
- proxy_pass http:// mysvr;#
proxy_set_header Host $host; proxy_set_header proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_ size 128k; -
proxy_connect_timeout 90; proxy_read_timeout 90; proxy_buffer_size proxy_busy_buffers_size 64k; -
- stub_status on;
- access_log on;
- auth_basic "NginxStatus"; auth_basic "NginxStatus"; .Customize keepalived configuration file
- [plain] view
plaincopy
-
vi /etc/keepalived/keepalived.conf
- The content is as follows:
-
[plain] view
plaincopy
- global_defs {
- notification_email {
- root@localhost.localdomain }
- notification_email_from notify@keepalived.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
-
- vrrp_script chk_http_port {
- script "/etc/keepalived/check_nginx.sh" ##Monitoring time
- Monitoring time VI_1 {
- , ### Monitoring network card
- ’ s ‐ ‐ ‐ ‐ out of 101 The weight value MASTRE must be higher than BAUCKUP
Auth_pass 1111}} track_script { chK_HTTP_PORT ###}}} virtual_ipaddress { 192.168.207.140 ### vip address - View
plaincopy
- vi /etc/keepalived/check_nginx.sh
The content is as follows:
[plain] view
plaincopy
- !/bin/bash
- A = `ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx - if no process is worth zero
sleep sleep -no-header |wc -l` -eq 0 ];then - Here is to check whether nginx has started. If it has not started, start nginx first. If it has not started after 3 seconds, the keepalived process will also be closed, so that keepalived can take over and provide high availability. Here, The keepalived service provides high availability, while nginx provides load balancing of back-end web servers.
You also need to add execution permissions to the script, as follows [plain] view
plaincopy
- chmod +x /etc/keepalived/check_nginx.sh
5. Start the service and test
Let me talk about it first. On WEB_1, I use the system auto I brought the apache web server last night, which is more trouble-free. In this case, I only need to start the master-slave keepalived, because it will use the check_nginx.sh script to automatically start nginx. Everything is started.
Visit http://192.168.207.140 to access the content of the three back-end web servers in rotationHere we turn off the main keepalived service to test high availabilityThen it will be in /var/log/ on the slave keepalived server messages saw a log like this
[plain] view
plaincopy
-
Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 19 17:42:45 localhost Keepalived_vrrp : VRRP_Instance(VI_1) Entering MASTER STATE
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added
- Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168. 207.140 added Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.
-
Continue to visit http://192.168.207.140, you can still access the three back-end web servers
Then open the original owner keepalived, you can observe the log display of the original slave keepalived server
[plain] view
plaincopy
- Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
- Apr 1 9 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
- Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
- Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance (VI_1) removing protocol VIPs.
- Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed
- Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168 .207.140 removed
- Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.
It means that the original master-slave result has been restored.
In the production environment, the back-end machine may also hang up, but you don’t have to worry about it, nginx will automatically allocate the session to a good back-end web server
ok, here you go It's all over, practice personal testing, I wish you success
From: http://blog.csdn.net/zmj_88888888/article/details/8825471
The above has introduced nginx+keepalived high-availability load balancing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn