Tomcat實(shí)現(xiàn)多域名訪問詳解
對(duì)于域名解析相信很多小伙伴都了解過,就是我們?cè)谌f(wàn)網(wǎng)購(gòu)買一個(gè)域名,比如hpugs.com,然后呢?我們希望域名與我們的服務(wù)器綁定,然后通過域名直接訪問我們的項(xiàng)目,這就是本篇要和大家一起探討的問題。下面開始我們的工作:
1、首先是域名,登錄萬(wàn)維網(wǎng)官網(wǎng),填寫我們想要購(gòu)買的域名,然后就是查詢是否已被搶注,如果沒有被搶注,下面就是付錢購(gòu)買了。
2、有了域名,接下來(lái)就是我們的服務(wù)器了,大家可以根據(jù)自身的需求,進(jìn)行選擇,比如像小筆一樣,是一枚窮逼,那怎么來(lái)模擬這個(gè)過程呢?答案當(dāng)然是有的,我們可以把自己的電腦當(dāng)做一臺(tái)服務(wù)器。這樣的話,我們的域名也無(wú)需購(gòu)買了,通過修改本地hosts文件,自定義本地域名綁定。具體方法:打開C:\Windows\System32\drivers\etc找到hosts文件,用記事本打開,我們可以看到,localhost與我們的127.0.0.1是綁定的。
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost
看到這里你是不是已經(jīng)知道該怎么做了。
3、有了域名和服務(wù)器,下面就是我們的Tomcat配置了,我們知道Tomcat服務(wù)器默認(rèn)監(jiān)聽的是8080端口,而瀏覽器默認(rèn)的端口是80,下面就是修改Tomcat的8080端口。打開Tomcat解壓地址,找到config文件夾下的server.xml,找到
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="0" />
然后把8080端口修改為80保存,然后啟動(dòng)Tomcat,在瀏覽器輸入剛剛我們?cè)O(shè)置的域名點(diǎn)擊回車,進(jìn)入Tomcat的默認(rèn)頁(yè)面,表示我們的配置成功。
4、穿插一個(gè)Tomcat的小配置說(shuō)明:
我們都知道get方式請(qǐng)求存在字符長(zhǎng)度的限制,那么post請(qǐng)求有么有長(zhǎng)度限制呢?相信寫過APP服務(wù)接口的小童鞋可以遇到過這樣的場(chǎng)景,當(dāng)APP端通過Base64的方式進(jìn)行照片上傳時(shí),當(dāng)照片大小超過2M后,我們的服務(wù)端接收不到數(shù)據(jù)包,這是什么問題呢?答案當(dāng)然不是post對(duì)于數(shù)據(jù)包有長(zhǎng)度限制,這是因?yàn)門omcat的內(nèi)部對(duì)于數(shù)據(jù)包的長(zhǎng)度有默認(rèn)長(zhǎng)度限制,最大支持的長(zhǎng)度是2M,這個(gè)也是可以解決的,通過在server.xml下添加:maxPostSize="-1"即可。
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="2000"
redirectPort="8443"
URIEncoding="UTF-8"
maxThreads="5000"
compression="on"
compressableMimeType="text/html,text/xml"
maxPostSize="-1"/>
5、下面就是我們域名與項(xiàng)目綁定:
還是上面的server.xml文件,我們找的Engine標(biāo)簽,然后我們可以看到:
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<!--localhost-->
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
這就是我們的Tomcat默認(rèn)綁定,我們可以通過localhost直接訪問項(xiàng)目即是這個(gè)配置。下面我們配一個(gè)通過域名來(lái)訪問項(xiàng)目的配置,在Engine標(biāo)簽下我們?cè)谔砑右粋€(gè)Host配置:
<!--www.hpugs.com-->
<Host name="www.hpugs.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context docBase="C:\Program Files\apache-tomcat-8.5.13\webapps\pc-server" path="" reloadable="true" />
</Host>
注意:Context 標(biāo)簽必須放置于Value下,不然Tomcat啟動(dòng)將會(huì)報(bào)錯(cuò),這里解釋兩個(gè)參數(shù):docBase項(xiàng)目實(shí)際路徑;path項(xiàng)目訪問虛擬路徑。簡(jiǎn)單的說(shuō)docBase指向我們的項(xiàng)目具體位置,path為我們?cè)L問路徑。
6、如何進(jìn)行多域名綁定
很簡(jiǎn)單如上,在Engine標(biāo)簽下我們?cè)偬砑訋讉€(gè)Host配置即可
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<!--localhost-->
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<!--www.hpugs.com-->
<Host name="www.hpugs.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context docBase="C:\Program Files\apache-tomcat-8.5.13\webapps\pc-server" path="" reloadable="true" />
</Host>
<!--m.hpugs.com-->
<Host name="m.hpugs.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context docBase="C:\Program Files\apache-tomcat-8.5.13\webapps\web-mobile-server" path="" reloadable="true" />
</Host>
</Engine>
7、最后需要說(shuō)幾點(diǎn):
defaultHost是指默認(rèn)Host配置,當(dāng)訪問域名沒有進(jìn)行綁定時(shí),使用默認(rèn)Host配置
Engine 標(biāo)簽下默認(rèn)localhost配置,是為了沒有進(jìn)行域名項(xiàng)目綁定的域名,通過域名+項(xiàng)目名稱來(lái)訪問。
總結(jié)
以上就是本文關(guān)于Tomcat多域名訪問詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。期待您的寶貴意見。
相關(guān)文章
詳解用Tomcat服務(wù)器配置https雙向認(rèn)證過程實(shí)戰(zhàn)
本篇文章主要介紹了詳解用Tomcat服務(wù)器配置https雙向認(rèn)證過程實(shí)戰(zhàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Tomcat+Mysql高并發(fā)配置優(yōu)化講解
今天小編就為大家分享一篇關(guān)于Tomcat+Mysql高并發(fā)配置優(yōu)化講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Tomcat中實(shí)現(xiàn)Session小結(jié)
本篇文章主要介紹了Tomcat中實(shí)現(xiàn)Session小結(jié) ,Session的主要目的就是為了彌補(bǔ)Http的無(wú)狀態(tài)特性。簡(jiǎn)單的說(shuō),就是服務(wù)器可以利用session存儲(chǔ)客戶端在同一個(gè)會(huì)話期間的一些操作記錄。2017-01-01

