nginx实现tomcat的负载均衡及企业内部应用的代理
192.168.3.87 mysql+redis+tomcat
192.168.3.112 nginx:6688192.168.3.86 nginx+h5
环境准备
a.安装包
open jdk 1.8
tomcat 8.0
nginx 1.1.13.tar.gz
mysql5.5.48.tar.gz
b.解析域名admin.chinasoft.com和ops.chinasoft.com到nginx负载均衡服务器
①后端3.87和3.112jdk的部署
yum install -y java-1.8.0-openjdk
yum install -y redis
验证是否安装成功
java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode
②tomcat的部署
tar -zxvf apache-tomcat-8.0.36.tar.gz
mv apache-tomcat-8.0.36 /usr/local/tomcat
将tomcat的命令加入环境变量中
# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
source /etc/profile.d/tomcat.sh
修改http的web端口为8888
vim /usr/local/tomcat/conf/server.xml
<Connector port="8888" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
启动服务并验证是否正常启动
startup.sh
ss -tunlp|grep 8888
③将war包放入webapps目录下,重新启动tomcat,验证是否成功,错误日志可以查看catalina.out文件
将域名解析指向nginx负载均衡器:192.168.8.86 admin.chinasoft.com
配置nginx负载均衡器
将具体的定义防止到/etc/nginx/conf文件目下
在nginx.conf中定义目录包含:
include /etc/nginx/conf/*.conf;
在conf目录下分别定义后端服务器,nginx中h5和zabbix等内部管理系统的定义
cat h5.conf
server {
access_log /data/logs/h5_acc.log; #访问日志
error_log /data/logs/h5_error.log info; #错误日志
server_name localhost; #因为是放置在本机,所以直接用Localhost
root /web/h5; #自定义存放web文件目录
}
cat flow_admin.conf
# 定义后端负载均衡的两台机器
upstream flows {
server 192.168.3.87:8888 weight=1;
server 192.168.3.112:6688 weight=1;
}
server {
server_name admin.chinasoft.com; #定义servername即指向nginx的域名
access_log /data/logs/flowadm_acc.log;
error_log /data/logs/flowadm_error.log info;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://flows/;
}
}
负载均衡效果:
cat ops.conf
# 定义zabbix等内部服务
server {
server_name ops.chinasoft.com;
location /zabbix/ {
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://192.168.3.108/zabbix/;
}
}
几段生产环境的例子:
ops.conf
server{ server_name ss.chinasoft.com; index index.html index.htm index.php; #zabbix server access by nginx proxy location /zabbix/ { proxy_pass http://u04zbx01.yaya.corp/zabbix/; } location /ldap/ { proxy_pass http://u04dns01.yaya.corp/ldap/; } }
flow.conf
server { server_name flowadm.chinasoft.com; access_log /data/logs/flowadm_access.log; error_log /data/logs/flowadm_error.log info; location / { proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; #proxy_pass http://flows_admin; proxy_pass http://u04flow01.yaya.corp:8888/; } }
h5.conf
upstream flows { server u04flow01.yaya.corp:8080; server u04flow02.yaya.corp:8080; } server { server_name flow.chinasoft.com; error_log /data/logs/flow_h5.log info; access_log /data/logs/flow_h5.log; location /h5/flow/{ alias /data/h5/flow/; index index.html; } location /sdkApi/{ proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; #proxy_pass http://u04flow01.yaya.corp:8080/; proxy_pass http://flows; } #error_page 404 /404.html; }
web.conf
server { server_name 1.1.1.1; server_name www.baidu.com www.aaa.com error_log /data/logs/web-acccess.log info; access_log /data/logs/web-error.log; location /{ alias /home/hailitao/; #这里为代码存放路径 index views/index/index.html; } location /api/{ proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://192.168.3.85:9783/; #这里为服务端所部署的地址 } }
share.conf
upstream bills { server u04tv01.chinasoft.com:6643; server u04tv02.chinasoft.com:6643; } server { server_name fx.chinasoft.com; error_log /data/logs/share_error.log info; access_log /data/logs/share_access.log; location /{ alias /data/haili_share/; index room.html; } location /hailishare/{ # proxy_set_header Host $host; # proxy_set_header X-Real-Ip $remote_addr; # proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://bills/; } }