Tomcat多域名訪問

  對於域名解析相信不少小夥伴都瞭解過,就是咱們在萬網購買一個域名,好比hpugs.com,而後呢?咱們但願域名與咱們的服務器綁定,而後經過域名直接訪問咱們的項目,這就是本篇要和你們一塊兒探討的問題。下面開始咱們的工做:html

  一、首先是域名,登陸萬維網官網,填寫咱們想要購買的域名,而後就是查詢是否已被搶注,若是沒有被搶注,下面就是付錢購買了。web

  二、有了域名,接下來就是咱們的服務器了,你們能夠根據自身的需求,進行選擇,好比像小筆同樣,是一枚窮逼,那怎麼來模擬這個過程呢?答案固然是有的,咱們能夠把本身的電腦當作一臺服務器。這樣的話,咱們的域名也無需購買了,經過修改本地hosts文件,自定義本地域名綁定。具體方法:打開C:\Windows\System32\drivers\etc找到hosts文件,用記事本打開,咱們能夠看到,localhost與咱們的127.0.0.1是綁定的。apache

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost

  看到這裏你是否是已經知道該怎麼作了。瀏覽器

  三、有了域名和服務器,下面就是咱們的Tomcat配置了,咱們知道Tomcat服務器默認監聽的是8080端口,而瀏覽器默認的端口是80,下面就是修改Tomcat的8080端口。打開Tomcat解壓地址,找到config文件夾下的server.xml,找到tomcat

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxPostSize="0" />

  而後把8080端口修改成80保存,而後啓動Tomcat,在瀏覽器輸入剛剛咱們設置的域名點擊回車,進入Tomcat的默認頁面,表示咱們的配置成功。服務器

  四、穿插一個Tomcat的小配置說明:app

  咱們都知道get方式請求存在字符長度的限制,那麼post請求有麼有長度限制呢?相信寫過APP服務接口的小童鞋能夠遇到過這樣的場景,當APP端經過Base64的方式進行照片上傳時,當照片大小超過2M後,咱們的服務端接收不到數據包,這是什麼問題呢?答案固然不是post對於數據包有長度限制,這是由於Tomcat的內部對於數據包的長度有默認長度限制,最大支持的長度是2M,這個也是能夠解決的,經過在server.xml下添加:maxPostSize="-1"便可。webapp

<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"/>

  五、下面就是咱們域名與項目綁定:post

  仍是上面的server.xml文件,咱們找的Engine標籤,而後咱們能夠看到:this

<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 &quot;%r&quot; %s %b" />
        </Host>
</Engine>

  這就是咱們的Tomcat默認綁定,咱們能夠經過localhost直接訪問項目便是這個配置。下面咱們配一個經過域名來訪問項目的配置,在Engine標籤下咱們在添加一個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 &quot;%r&quot; %s %b" />  
            <Context docBase="C:\Program Files\apache-tomcat-8.5.13\webapps\pc-server" path="" reloadable="true" />
        </Host>

  注意:Context 標籤必須放置於Value下,否則Tomcat啓動將會報錯,這裏解釋兩個參數:docBase項目實際路徑;path項目訪問虛擬路徑。簡單的說docBase指向咱們的項目具體位置,path爲咱們訪問路徑。

  六、如何進行多域名綁定

  很簡單如上,在Engine標籤下咱們再添加幾個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 &quot;%r&quot; %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 &quot;%r&quot; %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 &quot;%r&quot; %s %b" />  
            <Context docBase="C:\Program Files\apache-tomcat-8.5.13\webapps\web-mobile-server" path="" reloadable="true" />
        </Host>
    </Engine>

  七、最後須要說幾點:

  defaultHost是指默認Host配置,當訪問域名沒有進行綁定時,使用默認Host配置

  Engine 標籤下默認localhost配置,是爲了沒有進行域名項目綁定的域名,經過域名+項目名稱來訪問。

相關文章
相關標籤/搜索