实现负载均衡的方式有很多种,DNS、反向代理、LVS负载均衡器(软件实现)、F5(负载均衡器,硬件,非常昂贵)这里我们只提到基于DNS,以及反向代理的方式来实现负载均衡Web服务
DNS服务器实现负载均衡的原理是基于轮询的方式实现的,直接由DNS解析系统进行随机分配。除了性能分配不均之外,还有更可怕的就是如果一台服务器down了,DNS服务器因为是基于轮询方式的负载均的,所以它并不能够探测到或者没有对付这种情况的动作,所以会继续向这台服务器分配流量,导致访问超时,给用户带来不便。并且基于DNS服务实现负载均衡web集群,需要3个公网IP,代价可想而知。现在IPv4的资源及其紧张,铺张浪费不是一个好的运维工程师,你懂得.
如果说使用nginx实现负载均衡的话
如图所示:内网有三台web server(两台Apache,一台Nginx),管理员可以根据服务器的性能,设置权重,来分配服务器的使用几率。如果某台服务器反回超时或者挂掉了,Nginx反向代理会基于max_fails这样的机制,使其不再把用户分配到那台服务器,这样比DNS服务器直接进行随机分配好的多。可靠性和利用率大大提高。你懂得.
并且基于nginx反向代理:只要有一个IP地址来绑定就可以实现nginx负载均衡,大大节省购买IP地址时的代价。当用户访问公司域名时,请求直接被发送到Nginx Server上,Nginx Server根据weight值和fail_timeout来进行合理分配服务器资源。注释:
更加人性化。反向代理还经常被称为网关服务器,因为它能够防止了公司内网Web server直接暴露在外网的环境下,在一定程度上保证了web server的安全。
NFS 全称:Network file system NFS由SUN公司开发,目前已经成为文件服务的一种标准。我们在这里,通过NFS实现存储.
NFS 跟PHP-FPM服务器为同一台都为(172.16.251.215)
一、配置Nginx反向代理
1、创建用户和组
2、编译安装Nginx
点击(此处)折叠或打开
# groupadd nginx
# useradd -g nginx -s /bin/false -M nginx
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-pcre
make && make install
3、提供Nginx服务脚本
点击(此处)折叠或打开
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
4、配置Nginx实现反向代理
点击(此处)折叠或打开
#使用的用户和组
#user nobody;
#指定工作衍生进程数(一般等于CPU的总核数或者总和数)
worker_processes 1;
#指定错误日志存放的路径,错误日志记录级别可选项[debug|info|notice|warn|error|crit]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定pid存放的路径
#pid logs/nginx.pid;
events {
#默认使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeBSD推荐采用kqueue模型
#use epoll;
worker_connections 1024; #允许的连接数
}
http {
include mime.types;
default_type application/octet-stream;
#设置使用的字符集,如果一个网站多种字符集,请不要随便设置。
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#通过$remote_addr:可以获取Nginx反向代理服务器的IP地址
#X-Forwarded-For:用以记录原有的客户端ip和原来客户端请求的服务器地址.
#$remote_user:用于记录远程客户端用户名称
#$time_local:用于记录访问时间与时区
#$request:用于记录请求URL与http协议
#$status:用于记录请求状态,列如成功状态为200,,页面找不到时状态404
#$body_bytes_sent:用于记录发送客户端的文件主体内容的大小;
#$http_referer:用于记录是从哪个页面链接访问过来的;
#$http_usr_agent:用于记录客户端浏览器的相关信息.
#采用日志格式combined记录的日志的格式是非常广泛和流行的
access_log path [format [buffer=size | off]]
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#proxy_cache_path /nginx/cache levels=1:2 keys_zone=mycache:16m
# inactive=24h max_size=1g;
upstream backend
server 172.16.251.253 weight=1 max_fails=2 fail_timeout=30s;
server 172.16.251.244 weight=1 max_fails=2 fail_timeout=30s;
server 172.16.251.208 weight=1 max_fails=2 fail_timeout=30s;
}
#upstream:该指令用于设置可以再proxy_pass和fastcgi_pass指令中使用的代理服务器
#weight:设置服务器的权重,权重数值越高,被分配到的客户端请求数越多.默认为1
#max_fails:指定的时间内对后端服务器请求失败的次数,如果检测到后端服务器无法连接及发生服务器错误(404错误除外),则标记为失败.默认为1,设为数值0将关闭这项检测
#fail_timeout:在经历参数max_fails设置的失败次数后,暂停的时间.
#down:标记服务器为永久离线状态,用于ip_hash指令
#backup:仅仅非在backup服务器全部繁忙的时候才会启用.
server {
listen 80;
server_name www.nginx.com 220.2.2.2;
#server_name 绑定域名,以及IP地址
location / {
proxy_pass http://backend;
proxy_set_header host www.nginx.com;
#proxy_cache mycache;
#proxy_cache_valid 200 301 1d;
#proxy_cache_valid 404 1m;
}
#proxy_pass:指定后端被代理的服务器
#proxy_connect_timeout:跟后端服务器连接的超时时间_发起握手等候响应超时时间
#proxy_read_timeout:连接成功后_等待后端服务器响应时间_其实已经进入后端的排队之中等候处理。
#proxy_send_timeout:后端服务器数据回传时间_就是规定时间内后端服务器必须传完所有的数据
#proxy_buffer_size:代理请求缓存_这个缓存区间会保存用户的头部信息以供Nginx进行规则处理_一般只能保存下头信息即可
#proxy_buffers:同上 告诉nginx保存单个用的几个buffer最大可用空间
#proxy_busy_buffers_size:如果系统很忙的时候可以申请更大的Proxy_buffers
#proxy_temp_file_write_size:缓存临时文件的大小;
# location / {
# root html;
# index index.php index.html index.htm;
# }
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 172.16.251.215:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
二、安装web服务,编译安装Httpd Web Server:172.16.251.208
Web Server: 172.16.251.244
1、编译安装http2.4.9,依赖于更高版本的apr和apr-util。apr全称为apache portable runtime
点击(此处)折叠或打开
#tar xf apr-1.5.0.tar.bz2
#cd apr-1.5.0
#./configure --prefix=/usr/local/apr
#make && make install
2、编译安装apr-util-1.5.2
点击(此处)折叠或打开
#tar xf apr-util-1.5.2.tar.bz2
#cd apr-util-1.5.2
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
#make && make install
3、编译安装http2.4.9
点击(此处)折叠或打开
# tar xf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event
# make && make install
4、后续的配置:
点击(此处)折叠或打开
#ln -sv /usr/local/apache/include/usr/include/httpd
#vim /etc/profile.d/httpd.sh
exportPATH=/usr/local/apache/bin:$PATH
#vim /etc/man.conf
#MANPATH /usr/local/apache/man
5、修改httpd的主配置文件,设置其Pid文件的路径 vim /etc/httpd24/httpd.conf
点击(此处)折叠或打开
PidFile "/var/run/httpd.pid"
6、补充:
(1)构建MPM为静态模块
在 全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行 configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l 来 确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。
(2)构建 MPM 为动态模块
在Unix或类似平台 中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而 不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时, 所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with- mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。
7、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:
点击(此处)折叠或打开
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
转载于:https://my.oschina.net/zijian1315/blog/213750