ngixn+tomcat负载均衡 动静分离配置 (nginx反向代理)
文章目錄
- Tomcat主要配置文件
- Nginx負載均衡實現(xiàn)原理
- 實驗環(huán)境
- 動靜分離配置
- 創(chuàng)建靜態(tài)頁面
- 配置動態(tài)頁面
- 圖片動靜分離
Tomcat主要配置文件
Nginx負載均衡實現(xiàn)原理
- Nginx配置反向代理的主要參數(shù)
upstream 服務(wù)器池名{ }
配置后端服務(wù)器池,以提供響應(yīng)數(shù)據(jù)
proxy_pass http://服務(wù)池名
配置將訪問請求轉(zhuǎn)發(fā)給后端服務(wù)器池的服務(wù)器處理
-
動靜分離原理
服務(wù)端接受來自客戶端的請求中,既有靜態(tài)資源也有動態(tài)資源,靜態(tài)資源由Nginx提供服務(wù),動態(tài)資源Nginx轉(zhuǎn)發(fā)至后端
-
Nginx靜態(tài)處理優(yōu)勢
實驗環(huán)境
Nginx+Tomcat負載均衡,動靜分離
服務(wù)機臺數(shù)3:1nginx,2tomcat
| Nginx負載均衡器 | 192.168.136.88 |
| Tomcat-Webserver1 | 192.168.136.60 |
| Tomcat-Webserver2 | 192.168.136.10 |
nginx服務(wù)安裝與部署
重新命名
[root@localhost ~]# hostnamectl set-hostname nginx [root@nginx ~]#所有服務(wù)器關(guān)閉防護墻
[root@nginx init.d]# iptables -F [root@nginx init.d]# setenforce 0配置nginx
[root@nginx ~]# yum -y install gcc-c++ gcc pcre-devel zlib-devel [root@nginx ~]#useradd -M -s /sbin/nologin nginx [root@nginx ~]#cd nginx-1.12.0/ [root@nginx nginx-1.12.2]#./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module [root@nginx nginx-1.12.2]# make && make install [root@nginx nginx-1.12.2]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@nginx nginx-1.12.2]# cd /etc/init.d/ [root@nginx init.d]# vim nginx #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" instart)$PROG;;stop)kill -s QUIT $(cat $PIDF);;restart)$0 stop$0 start;;reload)kill -s HUP $(cat $PIDF);;*)echo "Usage: $0 {start|stop|restart|reload}"exit 1 esac exit 0 [root@nginx init.d]# chmod +x nginx [root@nginx init.d]# chkconfig --add nginx [root@nginx init.d]# service nginx start [root@nginx init.d]# netstat -ntap | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15334/nginx: master部署tomcat服務(wù)器
[root@tomcat ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ [root@tomcat ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/ 設(shè)置環(huán)境變量 [root@tomcat local]# vim /etc/profile 在末尾插入 export JAVA_HOME=/usr/local/jdk1.8.0_91 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:$JAVA_HOME/lib:${JAVA_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH [root@tomcat local]# cd /usr/local/ [root@tomcat local]# source /etc/profile [root@tomcat local]# mv apache-tomcat-8.5.16/ tomcat 系統(tǒng)識別 [root@tomcat local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/ [root@tomcat local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/ [root@tomcat02 local]# startup.sh [root@localhost local]# netstat -ntap | grep 8080 tcp6 0 0 :::8080 :::* LISTEN 22407/java動靜分離配置
在nginx中配置靜態(tài)頁面 (請求java轉(zhuǎn)發(fā)到tomcat處理)
vim /usr/local/nginx/conf/nginx.conf 在42行下添加42 location ~.*.jsp$ {43 proxy_pass http://192.168.136.40:8080; 44 proxy_set_header Host $host;45 }檢查格式
[root@nginx init.d]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful創(chuàng)建靜態(tài)頁面
cd /usr/local/nginx/html/ 站點目錄創(chuàng)建靜態(tài)頁面
vim index.html ( 刪除所有復(fù)制下面) <head> <meta http-equiv="content-type"content="text/html:charset=utf-8" > <title >靜態(tài)頁面</title> <style> body{width:35em;margin:0 auto;font-family:Tahoma,Verdana,Arial,sans-serif; } </style> </head > <body ></style></head><body><h1>靜態(tài)頁面</h1><p>這是個靜態(tài)頁面</P></body></html >ps:可以不行這么多中間 比如(style美好頁面功能,width寬度,margin:0 auto外邊距,head 頭部,body詳細內(nèi)容)
開啟服務(wù)
[root@nginx html]# service nginx stop [root@nginx html]# service nginx start配置動態(tài)頁面
在Tomcat中配置
進入站點
cd /usr/local/tomcat/webapps/創(chuàng)建動態(tài)頁面
[root@localhost webapps]# mkdir test [root@localhost webapps]# cd test/ [root@localhost test]# vim index.jsp <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>動態(tài)頁面</title> </head> <body> <div>動態(tài)頁面</div> </body> </html>開啟服務(wù)
[root@tomcat test]# shutdown.sh [root@tomcat test]# startup.sh查看一下靜態(tài)頁面結(jié)果
查看一下動態(tài)頁面結(jié)果
圖片動靜分離
nginx處理靜態(tài)圖片,tmocat處理動態(tài)頁面
在tomcat上加上標簽
[root@tomcat test]# cd /usr/local/tomcat/webapps/test/ [root@tomcat test]# vim index.jsp13 <img src="cat.jpg"/>nginx上面更改
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf 在43行插入 location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {43 root html;44 expires 30d;45 }ps:運用正則表達式,去站點尋找,緩存30天
加入圖片,加入時要注意圖片的目錄名稱要和java項目名稱一致,webapps是項目,test是項目名稱
[root@nginx html]# cd /usr/local/nginx/html/ [root@nginx html]# mkdir test [root@nginx html]# cd test/ [root@nginx test]# rz -E 輸出一張圖到這里 [root@nginx test]# ls cat.jpg開啟服務(wù)
[root@nginx test]# service nginx stop [root@nginx test]# service nginx start總結(jié)
以上是生活随笔為你收集整理的ngixn+tomcat负载均衡 动静分离配置 (nginx反向代理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web群集与日志管理Haproxy搭建
- 下一篇: ACL访问控制