java-web - Tomcat+Nginx implementiert die Funktion der dynamischen und statischen Trennung. Warum werden dynamische Anfragen nicht an Tomcat gesendet? (gelöst)
大家讲道理
大家讲道理 2017-05-16 17:15:39
0
1
758

Frage

RT
Da ich kürzlich den Nginx-Server erlernen möchte, möchte ich einen Tomcat + Nginx erstellen, um einfach die Funktion der dynamischen und statischen Trennung zu realisieren.

Detaillierter Code und Konfiguration

Version

  1. Ubuntu:ubuntu-16.04-desktop-amd64

  2. JDK: jdk1.8.0_101

  3. Tomcat: Apache-Tomcat-8.5.4

  4. Nginx:nginx/1.10.0

  5. ubuntu ip:192.168.182.129

Tomcat-Konfiguration (/conf/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="8005" 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>
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="test.nt.springmvc.com"  appBase="webapps/springmvc"
            unpackWARs="true" autoDeploy="true">

        <Context path="/springmvc"  docBase="springmvc.war"/>    
        
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

Nginx-Konfiguration (/nginx/sites-available/default)

upstream tomcat {   
 server 192.168.182.129:8080;   
} 


server {
    listen 80 default_server;
    listen [::]:80 ;

    root /opt/apache-tomcat-8.5.4/webapps/springmvc;

    #server_name _;
    server_name test.nt.springmvc.com;

    location /{
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://tomcat;
        try_files $uri $uri/ =404;

    }
}

Test

  1. Über Tomcat URL anfordern: http://test.nt.springmvc.com:...

  2. Über Nginx die URL anfordern: http://test.nt.springmvc.com/

  3. Über Nginx die URL anfordern: http://test.nt.springmvc.com/...

  4. Über Nginx die URL anfordern: http://test.nt.springmvc.com/...

Tomcat-Protokoll

192.168.182.129 - - [28/Jul/2016:15:11:36 +0800] "GET /springmvc/test/ HTTP/1.1" 200 106
192.168.182.129 - - [28/Jul/2016:15:12:40 +0800] "GET / HTTP/1.0" 404 -
192.168.182.129 - - [28/Jul/2016:15:13:05 +0800] "GET /springmvc HTTP/1.0" 302 -
192.168.182.129 - - [28/Jul/2016:15:13:05 +0800] "GET /springmvc/ HTTP/1.0" 404 990

Nginx-Protokoll

192.168.182.129 - - [28/Jul/2016:15:12:40 +0800] "GET / HTTP/1.1" 404 0 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.182.129 - - [28/Jul/2016:15:13:05 +0800] "GET /springmvc HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.182.129 - - [28/Jul/2016:15:13:05 +0800] "GET /springmvc/ HTTP/1.1" 404 441 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.182.129 - - [28/Jul/2016:15:13:37 +0800] "GET /springmvc/test HTTP/1.1" 404 152 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"

Wie Sie den Protokollen entnehmen können, wurde meine vierte Anfrage nicht an Tomcat weitergeleitet. Ich habe viele Dokumente im Internet durchsucht und konnte nicht finden, wo das Problem aufgetreten ist. Unten finden Sie die entsprechende Webseite
Lösung Nginx-Konfiguration (/nginx/sites-available/default)

upstream tomcat {   
 server 192.168.182.129:8080;   
} 

server {
    listen 80 default_server;
    listen [::]:80 ;

    root /opt/apache-tomcat-8.5.4/webapps/springmvc;

    server_name test.nt.springmvc.com;

    location / {
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://tomcat;
        #try_files $uri $uri/ =404;
    }
}
Sie können das in der Nginx-Konfigurationsdatei sehen. Kommentieren Sie try_files $uri $uri/ =404 aus und Sie können normal darauf zugreifen try_files
try_files auf Nginx stellt fest, ob die Datei existiert $uri
Bezieht sich auf die angeforderte Datei und den Pfad, ausgenommen Dinge nach „?“ oder „#“$request_uri: /stat.php?id=1585378&web_id=1585378$uri /stat.php

$document_uri: /stat. php
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

Antworte allen(1)
Ty80
upstream tomcat {   
 server 192.168.182.129:8080;   
} 


server {
    listen 80 default_server;
    listen [::]:80 ;

    root /opt/apache-tomcat-8.5.4/webapps/springmvc;  #**(这行拿掉)**

    #server_name _;
    server_name test.nt.springmvc.com;

    location /{  # location / { 斜杠后面加空格
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://tomcat;
        try_files $uri $uri/ =404;

    }
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage