keepalived+nginx实现负载均衡高可用配置方案

in 高性能web with 0 comment

废话不多说了。直接上配置过程。

架构是前端一台nginx做负载均衡,配置keepalived实现高可用。

web1: LAMP   (ip:192.168.213.3)

web2:LNMP    (ip:192.168.213.4)

mysql: 192.168.213.5 (兼做keepalived+nginx备服务器)

nfs:192.168.213.6   (通过nfs实现共享web目录,兼做keepalived+nginx master)

1.首先安装web
web1和web2 分别搭建好LAMP LNMP环境。配置好网站,具体步骤就省略了。
//两台服务器新建用户www
#groupadd www -g 1000 #useradd -g 1000 -M -s /sbin/nologin www

2.安装nfs
#yum install -y nfs-utils #groupadd www -g 1000 #useradd -g 1000 -M -s /sbin/nologin www #vi /etc/exports /data/www 192.168.213.0/24(rw,sync,root_squash,anonuid=1000,anongid=1000) //1000为www用户 #/etc/init.d/rpcbind start //启动服务 #/etc/init.d/nfs start

3.安装mysql
步骤省略

反向代理配置

4.在nfs上安装nginx前端做反向代理兼负载均衡
#cd /opt //进入到nginx源码包目录 #tar -zxf nginx-1.6.2.tar.gz #cd nginx-1.6.2 #yum install -y pcre pcre-devel //nginx所需要的支持库 #./configure --prefix=/usr/local/nginx --with-pcre #make && make install #cd /usr/local/nginx/conf
#vi nginx.conf //编辑配置文件,以下是我自己的,各位可自行配置具体参数
#user options user www www; #CPU Core options worker_processes 1; ##nginx Process options pid /usr/local/nginx/logs/nginx.pid; # [ debug | info | notice | warn | error | crit ] error_log /usr/local/nginx/logs/error.log debug; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200;

events
{
use epoll;
#maxclient = worker_processes * worker_connections / cpu_number
worker_connections 51200;
}

http
{
include mime.types;
default_type application/octet-stream;
log_format combined_realip '$remote_addr $http_x_forwarded_for $remote_user [$time_local]'
'$host"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent"'
'"$gzip_ratio"';
#log_format download '$remote_addr -$remote_user [$time_local]'
#'$host"$request" $status $bytes_sent'
#'"$http_referer" "$http_user_agent"'
#'"$http_range" "$sent_http_content_range"';
access_log logs/789.access.log combined_realip;
#charset gb2312;

#General options
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

sendfile on;

#timeouts
#keepalive_timeout 60;

#TCP options
tcp_nopush on;
tcp_nodelay on;

#fastcgi options
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#gzip compression
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain text/css application/x-javascript application/xml;
gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;

#virtual hosts options
include vhosts/*.conf;
}
#mkdir vhosts //建立虚拟主机目录
#vi vhosts/upstream.conf
upstream web { server 192.168.213.3 max_fails=3 fail_timeout=20s; server 192.168.213.4 max_fails=3 fail_timeout=20s; } server { listen 80; server_name www.789.com 192.168.213.100;

location / {

proxy_pass http://web/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

5.同上,在mysql所在服务器上也安装nginx 作为备机。

web配置

6.1在web1 web2上分别建立目录/data/www
#mkdir /data/www
#chown -R www.www /data/www

6.2安装nfs客户端(web1和web2都安装)
#yum install -y nfs-utils
#showmount -e 192.168.213.6
#mount -t nfs 192.168.213.6:/data/www /data/www

6.3web1、web2虚拟主机配置
web1:LAMP
#vi /usr/local/apache2/conf/httpd.conf

  1. Include conf/extra/httpd-vhosts.conf       //取消前面的注释符,开启此项,虚拟主机配置文件
  2. Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
  3. </IfModule>
    ServerAdmin you@example.com
    ServerName localhost:80

#vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

//最末行添加如下主机信息

<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.123.com
ErrorLog "logs/123-error_log"
CustomLog "logs/123-access_log" common
</VirtualHost>

web2:LNMP

#vi /usr/local/nginx/conf/nginx.conf

//在最后的上面添加如下配置信息

server

{
listen 80;
server_name www.456.com;
index index.html index.htm index.php;
root /data/www;

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

}
}

6.4.启动各服务

#/etc/init.d/httpd start   (web1)

#/usr/local/nginx/sbin/nginx (web2)

#/usr/local/php/sbin/php-fpm start (web2)

#/etc/init.d/mysqld restart (mysql)

//web端均已经挂载nfs共享文件夹

7.测试

在本地host文件中添加域名解析记录
www.123.com 192.168.213.3
www.456.com 192.168.213.4
www.789.com 192.168.213.100

客户端浏览器输入:http://192.168.213.100   或者输入:www.789.com    
正常的情况下应该是123.com和456.com页面交替出现。如有问题请检查日志,再根据日志修改查找原因。

 

 

Responses