: This article mainly introduces nginx + keepalived high-availability server load balancer. if you are interested in the PHP Tutorial, refer to it. Let's just talk about nginx installation and configuration, as well as server load balancer. you can refer to another article I wrote, "nginx server load balancer practice", and what about server load balancer, let's take a look at the two other articles I have written: lvs + keepalived server load balancer and haproxy + keepalived server load balancer. What are the differences between the three types of server load balancer, let's take a look at the reposted article "Introduction and Comparison of software-level load balancer (LVS/HAProxy/Nginx) features". the configuration steps are as follows:
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
-
- Master 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 port 8080
- WEB_3: 192.168.207.131 port 8080
2. customize the nginx configuration file
Operate on 192.168.207.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
- # Running user
- User nginx;
- # Start a process
- Worker_processes 2;
- # Global error logs and PID files
- Error_log logs/error. log notice;
- Pid logs/nginx. pid;
- # Working mode and maximum number of connections per process
- Events {
- Use epoll;
- Worker_connections 1024; # 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 indicates the multimedia types supported by nginx. you can view the supported multimedia types in conf/mime. types.
- Default_type application/octet-stream; # default data type
- # Set the log format
-
- Log_format main '$ remote_addr-$ remote_user [$ time_local]'
- '"$ Request" $ status $ bytes_sent'
- '"$ Http_referer" "$ http_user_agent "'
- '"$ Gzip_ratio "';
-
- Log_format download '$ remote_addr-$ remote_user [$ time_local]'
- '"$ Request" $ status $ bytes_sent'
- '"$ Http_referer" "$ http_user_agent "'
- '"$ Http_range" "$ sent_http_content_range "';
- # Set request buffer
- Client_header_buffer_size 1 k;
- Large_client_header_buffers 4 4 k;
- # Enable the gzip module
- # Gzip on;
- # Gzip_min_length 1100;
- # Gzip_buffers 4 8 k;
- # Gzip_types text/plain;
- # Output_buffers 1 32 k;
- # Postpone_output 1460;
- # Setting access log
- Access_log logs/access. log main;
- Client_header_timeout 3 m;
- Client_body_timeout 3 m;
- Send_timeout 3 m;
- Sendfile on;
- Tcp_nopush on;
- Tcp_nodelay on;
- Keepalive_timeout 65;
- # Set the server list of server load balancer
-
- Upstream mysvr {
- # The weigth parameter indicates the weight. a higher weight indicates a higher probability of being assigned.
- Server 192.168.207.129: 80 weight = 5;
- Server 192.168.207.130: 8080 weight = 5;
- Server 192.168.207.131: 8080 weight = 5;
- }
- Server {# This is the web service, listening to port 8080
- Listen 8080;
- Server_name 192.168.207.131; # this parameter varies depending on the system ip address.
- Index index.html index.htm;
- Root/var/www/html;
- # Error_page 500 502 503 504/50 x.html;
- # Location =/50x.html {
- # Root html;
- #}
- }
- # Set virtual hosts
- Server {
- Listen 80;
- Server_name 192.168.207.140; # VIP here
- # Charset gb2312;
- # Set access logs for the current virtual host
- Access_log logs/three. web. access. log main;
- # If you access/img/*,/js/*,/css/* resources, you can directly retrieve the local file without passing squid
- # This method is not recommended if there are many files, because the squid cache works better.
- # Location ~ ^/(Img | js | css )/{
- # Root/data3/Html;
- # Expires 24 h;
- #}
- # Enable server load balancer "/"
- Location /{
- Proxy_pass http: // mysvr; # Use the backend web server in this format
- Proxy_redirect off;
- Proxy_set_header Host $ host;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
- Client_max_body_size 10 m;
- Client_body_buffer_size 128 k;
- Proxy_connect_timeout 90;
- Proxy_send_timeout 90;
- Proxy_read_timeout 90;
- Proxy_buffer_size 4 k;
- Proxy_buffers 4 32 k;
- Proxy_busy_buffers_size 64 k;
- Proxy_temp_file_write_size 64 k;
- }
- # Set the address for viewing Nginx status, add -- with-http_stub_status_module parameters during installation
- Location/NginxStatus {
- Stub_status on;
- Access_log on;
- Auth_basic "NginxStatus ";
- Auth_basic_user_file conf/htpasswd; # set the access password, htpasswd-bc filename username password
- }
- }
- }
3. customize the keepalived configuration file
[Plain] view plaincopy
- Vi/etc/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 script
- Interval 2 ### monitoring time
- Weight 2 ### unclear
- }
- Vrrp_instance VI_1 {
- State MASTER ### set as MASTER
- Interface eth0 ### monitor Nic
- Virtual_router_id 51 ### the two servers must be the same
- Priority 101 ### weight MASTRE must be higher than BAUCKUP
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- Track_script {
- Chk_http_port ### service for monitoring execution
- }
- Virtual_ipaddress {
- 192.168.207.140 ### VIP address
- }
- }
4. write a custom script
[Plain] 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' # check whether any nginx process assigns the value to variable.
- If [$ A-eq 0]; then # if no process is worth zero
- /Usr/local/nginx/sbin/nginx
- Sleep 3
- If ['PS-C nginx -- no-header | wc-L'-eq 0]; then
- /Etc/init. d/keepalived stop # end the keepalived process.
- Fi
- Fi
Check whether nginx is started. if nginx is not started, start nginx first. if it has not been started three seconds later, the keepalived process is also disabled, in this way, keepalived can take over and provide high availability. here, the keepalived service provides high availability, while nginx provides load balancing for backend web servers.
Add the execution permission to the script as follows:
[Plain] view plaincopy
- Chmod + x/etc/keepalived/check_nginx.sh
5. start the service and test
Here, let's talk about it first. on WEB_1, I used the system's self-contained apache web server last night, which is quite easy. 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.
All started.
Access http: // 192.168.207.140 to access the content of the three backend web servers.
Here we turn off the master keepalived service to test high availability.
Then, the logs are displayed in/var/log/messages on the keepalived server.
[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 access http: // 192.168.207.140. you can still access the three backend web servers.
Then open the original master keepalived, and you can observe the log display of the original 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 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance (VI_1) stored Ed 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 indicates that the original master-slave result is restored.
In the production environment, backend machines may also fail, but you don't have to worry about it. nginx will automatically allocate the session to a good backend web server.
OK. Now it's all over. practice the test and wish you success.
From: http://blog.csdn.net/zmj_88888888/article/details/8825471
The above introduces nginx + keepalived high-availability server load balancer, including some content, hope to be helpful to friends who are interested in PHP tutorials.