一尘不染

如何在端口80上运行Spring Boot应用程序

spring

我无法在端口80上启动应用程序。

我已经在本地计算机(使用我的IDE和本地服务器)上尝试过,没有运气。

我检查了其他类似的帖子,并确保我在具有root用户的服务器上运行jar。

这是错误:

 till here all ok
...
java.net.SocketException: Permission denied
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:338)
at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:760)
at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:472)
at org.apache.catalina.connector.Connector.startInternal(Connector.java:986)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:237)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:186)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:149)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.andirod.StartApplication.main(StartApplication.java:20)
...
...
...
Exception in thread "main" java.lang.IllegalStateException: Tomcat connector in failed state
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:157)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.andirod.StartApplication.main(StartApplication.java:20)

阅读 607

收藏
2020-04-19

共1个答案

一尘不染

在Linux上,只有root可以打开低于1024的端口,因此默认情况下限制端口80

如果要在80端口上发布应用,则需要将请求从80端口重定向到要运行springapp(例如8080)端口的端口

解决方案1:HTTP代理服务器
你可以使用默认情况下允许在端口80上运行的Apache2服务器,并且可以将对你的请求转发到Tomcat

Debian的示例配置

sudo apt-get install apache2

a2enmod proxy
a2enmod proxy_http   

cd /etc/apache2/sites-enabled
sudo nano 000-default.conf

编辑文件:

<VIRTUALHOST *:80>

    ProxyPreserveHost On

    # ...

    ProxyPass / http://localhost:8080/
</VIRTUALHOST>

保存文件:Ctrl+ OENTER,Ctrl+X

注意:要了解有关虚拟主机配置的更多信息,你可以通过单击此处查看有关该主题的详细Apache手册。

重新启动Apache2以应用更改:

sudo service apache2 restart

要么

sudo systemctl restart apache2

解决方案2:端口转发
使用iptables进行重定向

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

如果你需要使用localhost也可以添加

iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080
2020-04-19