Nginx主主负载均衡

  1. Nginx主机配置
ser www www;
worker_processes 8;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
	user epoll;
	worker_connections 51200;
}
http {
	include mine.types;
	default_type application/octet-stream;
	server_names_hash_bucket_size 128;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	client_max_body_size 8m;
	sendfile on;
	tcp_nopush on;
	keepalive_timeout 60;
	tcp_nodelay on;
	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffer_size 64k;
	fastcgi_buffers 4 64k;
	fastcgi_busy_buffer_size 128k;
	fastcgi_temp_file_write_size 128k;
	gzip on;
	gzip_min_length 1k;
	gzip_buffers 4 16k;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_types text/plain application/x-javascript text/css application/xml;
	gzip_vary on;

	upstream backend {
		server 192.168.1.17:80; #web server 1
		server 192.168.1.18:80; #web server 2
	}
	server {
		listen 80;
		server_name www.bee.2.com;
		location / {
			root /var/www/html;
			index index.php index.htm index.html;
			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;
			proxy_pass http://backend;
		}
		location /nginx {
			access_log off;
			auth_basic "NginxStatus";
		}
		log_format access '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';
		access_log /data/logs/access.log access;
	}
}
  1. Keepalived的实现原理
    Keepalived启用两个实例,两台Nginx主机互为备份。生成的两个VIP分别都指向目标网站,比如www.bee.2.com。另,采用DNS轮询访问该网站。
  • 第一台Keepalived配置如下:
! Configuration File for keepalived
global_defs {
	notification_email {
		bee@163.com
	}
	notification_email_from keepalived@bee.com
	smtp_server 127.0.0.1
	smtp_connect_timeout 30
	router_id LVS_DEVEL
}
vrrp_instance VI_1 {
	state MASTER
	interface eth0
	virtual_router_id 51
	priority 100
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass bee.2.com
	}
	virtual_ipaddress {
		192.168.1.8  #VIP 1
	}
}
vrrp_instance VI_2 {
	state BACKUP
	interface eth0
	virtual_router_id 52
	priority 99
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass bee.2.com
	}
	virtual_ipaddress{
		192.168.1.9  #VIP 2
	}
}
  • 第二台Keepalived的配置如下
! Configuration File for keepalived
global_defs {
	notification_email {
		bee@163.com
	}
	notification_email_from keepalived@bee.com
	smtp_server 127.0.0.1
	smtp_connect_timeout 30
	router_id LVS_DEVEL
}
vrrp_instance VI_1 {
	state BACKUP
	interface eth0
	virtual_router_id 51
	priority 99
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass bee.2.com
	}
	virtual_ipaddress {
		192.168.1.8
	}
}
vrrp_instance VI_2 {
	state MASTER
	interface eth0
	virtual_router_id 52
	priority 100
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass bee.2.com
	}
	virtual_ipaddress{
		192.168.1.9
	}
}

Keepalived实现不了应用程序级别的高可用性,需要通过shell脚本实现。
脚本/root/nginx_pid.sh

#!/bin/bash
while :
do
	nginxpid=`ps -C nginx --no-header | wc -l`
	if [ $nginxpid -eq 0 ]; then
		/usr/local/nginx/sbin/nginx
		sleep 5
		if [ $nginxpid -eq 0 ]; then
			/etc/init.d/keepalived stop
		fi
	fi
	sleep 5
done

分别在两台主机上运行

nohup sh /root/nginxpid.sh &

注意,杀nginx进程(好几个)需用killall。

pgrep -l nginx  #不使用-l,则只输出pid
killall nginx

SSL证书可考虑采用GeoTrust。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值