0
点赞
收藏
分享

微信扫一扫

Tomcat配置--如何能够让外网通过公网IP或域名访问到服务器上面的项目

一世独秀 2022-04-13 阅读 57
java后端

今天突然想到自己还有一台服务器可以使用,于是就准备把之前做的一个网站弄上去玩玩。结果发现不知道是因为太久没用还是什么原因,出问题了。死活连不上,就重装了一下。于是就出现了这个问题,Tomcat忘记怎么配置了。因为公司大部分项目都用的php,java没怎么动过。折腾了一上午,终于弄好了;特意记录一下免得又忘了。

需要修改配置的文件

所有版本的文件应该都在这个目录下(除了版本号不同)

第一处修改:

	<!-- ... -->
	<Service name="Catalina">
	<!-- ... -->
	<!-- 将这里的8080改为80 -->
    <Connector port="8080" protocol="HTTP/1.1"
     	connectionTimeout="20000"
     	redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
   <!-- ... -->

注意注意注意

   <!-- Define an AJP 1.3 Connector on port 8009 -->
   <!-- 就是这里,不需要改-->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->
  • 普及一下

第二处修改

 	<!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <!-- 将这里的defaultHost的值改为服务器的公网IP,或域名 -->
    <Engine name="Catalina" defaultHost="localhost">
     <!--For clustering, please take a look at documentation at:
     /docs/cluster-howto.html  (simple how to)
     /docs/config/cluster.html (reference documentation) -->

第三处修改

	<!-- 将这里的localhost改为公网ip或域名 -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
            <!-- 添加Context元素,并将docBase设置为项目地址 -->
			<Context path="" docBase="C:Program Filesapache-tomcat-7.0.92webappsProject"/>
        <!-- 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>

这样就大功告成了,重新启动一下Tomcat就可以通过公网IP或域名访问了。

启动时如控制台出现乱码,修改logging.properties后重启即可解决

文件路径:apache-tomcat-xxxconflogging.properties

	###将UTF-8改为GBK即可
	###java.util.logging.ConsoleHandler.encoding = UTF-8
	java.util.logging.ConsoleHandler.encoding = GBK
举报

相关推荐

0 条评论