1、实验前准备
所需两台主机如下:
node1:eht0 192.168.4.82 eht0:0 192.168.4.98 在此说明一下,因为实验条件有限,这里为一台机器网卡配置了两个IP,但不不影响实验效果的。
node2:192.168.4.97
2、软件安装
node1和node2的nginx安装如下
A、具体安装步骤如下:
1、解决依赖关系
yum install gcc openssl-devel pcre-devel zlib-devel
2、创建nginx用户
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx
3、编译安装
./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/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
make && make install
B、为nginx添加syv脚本并启动nginx服务
#vi /etc/rc.d/init.d/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 为这个脚本添加执行权限 #chmod +x /etc/rc.d/init.d/nginx 添加到服务列表 #chkconfig --add nginx #chkconfig nginx on #service nginx start //启动nginx服务
此时如果不修改任何东西在浏览器里输入对应的IP进行访问就可以看到如下界面
3、接下来配置node1,使其单独访问两个IP时能有所区分
在配置node1之前先说明一下怎添加eht0:0具体如下
#cd /etc/sysconfig/network-scripts/
#cp ifcfg-eth0 ifcfg-eth0:0
#vi ifcfg-eth0:0 内容如下即可
DEVICE=eth0:0
BOOTPROTO=static
IPADDR=192.168.4.98
NETMASK=255.255.255.0
HWADDR=00:0C:29:E2:D4:CF
ONBOOT=yes
#service network restart //重启网卡
node1 具体操作如下
为node1的192.168.4.98创建页面 #cd /usr/html/ #mkdir smile #echo "www.smile.com" > index.html
修改配置文件,具体如下内容
server{ //4.82的server listen 192.168.4.82:80; server_name www.82.com; location / { root html; index index.html; } } server{ listen 192.168.4.98:80; //4.98的server server_name www.98.com; location / { root html/smile; index index.html; } } #service nginx restart //重启nginx使配置文件生效
浏览器里输入对应的IP进行访问就可以看到如下界面
4、配置node2
A、实现反向代理,配置如下
server{ //4.82的server
listen 192.168.4.97
server_name www.82.com;
location / {
proxy_pass http://192.168.4.82
}
}
#service nginx restart //重启nginx使配置文件生效
访问node2 192.168.4.97反向到后端真正的服务器node1上,界面如下
到此为止反向代理配置成功,接下来看nginx如何实现负载均衡的。
B、实现负载均衡,配置如下
vim /etc/nginx/nginx.conf
http {
include mime.types;
default_type application/octet-stream; proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m; proxy_temp_path /var/www/cache/tmp;
sendfile on;
keepalive_timeout 65;
upstream cluster {
server 192.168.4.82 weight=1;
server 192.168.4.98 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://cluster;
proxy_cache mycache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#service nginx restart
访问页面,如下图所示
5、安装配置第三方模块,实现upstream中对后端http server的健康状态检测:
模块下载地址:https://github.com/cep21/healthcheck_nginx_upstreams;模块名称:ngx_http_healthcheck_module
安装配置方法
1、首先解压healcheck模块到某路径下,这里假设为/tmp/healthcheck_nginx_upstreams
2、对nginx打补丁
首先解压nginx,并进入nginx源码目录:
tar xf nginx-1.0.11.tar.gz
cd nginx-1.0.11
patch -p1 < /tmp/healthcheck_nginx_upstreams/nginx.patch
而后编译nginx,在执行configure时添加类似下面的选项:
--add-module=/tmp/healthcheck_nginx_upstreams
所以,这里就使用如下命令:
./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 \
--add-module=/tmp/healthcheck_nginx_upstreams
make && make install
此模块支持的指令有:
healthcheck_enabled //启用此模块
healthcheck_delay //对同一台后端服务器两次检测之间的时间间隔,单位毫秒,默认为1000;
healthcheck_timeo //进行一次健康检测的超时时间,单位为毫秒,默认值2000;
healthcheck_failcoun //对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器;
healthcheck_sen //为了检测后端服务器的健康状态所发送的检测请求;如:healthcheck_send "GET /health HTTP/1.0" 'Host: www.magedu.com';
healthcheck_expected //期望从后端服务器收到的响应内容;如果未设置,则表示从后端服务器收到200状态码即为正确;
healthcheck_buffer 健康状态检查所使用的buffer空间大小;
具体实现方法如下
#vi /etc/nginx/nginx.conf //编辑node2的配置文件
location /nginx_status {
healthcheck_status; //启用检测后端服务器状态
}
#service nginx restart
后再浏览器输入http://192.168.4.97/nginx_status 访问即可
转载于:https://blog.51cto.com/sxhxt/1278771