无法远程访问tomcat服务器

时间:2020-07-03 22:53:32

标签: java tomcat google-cloud-platform

我正在Google Compute Engine上使用VM,我选择了一台g1小型计算机。

我安装了tomcat服务器。该服务器可以运行,但是我无法在Web浏览器上访问它。

我还安装了httpd,我可以在浏览器上访问端口80,但不能访问8080

这是我的开放端口:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1180/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1198/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1180/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1198/master         
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      11470/java          
tcp6       0      0 :::8080                 :::*                    LISTEN      11470/java          
tcp6       0      0 :::80                   :::*                    LISTEN      11319/httpd

这是我的tomcat.service文件:

[Unit]
Description=Tomcat 8.5 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="CATALINA_BASE=/opt/tomcat/apache-tomcat-8.5.56"
Environment="CATALINA_HOME=/opt/tomcat/apache-tomcat-8.5.56"
Environment="CATALINA_PID=/opt/tomcat/apache-tomcat-8.5.56/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/apache-tomcat-8.5.56/bin/startup.sh
ExecStop=/opt/tomcat/apache-tomcat-8.5.56/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

我已经使用命令将端口8080添加到防火墙

firewall-cmd --zone=public --permanent --add-port=8080/tcp
firewall-cmd --reload

但是当我列出防火墙设置时,我得到了

trusted (active)
 target: ACCEPT
 icmp-block-inversion: no
 interfaces: eth0
 sources: 
 services: http https
 ports: 
 protocols: 
 masquerade: no
 forward-ports: 
 source-ports: 
 icmp-blocks: 
 rich rules: 

1 个答案:

答案 0 :(得分:1)

要解决您的问题,您应该配置GCP防火墙以允许与Tomcat服务器的入口连接。

请查看文档Configuring Firewall Rules并通过控制台或命令行遵循说明Creating a firewall ingress rule

gcloud compute firewall-rules create tomcat-server-allow-ingress --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:8080 --source-ranges=0.0.0.0/0 --target-tags=tomcat-server

我建议您用IP替换0.0.0.0/0并使用network tags将规则直接应用于VM实例:

gcloud compute instances add-tags tomcat-server --zone=europe-west3-a --tags=tomcat-server