通过域名访问部署在服务器上的javaweb项目

因为对域名访问什么也不了解,遇到问题就有种不知道从哪里下手的茫然,也就更不知道错在哪里,前前后后一共折腾了一天多,最后问了阿里客服才成功弄出来,因此记录一下。
一、端口映射
服务器设备,如果申请域名之后,端口默认指向的为80端口,这里服务是发布在tomcat中,使用的是8080端口,因此需要端口映射,将80端口映射到8080端口。
例: http://127.0.0.1 实际等价于:http://127.0.0.1:80 用的是80端口,因为只有80端口可以省略
又例:http://www.sohu.com/ 实际等价于:www.sohu.com:80

以下是Windows系统中的端口映射方法(Linux系统中可以使用iptables)
端口映射可以使用nginx(需要在服务器安装nginx),也可以使用netsh。

这里用的netsh,netsh是win7以上系统自带的,支持IPv4和IPv6。直接cmd,在命令行配置

//添加端口映射
netsh interface portproxy add v4tov4 listenaddress=121.21.36.190 listenport=81 connectaddress=192.168.99.10 connectport=81
//将服务器IP地址为121.21.36.190的端口81映射到服务器IP地址为192.168.99.10的端口8080.现在访问121.21.36.190:81,实际是跟访问192.168.99.10:8080效果一样

//查看已配置的端口映射清单
netsh interface portproxy show v4tov4

//删除端口映射
netsh interface portproxy delete v4tov4 listenaddress=121.21.36.190 listenport=81

这里遇到的一个问题,端口映射用的ip地址是服务器内网ip,而不是服务器公网ip。配置如图

后来发现好像不用端口映射也可以,直接把Tomcat的端口改成80就好了

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

直接把port="8080"改为port="80"就可以了。

二、配置Tomcat

打开tomcat/conf/server.conf文件
1. 修改为域名。这里要改两个地方,如图


2. 配置docBase。在<Host></Host>里面添加<Context>。path="" docBase=项目在服务器上的绝对路径,如图

配置好的server.conf如下:

    
    <Engine name="Catalina" defaultHost="***.top">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <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>

      <Host name="***.top"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      
      <Context docBase="C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\SpringDemo" path="" reloadable="true"/>

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <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>

参考:
windows下端口映射 
Tomcat配置域名 

猜你喜欢

转载自www.cnblogs.com/zeroingToOne/p/9013278.html