Tomcat 6 绑定域名和根域名

来源:互联网 发布:移动宽带网络设置 编辑:程序博客网 时间:2024/04/30 06:29

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

1 0
原创粉丝点击