【问题标题】:undertow webserver not binding to remote addressundertow 网络服务器未绑定到远程地址
【发布时间】:2016-10-28 07:37:31
【问题描述】:

我正在测试 undertow 2.0.0.Alpha1 网络服务器。当我在本地运行它时,它可以工作并在我转到localhost:80 时返回Hello World。然后我将网络服务器部署在远程服务器上并转到remote_ip:80,但我没有得到任何回复。如果我在远程服务器上运行curl -i -X GET http://localhost:80,那么我也会得到Hello World。所以服务器肯定正在运行,但由于某种原因,它只是无法通过远程 IP 地址访问。如果我尝试在代码中将主机名设置为远程 IP(即.addHttpListener(80, "remote.ip")),那么我会得到一个BindException

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorldServer {

    public static void main(final String[] args) {
        try {
            Runtime.getRuntime().exec("sudo fuser -k 80/tcp");
        } catch (IOException ex) {
            Logger.getLogger(HelloWorldServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        Undertow server = Undertow.builder()
                .addHttpListener(80, null)
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build();
        server.start();
    }

}

有什么线索吗?

【问题讨论】:

  • 线索 #1:使用“netstat -a”(或等效项)检查服务器正在侦听的 IP 和端口。
  • tcp6 0 0 127.0.0.1:80 :::* LISTEN 2939/java
  • 那么您是否使用该 IP 地址在 IPv6 上使用 curl? (它是“本地主机”...)
  • 不,我认为 localhost 在 Ubuntu 上默认只是链接到 127.0.0.1

标签: java jboss wildfly undertow nio2


【解决方案1】:

addHttpListener(80, null) 的第二个参数是主机。您需要在其中放置主机名或 IP 以使其侦听公共 IP。使用null 只会绑定到本地主机。

如果您想绑定到所有地址,请尝试绑定到公共 IP 或绑定到 0.0.0.0

Undertow server = Undertow.builder()
        .addHttpListener(80, "0.0.0.0")
        .setHandler(new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                exchange.getResponseSender().send("Hello World");
            }
        }).build();
server.start();

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多