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配置,是为了没有进行域名项目绑定的域名,经过域名+项目名称来访问。

相关文章
相关标签/搜索