使webserver不绑定到远程地址

时间:2016-06-26 01:33:22

标签: java jboss wildfly undertow nio2

我正在测试下载2.0.0.Alpha1网络服务器。当我在本地运行它时它会起作用并在我转到Hello World时返回localhost:80。然后我在远程服务器上部署Web服务器并转到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 个答案:

答案 0 :(得分:1)

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

如果要绑定到所有地址,请尝试绑定到公共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();