Words do not say more, nginx installation and configuration, and load balancing, you can see me write another article, "Nginx load Balancing Combat", as well as about load balancing, we can look at the other two articles I wrote, one is "lvs+keepalived load balance", the other is " haproxy+keepalived load balancer, three kinds of load balancing differences, you can look at my reproduced an article "Software-level load balancer (Lvs/haproxy/nginx) characteristics of the introduction and contrast," the following directly into 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 80 Port
- web_2:192.168.207.130 8080 Port
- web_3:192.168.207.131 8080 Port
2. Customizing the Nginx configuration file
Operating on 192.168.207.130 and 192.168.207.131
[Plain] View plaincopy
- Useradd Nginx
- Vi/usr/local/nginx/conf/nginx.conf
The contents are as follows:
[Plain] View plaincopy
- #运行用户
- User Nginx Nginx;
- #启动进程
- Worker_processes 2;
- #全局错误日志及PID文件
- Error_log Logs/error.log Notice;
- PID Logs/nginx.pid;
- #工作模式及每个进程连接数上限
- Events {
- Use Epoll;
- Worker_connections 1024; #所以nginx支持的总连接数就等于worker_processes * worker_connections
- }
- #设定http服务器, using its reverse proxy function to provide load balancing support
- HTTP {
- #设定mime类型
- Include Mime.types; #这个是说nginx支持哪些多媒体类型, you can go to Conf/mime.types to see which multimedia is supported
- Default_type Application/octet-stream; #默认的数据类型
- #设定日志格式
- 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";
- #设定请求缓冲
- Client_header_buffer_size 1k;
- Large_client_header_buffers 4 4k;
- #开启gzip模块
- #gzip on;
- #gzip_min_length 1100;
- #gzip_buffers 4 8k;
- #gzip_types Text/plain;
- #output_buffers 1 32k;
- #postpone_output 1460;
- #设定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;
- #设定负载均衡的服务器列表
- Upstream Mysvr {
- #weigth参数表示权值, the higher the weight, the greater the chance 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 {#这个是设置web服务的, listening on port 8080
- Listen 8080;
- server_name 192.168.207.131; #这个根据系统ip变化
- Index index.html index.htm;
- root/var/www/html;
- #error_page 502 503 504/50x.html;
- #location =/50x.html {
- # root HTML;
- #}
- }
- #设定虚拟主机
- server {
- Listen 80;
- server_name 192.168.207.140; #这里是VIP
- #charset gb2312;
- #设定本虚拟主机的访问日志
- Access_log Logs/three.web.access.log Main;
- #如果访问/img/*,/js/*,/css/* Resources, then take the local file directly, not through squid
- #如果这些文件较多, this method is not recommended because the cache effect is better through squid
- #location ~ ^/(IMG|JS|CSS)/{
- # root/data3/html;
- # expires 24h;
- #}
- #对 "/" Enable load balancing
- Location/{
- Proxy_pass Http://mysvr; #以这种格式来使用后端的web服务器
- 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 10m;
- Client_body_buffer_size 128k;
- Proxy_connect_timeout 90;
- Proxy_send_timeout 90;
- Proxy_read_timeout 90;
- Proxy_buffer_size 4k;
- Proxy_buffers 4 32k;
- Proxy_busy_buffers_size 64k;
- Proxy_temp_file_write_size 64k;
- }
- #设定查看Nginx状态的地址, add the--with-http_stub_status_module parameter to the installation
- Location/nginxstatus {
- Stub_status on;
- Access_log on;
- Auth_basic "Nginxstatus";
- Auth_basic_user_file conf/htpasswd; #设置访问密码, HTPASSWD-BC filename username password
- }
- }
- }
3. Customizing the keepalived configuration file
[Plain] View plaincopy
- Vi/etc/keepalived/keepalived.conf
The contents are 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" # # #监控脚本
- Interval 2 # # #监控时间
- Weight 2 # # #目前搞不清楚
- }
- Vrrp_instance Vi_1 {
- State MASTER # # # is set primarily
- Interface Eth0 # # # Monitor network card
- VIRTUAL_ROUTER_ID 51 # # # This two servers must be the same
- Priority 101 # # # Weight value Mastre must be higher than bauckup
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- Track_script {
- Chk_http_port # # # services to perform monitoring
- }
- virtual_ipaddress {
- 192.168.207.140 # # # VIP Address
- }
- }
4. Write a custom script
[Plain] View plaincopy
- vi/etc/keepalived/check_nginx.sh
The contents are as follows:
[Plain] View plaincopy
- !/bin/bash
- A= ' ps-c nginx--no-header |wc-l ' # # See if there are nginx processes assigning values to variable a
- 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 # # ends the keepalived process
- Fi
- Fi
here is to check if Nginx is started well, if not started, first start Nginx, after 3 seconds after the start is not good, then the keepalived process is also closed, so from keepalived can take over, provide high availability, here, Keepalived services provide high availability, and Nginx provides load balancing for back-end Web servers.
The script is also given execute permissions, as follows
[Plain] View plaincopy
- chmod +x/etc/keepalived/check_nginx.sh
5. Start the service and test
Here first say Ah, on the web_1, I use the system comes with the Apache Web server last night, it is easier, so, I just start a good master and slave keepalived OK, because it will use the check_nginx.sh script automatically start Nginx.
It's all up and ready.
Access to http://192.168.207.140 can rotation access to three Web server content on the backend
Here we turn off the main keepalived service to test high availability
The logs are then seen from/var/log/messages on the keepalived server.
[Plain] View plaincopy
- APR 17:42:44 localhost keepalived_vrrp:vrrp_instance (vi_1) Transition to MASTER State
- APR 17:42:45 localhost keepalived_vrrp:vrrp_instance (vi_1) Entering MASTER State
- APR 17:42:45 localhost keepalived_vrrp:vrrp_instance (vi_1) setting protocol VIPs.
- APR 17:42:45 localhost keepalived_vrrp:vrrp_instance (vi_1) sending gratuitous ARPs on eth0 for 192.168.207.140
- APR 17:42:45 localhost keepalived_vrrp:netlink reflector reports IP 192.168.207.140 added
- APR 17:42:45 localhost keepalived_healthcheckers:netlink reflector reports IP 192.168.207.140 added
- APR 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 and still have access to three Web servers on the backend
and then the original keepalived open, you can observe the origin from the keepalived server log display
[Plain] View plaincopy
- APR 17:42:50 localhost keepalived_vrrp:vrrp_instance (vi_1) sending gratuitous ARPs on eth0 for 192.168.207.140
- APR 17:44:06 localhost keepalived_vrrp:vrrp_instance (vi_1) Received higher Prio advert
- APR 17:44:06 localhost keepalived_vrrp:vrrp_instance (vi_1) Entering BACKUP State
- APR 17:44:06 localhost keepalived_vrrp:vrrp_instance (vi_1) removing protocol VIPs.
- APR 17:44:06 localhost keepalived_vrrp:netlink reflector reports IP 192.168.207.140 removed
- APR 17:44:06 localhost keepalived_healthcheckers:netlink reflector reports IP 192.168.207.140 removed
- APR 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.
indicates that the original master-slave result has been restored.
in the production environment, the back-end machine may also be hung up, but you don't have to worry about it, Nginx will automatically assign the session to a good backend Web server .
OK, here it's all over, practice pro-Test, wish June success
from:http://blog.csdn.net/zmj_88888888/article/details/8825471
The above describes the nginx+keepalived high-availability load balancing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.