Nginx+Tomcat负载均衡、动静分离

七层反向代理+动静分离

Matlab 复制代码
实验配置:
nginx:20.0.0.61 代理又是静态
tomcat1:20.0.0.71
tomcat2:20.0.0.72
Matlab 复制代码
[root@nginx1 conf]# vim nginx.conf

http {
    ...
    upstream tomcat {
        server 20.0.0.71:8080 weight=1;
        server 20.0.0.72:8080 weight=1;
    }
    server {
        ...
        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~* \.jsp$ {
            proxy_pass http://tomcat;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

[root@nginx1 conf]# nginx -t
[root@nginx1 conf]# systemctl restart nginx
[root@nginx1 conf]# cd ..
[root@nginx1 nginx]# cd html/
[root@nginx1 html]# vim index.html

<html>
<body>
<h1> this is Nginx static test1 !</h2>
<img src="naruto.PNG"/>
</body>
</html>

--传入naruto.PNG--

浏览器访问20.0.0.61

Matlab 复制代码
[root@tomcat1 ~]# cd /usr/local/tomcat/
[root@tomcat1 tomcat]# cd webapps/
[root@tomcat1 webapps]# mkdir test
[root@tomcat1 webapps]# cd test/
[root@tomcat1 test]# vim index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>

[root@tomcat2 ~]# cd /usr/local/tomcat/
[root@tomcat2 tomcat]# cd webapps/
[root@tomcat2 webapps]# mkdir test
[root@tomcat2 webapps]# cd test/
[root@tomcat2 test]# vim index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>
Matlab 复制代码
71、72相同操作:
[root@tomcat1 conf]# vim server.xml
--删除--
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
--插入--
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">        <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
Matlab 复制代码
71、72相同操作:
[root@tomcat1 conf]# cd ..
[root@tomcat1 tomcat]# cd bin/
[root@tomcat1 bin]# ./startup.sh 

浏览器直接访问71、72动态页面

浏览器访问代理服务器跳转至动态页面

四层+七层+动静分离

Matlab 复制代码
实验配置:
nginx代理:20.0.0.61
静态页面和动态请求转发服务器:
nginx2:20.0.0.62
nginx3:20.0.0.63
tomcat1:20.0.0.71
tomcat2:20.0.0.72
Matlab 复制代码
[root@nginx1 conf]# vim nginx.conf

stream {
        upstream static {
            server 20.0.0.62:80 weight=1;
            server 20.0.0.63:80 weight=1;
        }
    server {
        listen 80;
        proxy_pass static;
    }
}

[root@nginx1 conf]# nginx -t
[root@nginx1 conf]# systemctl restart nginx
Matlab 复制代码
62、63相同操作:
[root@nginx2 conf]# vim nginx.conf

upstream tomcat {
        server 20.0.0.71:8080 weight=1;
        server 20.0.0.72:8080 weight=1;
}

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
         location ~* \.jsp$ {
            proxy_pass http://tomcat;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
Matlab 复制代码
[root@nginx2 html]# vim index.html 

<html>
<body>
<h1> this is Nginx static test1 !</h2>
<img src="naruto.PNG"/>
</body>
</html>

--传入naruto.PNG--

[root@nginx3 html]# vim index.html 

<html>
<body>
<h1> this is Nginx static test2 !</h2>
<img src="sasuke.PNG"/>
</body>
</html>

--传入sasuke.PNG--

浏览器访问20.0.0.61请求静态页面

浏览器访问20.0.0.61动态页面跳转

相关推荐
安冬的码畜日常7 分钟前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
l1x1n035 分钟前
No.3 笔记 | Web安全基础:Web1.0 - 3.0 发展史
前端·http·html
昨天;明天。今天。1 小时前
案例-任务清单
前端·javascript·css
zqx_72 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
惜.己2 小时前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5
什么鬼昵称3 小时前
Pikachu-csrf-CSRF(get)
前端·csrf
长天一色3 小时前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
NiNg_1_2343 小时前
npm、yarn、pnpm之间的区别
前端·npm·node.js
秋殇与星河3 小时前
CSS总结
前端·css
BigYe程普4 小时前
我开发了一个出海全栈SaaS工具,还写了一套全栈开发教程
开发语言·前端·chrome·chatgpt·reactjs·个人开发