Tomcat 6 綁定域名和根域名

Tomcat 6 綁定域名和根域名

通常情況下,大家都會使用 Ngix + Tomcat 或者Apache +  Tomcat的模式來構建網站系統、綁定域名,這也是比較科學和實用的方式,這兩種方式筆者會在後續補充上來。這次主要講的是單獨使用Tomcat6來部署jsp網站(請不要吐槽技術low,因爲我們網站目前沒有大的訪問量,且節省時間)。

ps:本文的前提條件是你的www域名和頂級域名都可以解析到你的服務器了,本文以域名www.yddsj.com作爲域名示例,虛構的域名。。。

步驟一:將項目發佈到Tomcat服務器上,且指定端口爲80端口

可以將項目war包放到服務器的任意位置解壓例如解壓到/appuser/web/yddsj(後續會使用到),然後打開Tomcat目錄下的conf目錄中的server.xml文件,找到<Connnector port="8080"...>的配置項,改變端口爲80端口,例:

<Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />
ps:其中URIEncoding是用來設置tomcat服務器請求路徑的編碼設置,防止中文亂碼

步驟二:指定Tomcat對應的域名

接着編輯server.xml,找到<Host..>的配置項,指定Host中的name屬性爲你的域名例如www.yddsj.com,接着在Host下添加子標籤

<Context path="" docBase="/appuser/web/yddsj" allowLinking="true"></Context>

最後的Host標籤中的內容是這樣的:

<Host name="www.yddsj.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
...省略的默認配置
    <Alias>yddsj.com</Alias><!-- 綁定頂級域名時要配置的,提前放出來 -->
    <Context path="" docBase="/appuser/web/yddsj" allowLinking="true"></Context>
</Host>

其中docBase就是我們服務器上解壓後的war包目錄,這樣配置完成後啓動Tomcat沒有錯誤的話,訪問www.yddsj.com域名就可以了,網站都可以正常顯示。

但當你訪問yddsj.com的時候會發現網站無法解析,也就是你的根域名還沒有被解析。

步驟三:綁定Tomcat的頂級域名,這裏需要使用到tomcat的urlrewrite組件,是用來做tomcat的僞靜態組件

從SEO(搜索引擎優化)的角度來講根域名通常是需要重定向(http狀態碼301)到www域名的,這樣更利於搜索引擎的收錄和提升網站權重。因此將頂級域名重定向到www域名是非常必要的。

那麼這裏我們使用urlrewrite組件來做這個事情。首先從urlrewrite官網下載urlrewritefilter-4.0.3.jar包,版本隨意....,加入到項目中,然後在WEB-INF下添加urlrewrite組件對應的配置文件urlrewrite.xml,內容如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<!--

    Configuration file for UrlRewriteFilter
    http://www.tuckey.org/urlrewrite/

-->
<urlrewrite>
    <!-- 關鍵配置,域名爲www.yddsj.com -->
    <rule>
        <name>seo redirect</name>
        <condition name="host" operator="notequal">^www.yddsj.com</condition>
        <from>^/(.*)</from>
        <to type="permanent-redirect" last="true">http://www.yddsj.com/$1</to>
       </rule>
    <rule>
    <rule>
        <note>
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
        </note>
        <from>/test/status/</from>
        <to type="redirect">%{context-path}/rewrite-status</to>
    </rule>
     
    <outbound-rule>
        <note>
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
            in your pages.
        </note>
        <from>/rewrite-status</from>
        <to>/test/status/</to>
    </outbound-rule> 

</urlrewrite>

關鍵的地方是名字爲seo redirect的rule規則。接着需要在WEB-INF\web.xml個添加urlrewrite的過濾器,作用是所有請求通過urlrewrite過濾器進行過濾,添加配置如下:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

最後在Tomcat的server.xml配置文件中的Host便籤下再添加一個子標籤<Alias>yddsj.com</Alias>即可(在步驟二中已經添加好了),所有配置完成,重啓Tomcat服務器,訪問頂級域名yddsj.com發現已經可以訪問了,且被重定向到了www.yddsj.com,

打開chrome瀏覽器監聽網絡窗口可以發現訪問yddsj.com時服務器返回301重定向狀態碼到了www.yddsj.com。



參考文獻:

Urlrewrite

urlrewrite 301 permanent redirect with Tomcat HOWTO

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