目錄
- 一.簡介
- 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測試