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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33703965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:56:14  来源:igfitidea点击:

How can I run a Spring Boot application on port 80

javaspringspring-boot

提问by 5er

I can't start an application on port 80.

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

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

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

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

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

This is the error:

这是错误:

 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)

采纳答案by Oskar Dajnowicz

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

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

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 端口上发布您的应用程序,您需要将请求从端口 80 重定向到您要运行 springapp(例如 8080)端口的端口

Solution 1: HTTP Proxy server

方案一:HTTP代理服务器

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

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

Example configuration for Debian

Debian 的示例配置

sudo apt-get install apache2

a2enmod proxy
a2enmod proxy_http   

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

Edit file:

编辑文件:

<VIRTUALHOST *:80>

    ProxyPreserveHost On

    # ...

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

Save file: Ctrl+O, ENTER, Ctrl+X

保存文件:Ctrl+ O, ENTER, Ctrl+X

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

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

Restart Apache2 to apply changes:

重新启动 Apache2 以应用更改:

sudo service apache2 restart

or

或者

sudo systemctl restart apache2

Solution 2: Port forwarding

方案二:端口转发

Use iptables for redirects

使用 iptables 进行重定向

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

if you need to use localhost also add this

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

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

回答by sunysen

Add -Djava.net.preferIPv4Stack=trueto the VM options

添加-Djava.net.preferIPv4Stack=true到 VM 选项

JavaMail API to iMail -- java.net.SocketException: Permission denied: connect

JavaMail API 到 iMail -- java.net.SocketException: 权限被拒绝:连接

回答by PhoenixPan

Use sudoon linux.

sudo在 linux 上使用。

I was running a Spring Boot application on Ubuntu and java -jar app.jar --server.port=80gave me the same error. Since ports below 1024 can only be opened by root access, so use"sudo": sudo java -jar app.jar --server.port=80.

我在 Ubuntu 上运行 Spring Boot 应用程序并java -jar app.jar --server.port=80给了我同样的错误。由于低于1024的端口只能由root访问权限打开,所以用“命令”: sudo java -jar app.jar --server.port=80

This way of deployment is only suggested for local tests due to security concerns. See comments for details.

出于安全考虑,这种部署方式仅建议用于本地测试。详情见评论。

回答by VK321

Here are steps I followed on centos.

这是我在centos上遵循的步骤。

Step 1 (Optional):Set port

步骤 1(可选):设置端口

By default spring boot app run on port 8080, if you wish to change this you can change on your src/main/resources/application.properties file

默认情况下,spring boot 应用程序在端口 8080 上运行,如果您想更改此设置,您可以在 src/main/resources/application.properties 文件中进行更改

server.port = 8082 // any port above than 1024

Step 2:Install apache if not installed already

第 2 步:如果尚未安装,请安装 apache

On Centos 7

在 Centos 7

sudo yum install httpd

Step 3:Edit your virtual host

第 3 步:编辑您的虚拟主机

/etc/httpd/conf.d/vhost.conf

Your config should look like this

你的配置应该是这样的

<VirtualHost *:80>
   ServerName yourdomin.com
   #DocumentRoot /var/www/html

   ProxyPreserveHost On
   ProxyPass / http://localhost:8082/
   ProxyPassReverse / http://localhost:8082/
</VirtualHost>

And restart apache

并重启apache

sudo service httpd restart

回答by aydinugur

In the case of using macOs, it is now possible to run on port 80 without any changes on macOs Mojave Version 10.14.

在使用macOs的情况下,现在可以在端口 80 上运行而无需在macOs Mojave 版本 10.14上进行任何更改。