nginx学习(2)

Nginx 反向代理案例2

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

a. 首先,我们需要准备两个Tomcat实例,分别运行在8081和8082端口上。在/tools/目录下,创建两个文件夹,分别是tomcat8081和tomcat8082,并将Tomcat的安装包上传到这两个文件夹中。然后分别解压缩这两个安装包。

对于运行在8081端口的Tomcat,我们只需要修改其HTTP协议的默认端口号即可。而运行在8082端口的Tomcat,我们需要修改三个端口号。如果只修改HTTP协议的默认端口号,那么8081和8082只会启动一个,我已经测试过了。

默认情况下,Tomcat的运行端口是8080。如果这个端口已经被其他程序占用,我们需要创建一个新的文件夹,并在其中修改Tomcat的配置文件。同样的,如果8081和8082端口已经被占用,也需要进行相应的修改。

nginx学习(2)_第1张图片

tomcat8081

1、tomcat8081 解压包,
2、使用命令 编辑 文件 :/conf/server.xml 文件
vi server.xml
3、修改后如下:
3.1、修改server 的默认端口,由默认8005->8091
nginx学习(2)_第2张图片
3.2、修改http协议的默认端口,由默认的8080->8081
nginx学习(2)_第3张图片
3.3修改默认ajp协议的默认端口,由默认的8009->9001
nginx学习(2)_第4张图片

tomcat8082

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

<?xml version="1.0" encoding="UTF-8"?>
<!--
  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="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 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 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 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">
        <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" >
        <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 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>

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

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

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

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

测试页面
nginx学习(2)_第5张图片

nginx学习(2)_第6张图片
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学习(2)_第7张图片

修改完nginx.conf需要使修改文件生效,故需要

重启nginx服务或者重新加载nginx

进入到nginx目录下 /usr/local/nginx/sbin,执行重新加载nginx
命令:`./nginx -s reload`


开发的端口: nginx监听端口:8001,tomcat8081端口:8081,tomcat8082端口:8082

测试结果:

http://172.31.0.99:8001/vod/a.html
nginx学习(2)_第8张图片
http://172.31.0.99:8001/edu/a.html
nginx学习(2)_第9张图片

location 指令说明
该指令用于匹配 URL。
语法如下:

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

注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~*标识。

你可能感兴趣的:(nginx,nginx,学习,firefox)