nginx学习(2)

Nginx 反向代理案例2

1. 实现效果
实现效果:使用 nginx 反向代理,根据访问的路径跳转到不同端口的服务中
nginx 监听端口为 8001,
访问 http://127.0.0.1:8001/edu/ 直接跳转到 127.0.0.1:8081
访问 http://127.0.0.1:8001/vod/ 直接跳转到 127.0.0.1:8082

a. 第一步,两个tomcat端口和测试页面
准备两个 tomcat,一个 8081 端口,一个 8082 端口。
在**/tools/下 新建tomcat8081和tomcat8082两个文件夹,将 Tomcat安装包 分别上传到两个文件夹,进行解压缩安装,8081的Tomcat只改一个http协议默认端口号** 就行,直接启动即可。
这里需要改8082的端口号,需要修改三个端口,只修改一个端口号的话,是启动不了的,我已经测试过了(如果只修改http协议默认端口的话,8081和8082只会启动一个)。因为默认的都是8080(没有的直接创建文件夹,好多都是刚建的,与上面的第一个示例示例有点改动)

在这里插入图片描述
1、tomcat8081 解压包,
2、使用命令 编辑 文件 :/conf/server.xml 文件
vi server.xml
3、修改后如下:
3.1、修改server 的默认端口,由默认8005->8091
在这里插入图片描述
3.2、修改http协议的默认端口,由默认的8080->8081
在这里插入图片描述
修改默认ajp协议的默认端口,由默认的8009->9001
在这里插入图片描述

tomcat8082(注意:其实和上面修改的地方一样)
使用命令 编辑 文件 :/conf/server.xml 文件
vim server.xml
修改后的完整文件如下:(自己可以核对)

<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements.  See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may notdefine subcomponents such as "Valves" at this level.Documentation at /docs/config/server.html-->
<Server port="8092" shutdown="SHUTDOWN"><Listener className="org.apache.catalina.startup.VersionLoggerListener" /><!-- Security listener. Documentation at /docs/config/listeners.html<Listener className="org.apache.catalina.security.SecurityListener" />--><!-- APR library loader. Documentation at /docs/apr.html --><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /><!-- Prevent memory leaks due to use of particular java/javax APIs--><Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /><Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /><!-- Global JNDI resourcesDocumentation at /docs/jndi-resources-howto.html--><GlobalNamingResources><!-- Editable user database that can also be used byUserDatabaseRealm to authenticate users--><Resource name="UserDatabase" auth="Container"type="org.apache.catalina.UserDatabase"description="User database that can be updated and saved"factory="org.apache.catalina.users.MemoryUserDatabaseFactory"pathname="conf/tomcat-users.xml" /></GlobalNamingResources><!-- A "Service" is a collection of one or more "Connectors" that sharea single "Container" Note:  A "Service" is not itself a "Container",so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html--><Service name="Catalina"><!--The connectors can use a shared executor, you can define one or more named thread pools--><!--<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"maxThreads="150" minSpareThreads="4"/>--><!-- A "Connector" represents an endpoint by which requests are receivedand responses are returned. Documentation at :Java HTTP Connector: /docs/config/http.htmlJava AJP  Connector: /docs/config/ajp.htmlAPR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL/TLS HTTP/1.1 Connector on port 8082--><Connector port="8082" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" /><!-- A "Connector" using the shared thread pool--><!--<Connector executor="tomcatThreadPool"port="8082" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />--><!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443This connector uses the NIO implementation. The defaultSSLImplementation will depend on the presence of the APR/nativelibrary and the useOpenSSL attribute of the AprLifecycleListener.Either JSSE or OpenSSL style configuration may be used regardless ofthe SSLImplementation selected. JSSE style configuration is used below.--><!--<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"maxThreads="150" SSLEnabled="true"><SSLHostConfig><Certificate certificateKeystoreFile="conf/localhost-rsa.jks"type="RSA" /></SSLHostConfig></Connector>--><!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2This connector uses the APR/native implementation which always usesOpenSSL for TLS.Either JSSE or OpenSSL style configuration may be used. OpenSSL styleconfiguration is used below.--><!--<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"maxThreads="150" SSLEnabled="true" ><UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" /><SSLHostConfig><Certificate certificateKeyFile="conf/localhost-rsa-key.pem"certificateFile="conf/localhost-rsa-cert.pem"certificateChainFile="conf/localhost-rsa-chain.pem"type="RSA" /></SSLHostConfig></Connector>--><!-- Define an AJP 1.3 Connector on port 9002 --><!--<Connector protocol="AJP/1.3"address="::1"port="9002"redirectPort="8443" />--><!-- An Engine represents the entry point (within Catalina) that processesevery request.  The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon to the appropriate Host (virtual host).Documentation at /docs/config/engine.html --><!-- You should set jvmRoute to support load-balancing via AJP ie :<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">--><Engine name="Catalina" defaultHost="localhost"><!--For clustering, please take a look at documentation at:/docs/cluster-howto.html  (simple how to)/docs/config/cluster.html (reference documentation) --><!--<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>--><!-- Use the LockOutRealm to prevent attempts to guess user passwordsvia a brute-force attack --><Realm className="org.apache.catalina.realm.LockOutRealm"><!-- This Realm uses the UserDatabase configured in the global JNDIresources under the key "UserDatabase".  Any editsthat are performed against this UserDatabase are immediatelyavailable for use by the Realm.  --><Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/></Realm><Host name="localhost"  appBase="webapps"unpackWARs="true" autoDeploy="true"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /></Host></Engine></Service>
</Server>

并准备好测试的页面
写一个a.html页面,
tomcat8081的tomcat,放到目录 /webapp/vod 下,内容:

<h1>nginx-8081!!!</h1>

tomcat8082的tomcat,放到目录 /webapp/edu 下,内容:

<h1>nginx-8082!!!</h1>

测试页面
在这里插入图片描述

在这里插入图片描述
b. 第二步,修改 nginx 的配置文件
修改 nginx 的配置文件 在 http 块中添加 server{}
修改其中注释的就行。

 server {listen       8001;        #server_name  localhost;server_name  172.31.0.99; #charset koi8-r;#access_log  logs/host.access.log  main;location ~  /vod/ {proxy_pass http://172.31.0.99:8081;}location ~  /edu/ {proxy_pass http://172.31.0.99:8082;}}

在这里插入图片描述

开发的端口: nginx监听端口:8001,tomcat8081端口:8081,tomcat8082端口:8082。
测试结果:

在这里插入图片描述
在这里插入图片描述

location 指令说明
该指令用于匹配 URL。
语法如下:1= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配 成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 uri 包含正则表达式,并且区分大小写。
3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字 符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~*标识。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/188991.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第六十二周周报

学习目标&#xff1a; 一、实验 二、论文 学习时间&#xff1a; 2023.11.11-2023.11.17 学习产出&#xff1a; 实验 1、CB模块实验效果出来了&#xff0c;加上去效果不太行&#xff0c;后续实验考虑是否将CB模块换到其他地方 2、CiFAR100实验已完成&#xff0c;效果比Vi…

场景交互与场景漫游-osgGA库(5)

osgGA库 osgGA库是OSG的一个附加的工具库&#xff0c;它为用户提供各种事件处理及操作处理。通过osgGA库读者可以像控制Windows窗口一样来处理各种事件 osgGA的事件处理器主要由两大部分组成&#xff0c;即事件适配器和动作适配器。osgGA:GUIEventHandler类主要提供了窗口系统的…

基于SSM的智能仓储系统研究与设计

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

C++--STL总结

参考教程&#xff1a;黑马程序员匠心之作|C教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili 软件界一直希望建立一种可重复利用的东西&#xff0c;C的面向对象和泛型编程思想&#xff0c;目的就是复用性的提升。 大多情况下&#xff0c;数据结构和算法都未能有一套标准,…

python趣味编程-5分钟实现一个F1 赛车公路游戏(含源码、步骤讲解)

Python 中的 F1 赛车公路游戏及其源代码 F1 Race Road Game是用Python编程语言开发的,它是一个桌面应用程序。 这款 Python 语言的 F1 赛道游戏可以免费下载开源代码,它是为想要学习 Python 的初学者创建的。 该项目系统使用了 Pygame 和 Random 函数。 Pygame 是一组跨平…

Flutter笔记: 在Flutter应用中使用SQLite数据库

Flutter笔记 在Flutter应用中使用SQLite数据库&#xff08;基于sqflite&#xff09; 作者&#xff1a;李俊才 &#xff08;jcLee95&#xff09;&#xff1a;https://blog.csdn.net/qq_28550263 邮箱 &#xff1a;291148484163.com 本文地址&#xff1a;https://blog.csdn.net/q…

大模型之十二十-中英双语开源大语言模型选型

从ChatGPT火爆出圈到现在纷纷开源的大语言模型&#xff0c;众多出入门的学习者以及跃跃欲试的公司不得不面临的是开源大语言模型的选型问题。 基于开源商业许可的开源大语言模型可以极大的节省成本和加速业务迭代。 当前&#xff08;2023年11月17日)开源的大语言模型如下&#…

牛客——OR36 链表的回文结构(C语言,配图,快慢指针)

本题是没有对C的支持的&#xff0c;但因为Cpp支持C&#xff0c;所以这里就用C写了&#xff0c;可以面向更多用户 链表的回文结构_牛客题霸_牛客网 (nowcoder.com) 思路一&#xff1a;链表翻转 简单的想想整形我们怎么比较&#xff0c;就是将整形A 依次取尾&#xff0c;放到整形…

gorm的简单操作

1. 什么是orm ORM全称是&#xff1a;Object Relational Mapping(对象关系映射)&#xff0c;其主要作用是在编程中&#xff0c;把面向对象的概念跟数据库中表的概念对应起来。举例来说就是&#xff0c;我定义一个对象&#xff0c;那就对应着一张表&#xff0c;这个对象的实例&a…

基于SSM的设备配件管理和设备检修系统

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 目录…

Redis数据的持久化

Redis的持久化有两种方式&#xff1a; RDB&#xff08;Redis Database&#xff09;和AOF&#xff08;Append Only File&#xff09; 目录 一、RDB 保存方式 2、rdb在redis.conf文件中的配置 二、AOF 1、保存方式 2、aof方式持久化在redis.conf文件中的配置 三、持久化建…

spring cloud openfeign 使用注意点

近期在做项目时给自己挖了一个坑&#xff0c;问题重现如下 使用的组件版本如下 spring boot 2.7.15&#xff0c;对应的 spring cloud 版本为 2021.0.5&#xff0c;其中 spring cloud 适配的 openfeign 版本是 3.1.5。 项目中使用的 feign 接口如下 public interface QueryApi…