Nginx反向代理

目录

一.简介

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

复制代码
#拷贝两个tomcat
 tar -zxvf apache-tomcat-9.0.78.tar.gz
 mv apache-tomcat-9.0.78 /usr/local/tomcat1

 tar -zxvf apache-tomcat-9.0.78.tar.gz
 mv 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 more
  contributor license agreements.  See the NOTICE file distributed with
  this 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 with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed 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 and
  limitations under the License.
 -->
<!--  Note:  A "Server" is not itself a "Container", so you may not
     define 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 resources
       Documentation at /docs/jndi-resources-howto.html
   -->
<GlobalNamingResources>
<!--  Editable user database that can also be used by
         UserDatabaseRealm 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 share
       a 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 received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define 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 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the 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/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration 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 processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on 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 passwords
           via a brute-force attack  -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!--  This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available 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 applications
             Documentation at: /docs/config/valve.html  -->
<!-- 
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
         -->
<!--  Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: 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/aaa
 mkdir -p /usr/local/tomcat2/webapps/bbb
 echo "<h1>This is 8080 Port</h1>" > /usr/local/tomcat1/webapps/aaa/a.html
 echo "<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测试

相关推荐
無限神樂4 分钟前
docker compose简介
运维·docker·容器
ELI_He9994 分钟前
gpg使用记录
linux·运维·服务器
zzh_my8 分钟前
centos yum 安装软件报错
linux·运维·centos
杰克崔17 分钟前
内核里的__private变量的使用及sparse
linux·运维·服务器·车载系统
每次学一点17 分钟前
如何将网吧电脑加入ZeroTier虚拟局域网
运维·服务器·网络
很㗊25 分钟前
Linux --- tar命令常见用法
linux·运维·服务器
2501_9413297225 分钟前
气压表智能读数检测:基于YOLOv8的指针与刻度识别实现自动化读数
运维·yolo·自动化
RisunJan28 分钟前
Linux命令-ld(将目标文件连接为可执行程序)
linux·运维·服务器
济61734 分钟前
linux--Cortex-A7架构-- Ubuntu20.04
linux·运维·架构
运维栈记37 分钟前
Claude Code 与 MCP 服务器使用指南
运维·ai