目录
- 一.简介
- 1.反向代理
- 二.案例
- 1.案例1
- 2.案例2
一.简介
1.反向代理
1.1反向代理: 是指代理服务器来接收Internet上的客户端请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给客户端。此时代理服务器对外就表现为一个反向代理服务器。
反向代理客户端不知道服务器的信息,隐藏了服务器的信息
1.2正向代理:是一个位于客户端和原始服务器之间的服务器,为了从原始服务器获得内容。客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转发并获得的内容返回给客户端
正向代理服务器不知道客户端的信息,隐藏了客户端的信息
二.案例
1.案例1
1.1实现思路
1.2实现步骤
1.2.1Nginx配置
#配置文件中修改
vim /usr/local/nginx/conf/nginx.conf
server {listen 80;server_name 10.10.100.222;#charset koi8-r;#access_log logs/host.access.log main;location / {proxy_pass http://127.0.0.1:8080;root html;index index.html index.htm;}#检测语法是否正确
[root@localhost sbin]# /usr/local/nginx/sbin/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#重新加载nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
1.2.2搭建tomcat
tomcat包和jdk包
链接:https://pan.baidu.com/s/12n0LNdXiXIJh-GNQwp2WNw
提取码:7ptr
–来自百度网盘超级会员V1的分享
链接:https://pan.baidu.com/s/1yuBmzZ76QShBfBnrrgDLfA
提取码:hk96
–来自百度网盘超级会员V1的分享
安装jdk环境
#先创建java文件目录,如果已存在就不用创建
mkdir -p /usr/local/java#解压到java文件目录
tar -vzxf jdk-8u161-linux-x64.tar.gz -C /usr/local/java/#配置环境变量
vi /etc/profile
export JAVA_HOME=/usr/local/java/jdk1.8.0_11
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin#生效环境变量
source /etc/profile
下载Tomcat、解压Tomcat、安装Tomcat、启动Tomcat
#解压tomcat
tar -zxvf apache-tomcat-9.0.78.tar.gz#安装tomcat
mv apache-tomcat-9.0.78 /usr/local/tomcat#启动tomcat
/usr/local/tomcat/bin/startup.sh
1.3测试
可以看到tomcat的界面,实际上是访问ngnix跳转到tomcat的8080端口
2.案例2
2.1实现思路
2.2实现步骤
#修改配置文件
server {listen 80;server_name 10.10.100.222;#charset koi8-r;#access_log logs/host.access.log main;location ~/aaa/ {proxy_pass http://127.0.0.1:8080;}location ~/bbb/ {proxy_pass http://127.0.0.1:8081;}
}#重新加载nginx
/usr/local/nginx/sbin/nginx -s reload
2.2.1拷贝两个Tomcat,将其中一个的端口信息修改为8081
#拷贝两个tomcattar -zxvf apache-tomcat-9.0.78.tar.gzmv apache-tomcat-9.0.78 /usr/local/tomcat1tar -zxvf apache-tomcat-9.0.78.tar.gzmv apache-tomcat-9.0.78 /usr/local/tomcat2#删除tomcat2下面的配置
rm -f /usr/local/tomcat2/conf/server.xml
#添加新配置文件
vi /usr/local/tomcat2/conf/server.xml
<!-- 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="8006" 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 8080-->
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxParameterCount="1000"/>
<!-- A "Connector" using the shared thread pool -->
<!-- <Connector executor="tomcatThreadPool"port="8081" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443"maxParameterCount="1000"/>-->
<!-- 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"maxParameterCount="1000"><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"maxParameterCount="1000"><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 8009 -->
<!-- <Connector protocol="AJP/1.3"address="::1"port="8010"redirectPort="8443"maxParameterCount="1000"/>-->
<!-- 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 "%r" %s %b"/>
</Host>
</Engine>
</Service>
</Server>
#创建文件夹和文件mkdir -p /usr/local/tomcat1/webapps/aaamkdir -p /usr/local/tomcat2/webapps/bbbecho "<h1>This is 8080 Port</h1>" > /usr/local/tomcat1/webapps/aaa/a.htmlecho "<h1>This is 8081 Port</h1>" > /usr/local/tomcat2/webapps/bbb/b.html
#启动tomcat
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat2/bin/startup.sh
2.3测试