linux下:nginx+tomcat实现负载均衡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux下:nginx+tomcat实现负载均衡相关的知识,希望对你有一定的参考价值。
nginx+tomcat+jdk+负载均衡配置
Nginx与tomcat都具有发布网页的功能,但是nginx只能发布html的静态网页,tomcat可以发布jsp的网页,并且nginx可以发布tomcat。
下载:jdk.xxxxxx.rpm apache-tomcat-xxxxxxxxx.tar.gz openssl.xxxxxx.tar.gz zlib.xxxx.tar.gz pcre.xxxx.tar.gz Nginx.xxxxx.tar.gz
1.jdk安装配置
上传jdk-xxxxxx到/tmp下
cd /tmp
#执行
rpm -ivh jdk-xxxx.rpm
find / -name java
#显示结果
/usr/java/jdk1.8.0_121 -----------------------------默认安装路径
#设置环境变量
vi /etc/profile
#在最后一行加入下面的内容
JAVA_HOME=/usr/java/jdk1.8.0_121
PATH=${JAVA_HOME}/bin:$PATH:$HOME/bin
export PATH JAVA_HOME
ulimit -u 65535
保存退出
执行:source /etc/profile
使变量生效
执行:echo $JAVA_HOME
输出结果为java的安装目录
测试配置效果:
java -version
显示:
java version "xxxxx" -----------------------xxxxx为jdk版本,
qij (GNU libgcj) version
find
Jdk配置完毕
2.tomcat配置:
上传apache-tomcat-xxxxxxxxx.tar.gz到/tmp下
cd /tmp
tar zxvf apache-tomcat-xxxxxxxxx.tar.gz
mv apache-tomcat-8.5.11 /opt/tomcat
vi /etc/profile
#修改环境变量,并添加tomcat的变量
JAVA_HOME=/usr/java/jdk1.8.0_121
TOMCAT_HOME=/opt/tomcat
export PATH=${JAVA_HOME}/bin/:$PATH:$HOME/bin:${TOMCAT_HOME}/bin
export PATH JAVA_HOME TOMCAT_HOME
ulimit -u 65535
执行命令:
source /etc/profile
查看环境变量:
echo $JAVA_HOME
cd $TOMCAT_HOME/bin
启动tomcat:(进入到tomcat的安装目录下,bin目录)
./catalina.sh start (关闭tomcat ./shutdown.sh stop)
(4)测试
查看tomcat是否已经启动netstat -ntl |grep 8080
浏览器地址栏输入
注意:以上配置修改完毕后要关闭服务器防火墙
CentOs6.5以下版本:service iptables stop
CentOs7版本 :systemctl stop firewalld.service (临时关闭)
systemctl disable firewalld.service (永久关闭)
systemctl status firewalld.service (查看状态)
1.nginx安装配置
yum install gcc-c++ #先安装编译包,顺序不能错,否则nginx报错
#然后依次安装openssl、zlib、pcre, 然后安装Nginx包
上传 openssl、zlib、pcre,Nginx到服务器
[[email protected]] tar zxvf openssl-fips-2.0.9.tar.gz
[[email protected]] cd openssl-fips-2.0.9
[[email protected]] ./config && make && make install
cd /tmp
[[email protected]] tar zxvf pcre-8.39.tar.gz
[[email protected]] cd pcre-8.36
[[email protected]] ./configure && make && make install
cd /tmp
[[email protected]]tar zxvf zlib-1.2.8.tar.gz
[[email protected]] cd zlib-1.2.8
[[email protected]] ./configure && make && make install
cd /tmp
[[email protected]]tar zxvf nginx-1.8.0.tar.gz
[[email protected]] cd nginx-1.8.0
[[email protected]] ./configure && make && make install
#Nginx安装完毕,然后使用命令: 测试
/usr/local/nginx/sbin/nginx -t
启动命令:/usr/local/nginx/sbin/nginx
注意:报错:/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决办法:添加软链接
ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
说明:pcre安装在/lib64中,服务器找的是/lib,找不到,添加软链接
执行
ldd /usr/local/nginx/sbin/nginx
显示
linux-vdso.so.1 => (0x00007fff991ff000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003355c00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003356400000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003361400000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x0000003361000000)
libz.so.1 => /lib64/libz.so.1 (0x0000003357400000)
libc.so.6 => /lib64/libc.so.6 (0x0000003356000000)
/lib64/ld-linux-x86-64.so.2 (0x0000003355800000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007f78774b6000)
可以通过访问http://ip/看到nginx默认页面
Nginx安装配置完毕
/usr/local/nginx/sbin/nginx -s reload --------------重启nginx
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
4.配置nginx.conf
[[email protected] nginx-1.6.2]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[[email protected] nginx-1.6.2]# vi /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 10240;
}
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_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream tomcat_server{ ############新增 upstream参数
server 192.168.1.10:8080 weight=1;
server 192.168.1.11:8080 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server; #####与上面的upstream向对应 ,此处是用来将用户的请求代理转发到定义好的后台的服务器池####
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
客户端测试:
打开浏览器访问: http://192.168.1.254
关闭192.168.1.10上的tomcat,然后继续访问:http://192.168.1.254依然可以看到tomcat页面
以上是关于linux下:nginx+tomcat实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章
ubantu 下 tomcat + nginx 实现负载均衡
nginx+tomcat实现负载均衡以及session共享(linux centos7环境)