Jetty如何设置首页为自定义页面
😦 问题描述
嵌套Jetty的Web应用启动后,正常的访问路径为:localhost:8080/geoserver,访问正常。
但访问根目录:localhost:8080,则缺省页面不显示,显示错误信息如下, 希望访问根目录时自动跳转到正常路径(localhost:8080/geoserver)。
这个倒不是什么大的问题,但在生产环境部署后,也会有安全隐患,会暴露应用程序的路径等信息,信息安全扫描时,也会列出这个问题。
Error 404 - Not Found.
No context on this server matched or handled this request.
Contexts known to this server are:
/geoserver ---> o.e.j.w.WebAppContext@2d6e8792{/geoserver,file:/E:/Downloads/geoserver-2.14.1-bin/geoserver-2.14.1/webapps/geoserver/,AVAILABLE}{E:\Downloads\geoserver-2.14.1-bin\geoserver-2.14.1\webapps\geoserver}
/root1 ---> o.e.j.w.WebAppContext@76a86677{/root1,file:/E:/Downloads/geoserver-2.14.1-bin/geoserver-2.14.1/webapps/root1/,AVAILABLE}{E:\Downloads\geoserver-2.14.1-bin\geoserver-2.14.1\webapps\root1}
Powered by Jetty:// Java Web Server
😃 解决办法
-
登录到应用服务器
我这边是Linux服务器,采用Xshell登录。 -
在应用部署目录下新建root文件夹
cd /home/geoserver-2.14.1/webapps
mkdir root -
生成重定向文件
以下示例为将首页重定向到"/geoserver/“目录为例:
cp geoserver/index.html ./root
vim index.html
修改"web/“为”/geoserver/”,保存退出
根据情况,也有可能需要重启Jetty应用服务,我这边示例命令如下:
systemctl restart geoserver
本示例重定向文件示例:
index.html
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
// javascript based redirect courtesy of ahocevar
window.location.replace("/geoserver/");
</script>
<title>GeoServer: Redirecting</title>
</head>
<body>
<p><b>Redirecting to the actual home page.</b></p>
<p>If you're stuck here, it means you don't have javascript
enabled. Javascript is required to actually use the GeoServer admin console.</p>
</body>
</html>
😛 原因分析
Jetty容器启动后,根路径会映射到相应的目录“webapps/root”,所以访问:localhost:8080会到webapps/root目录下搜索index.html, index.htm, index.jsp等文件(首页文件在“etc/webdefault.xml”文件中有定义),解决方法就是新建webapps/root目录,并在下面放置index.html文件,该文件中通过JavaScript设置跳转,跳转到相应的目录(本示例是跳转到:/geoserver/目录),也就是访问根目录就直接跳转到应用程序。