問題
最近用 Tomcat
搭建了個 Jenkins
,但是訪問的時候需要端口加 /jenkins/
才能進行訪問。我們是直接將 Jenkins.war 包放在 webapps
下的。 我們想直接通過不加路徑進行訪問。
解決辦法
思路一
在 Host
里面進行配置 path ,經過測試,發現是不行的。
<Host name="localhost" appBase="webapps" path="/jenkins"
unpackWARs="true" autoDeploy="true">
</Host>
思路二(可用)
新建一個 jenkins 目錄在 ./webapps/
下 ./webapps/jenkins
. 然后將 jenkins.war 解壓,然后將壓縮包里所有內容放在 ./webapps/jenkins/
下, 然后在 ./conf/server.xml
的 Host
增加一個 Context
配置. 假設路徑為 /opt/apache-tomcat-8.5.47/webapps/jenkins
,那么配置為
<Host name="localhost" appBase="webapps" path="/"
unpackWARs="true" autoDeploy="true">
<!-- 新增-->
<Context docBase="/opt/apache-tomcat-8.5.47/webapps/jenkins" path="/" reloadable="true"/>
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
然后啟動 Jenkins 即可。
思路三(可用)
將 jenkins.war 解壓,然后將壓縮包里所有內容放在 ./webapps/ROOT/
下,然后啟動 jenkins.
[root@localhost webapps]# pwd
/opt/apache-tomcat-8.5.47/webapps
[root@localhost webapps]# ls -l ./ROOT/
total 2428
drwxr-x--- 3 root root 36 Nov 10 18:22 bootstrap
-rw-r----- 1 root root 1946 Feb 7 2019 ColorFormatter.class
drwxr-x--- 5 root root 265 Nov 10 18:22 css
-rw-r----- 1 root root 1544 Oct 28 13:22 dc-license.txt
drwxr-x--- 2 root root 30 Nov 10 18:22 executable
-rw-r----- 1 root root 17542 Oct 28 13:22 favicon.ico
drwxr-x--- 12 root root 180 Nov 10 18:22 help
drwxr-x--- 6 root root 4096 Nov 10 18:22 images
-rw-r----- 1 root root 1674 Feb 7 2019 JNLPMain.class
drwxr-x--- 2 root root 250 Nov 10 18:22 jsbundles
-rw-r----- 1 root root 862 Feb 7 2019 LogFileOutputStream$1.class
-rw-r----- 1 root root 636 Feb 7 2019 LogFileOutputStream$2.class
-rw-r----- 1 root root 2240 Feb 7 2019 LogFileOutputStream.class
-rw-r----- 1 root root 20730 Feb 7 2019 Main.class
-rw-r----- 1 root root 1048 Feb 7 2019 MainDialog$1$1.class
-rw-r----- 1 root root 1067 Feb 7 2019 MainDialog$1.class
-rw-r----- 1 root root 2633 Feb 7 2019 MainDialog.class
-rw-r----- 1 root root 512 Feb 7 2019 Main$FileAndDescription.class
drwxr-x--- 3 root root 94 Nov 10 18:22 META-INF
-rw-r----- 1 root root 71 Oct 28 13:22 robots.txt
drwxr-x--- 3 root root 218 Nov 10 18:22 scripts
drwxr-x--- 7 root root 275 Nov 10 18:22 WEB-INF
-rw-r----- 1 root root 2390099 May 12 15:50 winstone.jar
反思
- 經過這個測試,我發現了一個點,當我們 在 Root 目錄有內容的時候,我們沒有配置 Context 的時候,我們是取 Root 里面的內容的,當我們配置了的 Context 的內容的時候,訪問 ip:端口(默認訪問),就是我們 Context 里面 docBase 配置的路徑。
- webapps 下的默認訪問是
ROOT
,其他在webapps
下的 目錄都是需要加路徑的(也就是目錄名),當我們指定了也就是 Context 里面 docBase 配置的路徑,這個時候才不需要加路徑。