nginx实现tomcat的负载均衡及企业内部应用的代理
生活随笔
收集整理的這篇文章主要介紹了
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 proxylocation /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.comerror_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/;} }轉載于:https://www.cnblogs.com/reblue520/p/6239745.html
總結
以上是生活随笔為你收集整理的nginx实现tomcat的负载均衡及企业内部应用的代理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 基础之 static 静态
- 下一篇: Nginx + CGI/FastCGI