您的位置:首页 > 运维架构 > 反向代理

Nginx的反向代理和负载均衡应用实践

2016-11-06 14:06 447 查看
Nginx的反向代理和负载均衡应用实践 |--------------web1 www.niutianwen.org Client----------------->LB1------------------>| 访问 www.niutianwen.org |--------------web2 www.niutianwen.org 一: 反向代理和负载均衡集群的环境准备
1: 需要三台虚拟机作为本次实践的平台搭建: LB1 192.168.1.113 (Nginx的主负载均衡器) LB1 192.168.1.114 (Nginx的负负载均衡器) 后面用于结合keepalived作高可用集群来保证Nginx的反向代理和负载均衡集群的高可用 web1 192.168.1.109 web节点服务器1 web2 192.168.1.110 web节点服务器2
二:软件准备 系统平台Centos6.6 x86.64 软件:nginx-1.6.3.tar.gz 首先在web1和web2上面配置用于测试的web服务(两台后端节点web的配置操作相同) 虽然我配置了两台虚拟主机 但在这里我们只使用www.niutianwen.org来做测试。 1:安装编译安装nginx的依赖软件 配置yum源(本人选择的是阿里的Yum源地址) #mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup #wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #yum clean all #yum makecache #yum install pcre pcre-devel openssl openssl-devel -y 2:安装nginx软件包 #wget http://nginx.org/download/nginx-1.6.3.tar.gz #useradd nginx -s /sbin/nologin -M #cd nginx-1.6.3 #./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with- http_ssl_module #make #make install # ln -s /application/nginx-1.6.3/ /application/nginx # vim /application/nginx/conf/nginx.conf
worker_processes  1;
events {
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"';
sendfile        on;
keepalive_timeout  65;
server {
listen       80;
server_name  bbs.niutianwen.org;
location / {
root   html/bbs;
index  index.html index.htm;
}

access_log  logs/access_bbs.log  main;
}

server {
listen       80;
server_name  www.niutianwen.org;
location / {
root   html/www;
index  index.html index.htm;
}
access_log  logs/access_www.log  main;
}
}

#mkdir /application/nginx/html/www (创建www.niutianwen.org这台虚拟主机的网站根目录)
#/application/nginx/sbin/nginx -t (测试配置文件有无语法错误)
#/application/nginx/sbin/nginx (启动nginx服务) #echo "192.168.109 www" > /application/nginx/html/www/index.html 以上操作在web2上面相同,除了: #echo "192.168.110 www" > /application/nginx/html/www/index.html 这样可以让我方便的看到测试后的效果!
3:web1和web2进行本机测试:如下表示成功。
web1 web2 # vim /etc/hosts # vim /etc/hosts 192.168.1.109 www.niutianwen.org 192.168.1.110 www.niutianwen.org # curl www.niutianwen.org # curl www.niutianwen.org 192.168.1.109 www 192.168.1.110 www
然后我们实现一个简单的反向代理,负载均衡: 这里先只使用Lb1主负载均衡器做配置,后面配置负载均衡器高可用时会使用到Lb2。 安装nginx的过程如上,不在陈述,配置如下: 在这里需要多介绍一下,阿里开发的一个模块可以检测后端节点的状态信息,如有需要可以再编译 安装nginx的时 候加入该模块,操作如下: #wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master #unzip master #cd nginx-1.10.2/ #patch -pl < ../nginx_upstream_check_module-master/check_1.5.12+.patch #./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with- http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module-master/ (就是该模块) #make #make install (如果是给已安装的nginx安装补丁就不需要执行此步奏了!) #vim /application/nginx-1.6.3/conf/nginx.conf
worker_processes  1;
pid        /var/run/nginx.pid;
events {
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"';
sendfile        on;
keepalive_timeout  65;
upstream www_server_pools {                   (负载均衡就依赖于此模块)
server 192.168.1.109:80  weight=1;
server 192.168.1.110:80  weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http; (检测后端真实节点状态信息)
}
server {
listen       80;
server_name  www.niutianwen.org;
access_log  logs/www.access.log  main;
location / {
proxy_pass http://www_server_pools; (通过proxy_pass 的代理方式把请求发送 到实现定义好的对应upstream组的名字上)
proxy_set_header Host  $host;         (让反向代理主机明确告诉节点服务器要找哪个虚拟主机)
proxy_set_header X-Forwarded-For $remote_addr;  (让后端真实节点记录用户的真实IP)
}
location /status {                                                     (访问该目录可以获得后端节点的状态图)
check_status;
access_log off;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}

#/application/nginx/sbin/nginx -s stop
#/application/nginx/sbin/nginx
最后一步测试:
1:建议使用这几台实验节点以外的机器来做访问 (需要做好解析) 2:关闭防火墙iptables和selinux,减少不必要的麻烦。 至此,单台的反向代理就ok了,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息