Tomcat多域名訪問

  對於域名解析相信很多小夥伴都瞭解過,就是我們在萬網購買一個域名,比如hpugs.com,然後呢?我們希望域名與我們的服務器綁定,然後通過域名直接訪問我們的項目,這就是本篇要和大家一起探討的問題。下面開始我們的工作:

  1、首先是域名,登錄萬維網官網,填寫我們想要購買的域名,然後就是查詢是否已被搶注,如果沒有被搶注,下面就是付錢購買了。

  2、有了域名,接下來就是我們的服務器了,大家可以根據自身的需求,進行選擇,比如像小筆一樣,是一枚窮逼,那怎麼來模擬這個過程呢?答案當然是有的,我們可以把自己的電腦當做一臺服務器。這樣的話,我們的域名也無需購買了,通過修改本地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

  看到這裏你是不是已經知道該怎麼做了。

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

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

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

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

  我們都知道get方式請求存在字符長度的限制,那麼post請求有麼有長度限制呢?相信寫過APP服務接口的小童鞋可以遇到過這樣的場景,當APP端通過Base64的方式進行照片上傳時,當照片大小超過2M後,我們的服務端接收不到數據包,這是什麼問題呢?答案當然不是post對於數據包有長度限制,這是因爲Tomcat的內部對於數據包的長度有默認長度限制,最大支持的長度是2M,這個也是可以解決的,通過在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、下面就是我們域名與項目綁定:

  還是上面的server.xml文件,我們找的Engine標籤,然後我們可以看到:

<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爲我們訪問路徑。

  6、如何進行多域名綁定

  很簡單如上,在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>

  7、最後需要說幾點:

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

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

 

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章