nginx 动静分离

proxy 192.168.3.21

nginx 192.168.3.222

tomcat 192.168.3.223

 

 

nginx:------------------------------------------------

mkdir jia images

vim /etc/nginx/conf.d/www.conf

server {
listen 80;
server_name www.jia.com;
root /usr/share/nginx/html/jia;
index index.html;
location ~ \.(png|jpg|gif)$ {
root /usr/share/nginx/html/images;
}
}

cd images

wget  http://nginx.org/nginx.png

nginx2------------------------------------------------------------------

yum install -y tomcat

mkdir /usr/share/tomcat/webapps/ROOT

vim /usr/share/tomcat/webapps/ROOT/java_test.jsp

<%@ page language='java' import='java.util.*' pageEncoding='utf-8' %>
<HTML>
<HEAD>
<TITLE>JSP Test Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println('<h1>Random number:</h1>');
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>

systemctl restart tomcat

 

lb--------------------------------------------------------------------

/etc/nginx/conf.d/default.conf中添加

upstream static {
server 192.168.3.222:80;
}
upstream java {
server 192.168.3.223:8080;
}
server {
listen 80;
server_name www.jia.com;
location / {
root /code;
index index.html;
}
location ~ .*\.(png|jpg|gif)$ {
proxy_pass http://static;
include proxy_params;
}
location ~ .*\.jsp$ {
proxy_pass http://java;
include proxy_params;
}
}

------------------------------------------------------------

mkdir /code

vim /code/index.html

-------------------------------------------------------------------------------------------------------------------------------------

<html lang='en'>
<meta charset="UTF-8"/>
<title>测试ajax和跨域访问</title>
<script src='http://libs.baidu.com/jquery/2.1.4/jquery.min.js'></script>
<head>
</head>
<script type='text/javascript'>
$(document).ready(function(){
$.ajax({
type:'GET',
url:'http://www.jia.com/java_test.jsp',
success:function(data) {
$('#get_data').html(data)
},
error:function() {
alert('fail!!,agnet');
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src='http://www.jia.com/nginx.png'>
<div id='get_data'></div>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------
nginx -t
systemctl reload nginx
访问lb的www.jia.com
断开其中nginx查看情况
只断开静态

 

只断开动态

 

posted @ 2020-03-16 15:04  Le1543  阅读(264)  评论(0编辑  收藏  举报