[英]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. 我发现了几个问题。
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: 通常这可以通过以下步骤实现:
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.