Nginx安装,配置实现Tomcat负载均衡
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,具备占有内存少,并发能力强的特点。在高连接并发的情况下,是Apache服务器不错的替代品。
1.安装配置
1.1依赖安装
yum install pcre pcre-devel
1.2安装nginx
# 解压并进入解压目录 tar -zxvf nginx-1.9.5.tar.gz cd nginx-1.9.5 # 配置安装在默认的/usr/local目录下 ./configure # 编译安装 make && make install
1.3拷贝配置文件至/etc/nginx
cp -r /usr/local/nginx/conf/nginx.conf /etc/nginx
1.4将nginx定义为服务
# 编辑nginx启动脚本 vi /etc/init.d/nginx
输入如下Shell命令
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/etc/nginx/nginx.conf nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
完成后可通过如下命令对nginx进行启动
service nginx start
1.5添加开机启动
vi /etc/rc.d/rc.local
添加如下配置:
# tomcat启动命令 /usr/local/tomcat/bin/startup.sh # 加载指定的配置后启动nginx /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf /usr/local/nginx/sbin/nginx
1.6测试
在浏览器中输入http://localhost,若展现出欢迎界面,则说明配置成功
二、Nginx实现Tomcat的负载均衡
2.1实现目标
目标清单:
1)210、211和212三台主机上各安装一个Tomcat,利用统一的8080接口提供服务;
2)210和211还各安装了一个Nginx,利用统一的80接口提供请求分发并实现Tomcat负载均衡的目的;
3)可利用http://192.168.31.210和http://192.168.31.211两个http地址随机访问到最底层的Tomcat,但在实际的生产环境中,对外只提供一个url地址,因此需要keepalived来提供VIP(192.168.31.200)来实现;
4)keepalived安装在210和211两台主机上,它还有一个目的就是监控本机Nginx的运行状态。例如:当210的Nginx处于不可运行状态时,keepalived将VIP飘逸到211,使211上的keepalived继续利用VIP对外提供访问入口。
2.2实现过程
2.2.1在各主机上安装Tomcat
(此处省略一千字......)
2.2.2在210和211上安装配置Nginx
安装已在第1节中实现,直接进行Nginx的配置。
vi /etc/nginx/nginx.conf
主要的配置内容如下:
worker_processes 1; # 指定error日志和pid文件的路径 error_log /data/logs/nginx/error.log; 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"'; # 指定access日志文件的路径 access_log /data/logs/nginx/access.log main; sendfile on; # tcp_nopush on; keepalive_timeout 65; # 定义负载组 upstream upstream1 { # 对应具体若干个Tomcat服务器的访问地址 server 192.168.31.210:8080; server 192.168.31.211:8080; server 192.168.31.212:8080; } server { listen 80; # 服务器host,不同的主机有不同的名称 server_name 192.168.31.211; # 定义根路径访问规则 location / { root html; index index.html index.htm; # 访问地址,upstream1最终会被负载组中的某个server替换成真实的地址 proxy_pass http://upstream1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
由于在Nginx的启动脚本中修改了所加载的配置文件路径为/etc/nginx(默认为/usr/local/nginx/conf),因此在启动之前还需要将mime.types文件拷贝于此
cp /usr/local/nginx/conf/mime.types /etc/nginx
2.2.3重启210和211上的Nginx后测试
service nginx restart
1)输入http://192.168.31.210和http://192.168.31.211后,如果在浏览器中能呈现出Tomcat的首页,说明启动成功;
2)查看pid和日志目录,如果出现pid和access.log文件,则说明配置的文件路径有效;
3)见下图:
红框中的信息是我在tomcat首页中自己加的,反复刷新页面,如果IP地址在210 - 212之间变化,则说明Nginx的负载均衡配置成功。