Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
上一次我们介绍了kairosdb的集群搭建,现在我们在集群的基础上搭建一个负载均衡,就是用Nginx作为一个代理服务器即可。
一、环境
我们需要单独使用一个服务器作为Nginx服务器,我使用的环境为:
服务器说明 | CPU | Memory | Disk | IP | OS | Software |
Nginx | 2 core | 4 g | 100 g | 10.10.10.174 | centos7 | nginx-1.10.0.tar.gz |
二、安装Nginx
# tar zxvf nginx-1.10.0.tar.gz
# cd nginx-1.10.0
需要安装 gcc-c++否则报错./configure: error: can not define uint32_t
# yum -y install gcc gcc-c++ autoconf automake
#./congigure --prefix=/usr/local/nginx;
#make install;
三、修改配置
# cd /usr/local/nginx/conf
#vim nginx.conf
内容如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream myServer{
server 10.10.10.172:8080 weight=1; #这里是你自己要做负载均衡的服务器地址1
server 10.10.10.173:8080 weight=1; #这里是要参与负载均衡的地址2
}
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"';
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name 10.10.10.174;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://myServer;
#root html;
#index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
保存之后启动,Nginx即可,命令如下:
cd usr/local/nginx/sbin
./nginx
重启命令如下:
cd /usr/local/nginx/sbin
./nginx -s reload
检查Nginx的配置是否正确
nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
cd /usr/local/nginx/sbin
./nginx -t
四、验证
启动好Nginx之后,访问10.10.10.174:80,他会自动访问到10.10.10.172:8080或者10.10.10.173:8080。