如何在端口 80 上运行 Spring Boot 应用程序 [英] How can I run a Spring Boot application on port 80

查看:64
本文介绍了如何在端口 80 上运行 Spring Boot 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't start an application on port 80.

我已经在我的本地计算机上尝试过(使用我的 IDE,在本地服务器上),但没有成功.

I have tried on my local computer (using my IDE, and on a local server), no luck.

我已经检查了其他类似的帖子,并确保我使用 root 在服务器上运行 jar.

I have checked other similar posts and make sure that I run jar on server with root.

这是错误:

 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)

推荐答案

linux上1024以下的端口只能root才能打开,所以默认限制80端口

On linux ports below 1024 can be opened only by root, so the port 80 is restricted by default

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

if you want to publish your app on 80 port you need to redirect request from port 80 to the port you gonna run your springapp (e.g 8080) port

您可以使用默认允许工作在80端口的Apache2服务器,并且可以将您的请求转发给Tomcat

You can use Apache2 server which is allowed by default to work on port 80 and can forward requests for you to Tomcat

Debian 的示例配置

Example configuration for 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+OENTERCtrl+X

注意:要了解有关虚拟主机配置的更多信息,您可以通过单击 这里.

Note: To learn more about virtual host configurations, you can check out the detailed Apache manual on the subject by clicking here.

重新启动 Apache2 以应用更改:

Restart Apache2 to apply changes:

sudo service apache2 restart

sudo systemctl restart apache2

方案二:端口转发

使用 iptables 进行重定向

Solution 2: Port forwarding

Use iptables for redirects

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

如果你需要使用localhost也添加这个

if you need to use localhost also add this

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

这篇关于如何在端口 80 上运行 Spring Boot 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆