質問

It's easier to show than to tell. This is from the Apache Tika web service:

http://pastebin.com/jrCsVVtt

On line 89 of that file, localhost is hard-coded:

sf.setProviders(providers);
sf.setAddress("http://localhost:" + TikaServerCli.DEFAULT_PORT + "/");
BindingFactoryManager manager = sf.getBus().getExtension(
                                BindingFactoryManager.class);

This means that if you're running the web service on your local machine, you cannot access it via http://hostname:9998/tika or http://hostname.domain.net:9998/tika. It must be accessed as http://localhost:9998/tika.

My Java is extremely rusty, but after some Googling, I added a few lines:

sf.setProviders(providers);
String hostname;
try
{
  InetAddress ia = InetAddress.getLocalHost();
  hostname = ia.getCanonicalHostName() + ":";
}
catch (Exception e)
{
  //I'll do something else with this later
  hostname = "http://localhost:";
}
sf.setAddress(hostname + TikaServerCli.DEFAULT_PORT + "/");
BindingFactoryManager manager = sf.getBus().getExtension(
                                BindingFactoryManager.class);

This allows me to access it by hostname and by FQDN, but NOT via localhost.

Is there an idiomatic way to get a web service to respond in all of its possible forms?

  • 127.0.0.1 (when accessed locally)
  • localhost (when accessed locally)
  • hostname
  • FQDN
  • IP address
  • Whatever else I'm missing

I guess I could compute and more-or-less complete enumeration at runtime, but it seems like there's probably a better way(?).

役に立ちましたか?

解決

I submitted a patch which added an optional commandline parameter, and changed the default behavior to listen to all valid hostnames and IPs. (Whether this new default behavior stays remains to be seen.)

More details, as well as the patch, can be found on the Jira ticket:

https://issues.apache.org/jira/browse/TIKA-1196

他のヒント

Try nginx as frontend server, which proxies the requests to a Tika (Jetty) server.

  1. Install nginx on the same server as a Tika server running.

  2. Edit nginx conf file:

    vim /etc/nginx/conf.d/default.conf
    
  3. Setup:

    # The address or IP on which your Tika server is running. I choose port 9998.
    upstream your_domain_or_ip {
        server localhost:9998;
    }
    
    # The nginx server instance
    server {
       listen 80;
       server_name localhost;
    
       # Pass the request with corrent headers to the Tika server
       location / {
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_set_header X-NginX-Proxy true;
         proxy_pass http://your_domain_or_ip/;
         proxy_redirect off;
       }
    }
    

A system has several network interfaces - both hardware (like your ethernet card) and software (like your loopback interface with special ip 127.0.0.1). When using localhost your server binds your port only to the loopback interface and hence you can access it only from machine on that interface and loopback interface has only one machine on its network and that is your machine itself.

Now, you can choose which all interfaces you want to bind your port to. Your server will be accessible from all those machines in the network which the bound interfaces connect to. For eg. if you choose to bind your server port to the loopback interface and the ethernet interface then your server will be be accessible only from the machines on your LAN and the localhost alone. If you also have a WIFI interface and your server port is not bound to it then machines on the wifi will not be able to access your server.

Now, if you want to bind your server port to all the available interface so that it is accessible from everywhere then you need to specify a special IP - 0.0.0.0 instead of localhost or 127.0.0.1. Refer this to understand the network interfaces - http://wilddiary.com/list-ip-addresses-in-java/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top