av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

java-web - Tomcat+Nginx實(shí)現(xiàn)動(dòng)靜分離的功能,動(dòng)態(tài)請求為什么沒有發(fā)到Tomcat這里?(已解決)

瀏覽:111日期:2023-08-18 17:44:21

問題描述

問題

RT因?yàn)樽罱雽W(xué)習(xí)一下Nginx服務(wù)器,就想搭建一個(gè)Tomcat+Nginx,簡單的實(shí)現(xiàn)動(dòng)靜分離的功能。

具體代碼和配置版本

Ubuntu:ubuntu-16.04-desktop-amd64

JDK:jdk1.8.0_101

Tomcat:apache-tomcat-8.5.4

Nginx:nginx/1.10.0

ubuntu ip:192.168.182.129

Tomcat配置(/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 配置(/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_headerHost $host;proxy_set_headerX-Real-IP $remote_addr;proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://tomcat;try_files $uri $uri/ =404; }}測試

通過Tomcat,請求url:http://test.nt.springmvc.com:...

通過Nginx,請求url:http://test.nt.springmvc.com/

通過Nginx,請求url:http://test.nt.springmvc.com/...

通過Nginx,請求url:http://test.nt.springmvc.com/...

tomcat日志

192.168.182.129 - - [28/Jul/2016:15:11:36 +0800] 'GET /springmvc/test/ HTTP/1.1' 200 106192.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 990Nginx日志

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'

通過日志可以看到,我第四次的請求并沒有轉(zhuǎn)發(fā)到Tomcat這里,找了網(wǎng)上好多文檔,都沒有發(fā)現(xiàn)是哪里出現(xiàn)的問題。下面是對應(yīng)的網(wǎng)頁java-web - Tomcat+Nginx實(shí)現(xiàn)動(dòng)靜分離的功能,動(dòng)態(tài)請求為什么沒有發(fā)到Tomcat這里?(已解決)java-web - Tomcat+Nginx實(shí)現(xiàn)動(dòng)靜分離的功能,動(dòng)態(tài)請求為什么沒有發(fā)到Tomcat這里?(已解決)java-web - Tomcat+Nginx實(shí)現(xiàn)動(dòng)靜分離的功能,動(dòng)態(tài)請求為什么沒有發(fā)到Tomcat這里?(已解決)java-web - Tomcat+Nginx實(shí)現(xiàn)動(dòng)靜分離的功能,動(dòng)態(tài)請求為什么沒有發(fā)到Tomcat這里?(已解決)

解決方法Nginx 配置(/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_headerHost $host;proxy_set_headerX-Real-IP $remote_addr;proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://tomcat;#try_files $uri $uri/ =404; }}

可以看到在nginx的配置文件中,將try_files $uri $uri/ =404注解掉就可以正常訪問了

try_files

nginx上try_files 判斷文件是否存在

$uri

指的是請求的文件和路徑,不包括“?”或者“#”之后的東西$request_uri: /stat.php?id=1585378&web_id=1585378$uri /stat.php$document_uri: /stat.php

問題解答

回答1:

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_headerHost $host;proxy_set_headerX-Real-IP $remote_addr;proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://tomcat;try_files $uri $uri/ =404; }}

標(biāo)簽: java
主站蜘蛛池模板: 久久久一区二区 | 国产精品精品视频一区二区三区 | 久久黄视频 | 亚洲精品一区av在线播放 | 久久久国产一区二区三区四区小说 | 91一区二区 | 56pao在线| www精品 | 日日爽| 精品久| 91久久精品视频 | 久久久久久久久久久高潮一区二区 | 欧美福利 | 日本不卡一区 | 天天艹| 天堂一区二区三区四区 | 偷拍自拍第一页 | 国产精品久久777777 | 亚洲自拍一区在线观看 | 久久亚洲免费 | 国产剧情一区 | 爱爱视频日本 | 国产一区二区在线免费播放 | 久久综合一区二区 | 涩涩导航| 午夜免费观看网站 | 精品三级在线观看 | 免费视频一区二区 | 日韩精品一区二区三区在线播放 | 一区二区三区亚洲视频 | 色噜噜狠狠色综合中国 | 国产精品二区三区 | 国产一区二区在线视频 | 国产电影一区二区在线观看 | 国产乱码精品一区二区三区中文 | 亚洲国产视频一区二区 | 成人在线观看免费 | 久久久精品一区二区 | 国产精品久久久久久久久久久久 | 亚洲自拍偷拍av | 久久亚洲一区二区三区四区 |