简体   繁体   English

如何在Tomcat中设置上下文路径,以便可以在不附加部署的文件夹名称的情况下进入站点?

[英]How to set context path in Tomcat so one could enter the site without appending the deployed folder name?

I read about this on Tomcat guide here and some SO questions. 我在这里阅读有关Tomcat指南的内容和一些SO问题。 And I think I'm pretty much doing the same thing. 而且我认为我几乎都在做同样的事情。 But in some way cannot manage to succeed. 但在某种程度上无法成功。

First of all I have to say that my application is deployed on a shared Tomcat server that I have no control over. 首先,我必须说我的应用程序部署在我无法控制的共享Tomcat服务器上。 I just drop my .war file and it gets deployed. 我只是删除我的.war文件并进行部署。

I tried to package my application as ROOT.war but didn't work. 我试图将我的应用程序打包为ROOT.war但是没有用。 The admin told me to package it as whatever the name I want and they would take care of it. 管理员告诉我把它打包成我想要的名字,他们会照顾它。 I packaged it as my-application.war and it got deployed but I have to enter http://my-host/my-application to get to the website. 我把它打包为my-application.war并且已经部署但我必须输入http:// my-host / my-application才能进入网站。

After contacting the admin they told me that they have put a context elemnt in my host in Tomcat config file like: 联系管理员后,他们告诉我他们已经在我的主机中将一个上下文元素放在Tomcat配置文件中,如:

<Context path="" docBase="path of my-application deployed folder"/>

which was supposed to set my-application as default application for all the requests coming to my-host . 应该将my-application设置为进入my-host所有请求的默认应用程序。 But it didn't and whenever I enter http://my-host I get: 但它没有,每当我输入http:// my-host我得到:

HTTP Status 404 - / The requested resource (/) is not available

But again when I enter http://my-host/my-application it all works fine. 但是,当我输入http:// my-host / my-application时 ,一切正常。 Any suggestion on what might be wrong is definitely appreciated. 任何关于可能出错的建议都是绝对值得赞赏的。

Updates: 更新:
I tried to follow the steps described in tomcat documentation on how to make the application default . 我尝试按照tomcat文档中描述的如何使应用程序默认的步骤 3 ways are described and I tried all three ways and could successfully deploy my application as ROOT on localhost. 描述了3种方式,我尝试了所有三种方式,并且可以在localhost上将我的应用程序成功部署为ROOT。

I also tried to reproduce the problem I'm facing on remote server so I could find the reason and report it to admin. 我还尝试重现我在远程服务器上遇到的问题,以便找到原因并将其报告给管理员。 I find couple of problems. 我发现了几个问题。

  1. In server.xml fragment that admin sent me autoDeploy and deployOnStartUp are set to true, whereas they should be false if explicitly defining Context element in server.xml. admin.xml发送给我的server.xml片段中, autoDeploydeployOnStartUp设置为true,而如果在server.xml中显式定义Context元素,则它们应该为false。 This will cause double deployment which creates a ROOT folder and a folder with the name of the .war file. 这将导致双重部署,从而创建一个ROOT文件夹和一个名为.war文件的文件夹。 Deleting the .war will delete it's corresponding folder and undeploys the application but ROOT remains and must be deleted manually and requires a Tomcat restart. 删除.war将删除它的相应文件夹并取消部署应用程序,但ROOT仍然存在,必须手动删除并需要重新启动Tomcat。 Until it's restarted any deployment of ROOT.war will fail. 在重新启动之前,ROOT.war的任何部署都将失败。
  2. I figured there are some reasons preventing ROOT.war from deploying. 我想有一些原因阻止了ROOT.war的部署。 One could be that a ROOT.xml exists in conf/{engine-name}/{host-name} or a ROOT folder exists in appBase of the host or as I described above a ROOT application from previous deployment is not undeployed and requires Tomcat restart. 一个可能是一个在的ROOT.xml CONF / {发动机名}存在/ {主机名}或在主机的存在的appBase根文件夹或如我上面从以前的部署一个ROOT应用描述的内容不是未展开并且需要的Tomcat重新开始。

Either way I couldn't exactly pinpoint what exactly is preventing ROOT.war from deploying since that requires the access to Tomcat log files and conf files to check the cases I described above. 无论哪种方式,我无法确切地指出究竟是什么阻止ROOT.war部署,因为这需要访问Tomcat日志文件和conf文件来检查我上面描述的情况。

Also from all I see my the admin seems incapable of maintaining a Tomcat server and finding the problem. 另外,从我看来,我的管理员似乎无法维护Tomcat服务器并找到问题。 So I decided to go with a dedicated Tomcat server after struggling with the shared one. 所以我决定在与共享服务器挣扎之后使用专用的Tomcat服务器。

In your question, you state that the admin set the context as: 在您的问题中,您声明管理员将上下文设置为:

<Context path="" docBase="path of my-application deployed folder"/>

Based on the comments above, I would suggest trying to use the relative path of your application rather than the absolute path. 根据上面的评论,我建议尝试使用应用程序的相对路径而不是绝对路径。

I tried this on my tomcat server with: 我在我的tomcat服务器上试过这个:

<Context path="/" docBase="my-application/" />

and that did the trick. 这就是诀窍。

The Host element which contains the Context element does actually set some parameters that might also impact the context. 包含Context元素的Host元素实际上设置了一些可能也会影响上下文的参数。 If it's the default settings, then a relative context should simply point to the webapps folder. 如果它是默认设置,那么相对上下文应该只指向webapps文件夹。 If it's been changed, the results may vary. 如果它已被更改,结果可能会有所不同。

Usually this can be achieved by the following steps: 通常这可以通过以下步骤实现:

  • Define a ROOT.xml context file in conf/Catalina/localhost 在conf / Catalina / localhost中定义ROOT.xml上下文文件
  • Name your webapp WAR “ROOT.war” or containing folder “ROOT” 将您的webapp命名为WAR“ROOT.war”或包含文件夹“ROOT”

However, i doubt you will be able to do that on a shared Tomcat instance. 但是,我怀疑你能在共享的Tomcat实例上做到这一点。 Only one application can run as the default application. 只有一个应用程序可以作为默认应用程序运行 The hosting company will probably not allow it as otherwise which application will they allow to be the default out of the many people sharing the same Tomcat instance? 托管公司可能不会允许它,否则它们允许哪个应用程序成为共享同一个Tomcat实例的许多人的默认设置?

See this link : http://staraphd.blogspot.com/2009/10/change-default-root-folder-in-tomcat.html 请访问此链接: http//staraphd.blogspot.com/2009/10/change-default-root-folder-in-tomcat.html

The Tomcat Wiki has a section on putting applications into default context. Tomcat的Wiki有一个部分上把应用程序转换为默认上下文。 However, in order to do this it implies some control over the Tomcat server that may not be possible in the shared context you describe. 但是,为了做到这一点,它意味着对Tomcat服务器的一些控制,这在您描述的共享上下文中可能是不可能的。

If you have the ability to install other systems on the server, one alternate solution would be to use a proxy server like NGINX. 如果您能够在服务器上安装其他系统,则另一种解决方案是使用NGINX等代理服务器。 This is way more complicated than simply naming your war file ROOT.war, but sometimes it's the only option. 这比简单命名war文件ROOT.war更复杂,但有时它是唯一的选择。

If you have NGINX listening on the server and you have your own url, you use the HttpProxyModule with setting such as: 如果您在服务器上监听NGINX并且拥有自己的URL,则可以使用HttpProxyModule ,其设置如下:

server {
        listen          80;
        server_name     my.domain.com;
        location / {
            proxy_pass              http://my-host/my-application;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
        }
    }

Also in order to make this work, you'd have to own the "my.domain.com" url and it would need to be separate from the one everyone is using for the shared Tomcat server. 此外,为了使这项工作,你必须拥有“my.domain.com”网址它需要与每个人用于共享Tomcat服务器的网站分开。

The nginx portion of the solution is free, but if you need to register a new url and then use something like no-ip.com to redirect it to the tomcat server it would cost money. 解决方案的nginx部分是免费的,但是如果你需要注册一个新网址,然后使用像no-ip.com这样的东西将它重定向到tomcat服务器,它将花费金钱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Tomcat 8中设置上下文路径 - How to set context path in Tomcat 8 如何为Web应用程序设置tomcat上下文路径? .war文件名正在url中的应用程序名称前面 - How to set up tomcat context path for web application? .war file name is coming infront of the application name in the url 在容器tomcat服务器上更改已部署(Spring Boot)战争的上下文路径。 所以它不是demo-0.01-SNAPSHOT - Changing the context path of deployed (Spring Boot) war on a container tomcat server. So it is not demo-0.01-SNAPSHOT tomcat部署的Web应用程序的上下文路径 - Context path for tomcat deployed web application "设置上下文路径与 Tomcat 中的 WAR 版本名称无关" - Set context path irrespective for WAR version name in Tomcat Tomcat:未设置上下文路径 - Tomcat : Context path not being set 设置Tomcat默认上下文路径 - Set Tomcat Default Context Path 使用Maven时如何在Tomcat 7.0中设置root(“/”)的上下文路径 - How to set context path to root(“/”) in Tomcat 7.0 when using Maven 如何配置<context>如果应用程序文件夹名称与上下文名称不同,在多实例 Tomcat 中?</context> - How to configure <Context> in a multi instance Tomcat if the application folder name is different from the context name? 如何更改tomcat的上下文路径? - how to change context path of tomcat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM