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测试

相关推荐
qq109160698130 分钟前
Nginx SSL+tomcat,使用request.getScheme() 取到https协议
nginx·tomcat·ssl
陈煜的博客36 分钟前
paddleOcr -Linux 下的 Conda 安装
linux·运维·conda
乐维_lwops1 小时前
网络安全:数字时代的守护盾
运维·网络
上理考研周导师1 小时前
【智能化仪表设计】化工仪表及自动化全套课件
运维·自动化
挥剑决浮云 -1 小时前
Linux 网络编程
linux·运维·c语言·网络·笔记
北 染 星 辰1 小时前
Linux---常用shell脚本
linux·运维·服务器
潇湘秦2 小时前
AI助力智能运维!在Linux主机上实现和chatgpt对话
运维·ai
小笨猪-3 小时前
RabbitMQ运维
java·运维·redis·分布式·rabbitmq·java-rabbitmq
XY.散人3 小时前
初识Linux · 信号保存
运维·服务器
TsengOnce3 小时前
Docker安装稳定版本nginx-1.26.2
linux·nginx·docker