阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Tomcat 6 绑定域名和根域名

113次阅读
没有评论

共计 4061 个字符,预计需要花费 11 分钟才能阅读完成。

Tomcat 6 绑定域名和根域名

通常情况下,大家都会使用 Ngix + Tomcat 或者 Apache +  Tomcat 的模式来构建网站系统、绑定域名,这也是比较科学和实用的方式,这两种方式笔者会在后续补充上来。这次主要讲的是单独使用 Tomcat6 来部署 jsp 网站(请不要吐槽技术 low,因为我们网站目前没有大的访问量,且节省时间)。

ps:本文的前提条件是你的 www 域名和顶级域名都可以解析到你的服务器了,本文以域名 www.linuxidc.com 作为域名示例,虚构的域名。。。

步骤一:将项目发布到 Tomcat 服务器上,且指定端口为 80 端口

可以将项目 war 包放到服务器的任意位置解压例如解压到 /appuser/web/linuxidc(后续会使用到),然后打开 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.linuxidc.com,接着在 Host 下添加子标签

<Context path=”” docBase=”/appuser/web/linuxidc” allowLinking=”true”></Context>

最后的 Host 标签中的内容是这样的:

<Host name=”www.linuxidc.com”  appBase=”webapps”
            unpackWARs=”true” autoDeploy=”true”>
… 省略的默认配置
    <Alias>linuxidc.com</Alias><!– 绑定顶级域名时要配置的,提前放出来 –>
    <Context path=”” docBase=”/appuser/web/linuxidc” allowLinking=”true”></Context>
</Host>

其中 docBase 就是我们服务器上解压后的 war 包目录,这样配置完成后启动 Tomcat 没有错误的话,访问 www.linuxidc.com 域名就可以了,网站都可以正常显示。

但当你访问 linuxidc.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.linuxidc.com –>
    <rule>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <name>seo redirect</name>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <condition name=”host” operator=”notequal”>^www.linuxidc.com</condition>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <from>^/(.*)</from>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <to type=”permanent-redirect” last=”true”>http://www.linuxidc.com/$1</to>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </rule>
&nbsp;&nbsp;&nbsp; <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>linuxidc.com</Alias> 即可(在步骤二中已经添加好了),所有配置完成,重启 Tomcat 服务器,访问顶级域名 linuxidc.com 发现已经可以访问了,且被重定向到了 www.linuxidc.com,

打开 chrome 浏览器监听网络窗口可以发现访问 linuxidc.com 时服务器返回 301 重定向状态码到了 www.linuxidc.com。

更多 Tomcat 相关教程见以下内容

CentOS 6.6 下安装配置 Tomcat 环境  http://www.linuxidc.com/Linux/2015-08/122234.htm

RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目  http://www.linuxidc.com/Linux/2015-02/113528.htm 

Tomcat 权威指南(第二版)(中英高清 PDF 版 + 带书签)  http://www.linuxidc.com/Linux/2015-02/113062.htm 

Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm 

Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm 

CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm 

CentOS 6.5 下安装 Tomcat  http://www.linuxidc.com/Linux/2015-01/111415.htm 

Tomcat 的详细介绍:请点这里
Tomcat 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/134899.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4061字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中