Unable to access host port via http from guest

1,300 views
Skip to first unread message

Martin Nally

unread,
Sep 5, 2013, 6:44:27 PM9/5/13
to vagra...@googlegroups.com
I am trying to use vagrant to test an application configuration. My run-time configuration has 3 machines - one runs a reverse proxy (nginx), the second runs a python application and the third runs a database (MongoDB). My host machine runs windows 7, the guest machines run ubuntu 12.04. The particular test configuration I'm trying to get to work has MongoDB running on the host, the reverse proxy running on a virtualbox vm, and the application running on the host. What I am seeing is that from the virtualbox vm running the reverse proxy I can ping the host (ping 192.168.56.1), and I can access the mongoDB database on the host at 192.168.56.1:27017 (I have a program on the same machine as the nginx reverse proxy that does this), but I cannot access the python application running on the host  at 192.168.56.1:3000 via http. Nginx cannot forward messages to that address, and if I ssh to the box, I cannot curl that address either - the connection times out. On the host, I can successfully access 127.0.0.1:3000, so I know the python app is running properly. I did not (initially) set up a private network with vagrant - vagrant seems to have established 192.168.56.1 without me asking. I discovered it using ipconfig on the host. I tried setting up a private network explicitly (192.168.33.10), and then using 192.168.33.1 to address the host, but this did not change the behavior I saw, so I removed it.

Even more confusingly, if I run the python application in a second virtualbox, instead of on the host, everything works. On the second virtual box, I have a port forward statement that forwards host port 3000 to guest port 80. Curl on the first virtualbox can successfully address 192.168.56.1:3000 via http, and Nginx on the first box successfully forwards messages to the python application on second box.

I would be very grateful for any ideas on why things work the way they do, and what I can do get get it to work the way I want. Even suggestions of how to debug better would help.

Martin Nally

unread,
Sep 6, 2013, 12:48:48 PM9/6/13
to vagra...@googlegroups.com
I found a work-around for my problem. I added the following line to my windows hosts file and rebooted the machine
192.168.56.1    localhost
T
his causes everything to work as I had hoped, but the usual 127:0.0.1 ip address for localhost no longer works (I wonder when that will come back to bite me)

Although this works, I have no idea whether it's even a reasonable solution, far less the best one, so if anyone has any other ideas, I'd still be grateful.

Andrew Melo

unread,
Sep 6, 2013, 12:55:13 PM9/6/13
to vagra...@googlegroups.com
Is your python application binding to 0.0.0.0 or 127.0.0.1 .. The port forwarding wont won't work if your application can't be seen on the external address. Your hosts workaround does that behind the scenes, but there'll be collateral damage

Hth
Andrew
--
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
--
Andrew Melo

Martin Nally

unread,
Sep 6, 2013, 5:25:48 PM9/6/13
to vagra...@googlegroups.com

Thanks for the reply, Andrew. I may not fully understand your questions, but I'll give it a shot:


>> Is your python application binding to 0.0.0.0 or 127.0.0.1 ..  

The python app is run by executing this python on the host machine:

import os, sys, logging
from wsgiref.simple_server import make_server
from wsgiref.simple_server import WSGIServer
from SocketServer import ThreadingMixIn

class ThreadedWSGIServer(ThreadingMixIn, WSGIServer):
    """Handle requests in a separate thread."""

os.environ['APP_NAME'] = 'LifecycleConcepts'
os.environ['MONGODB_DB_HOST'] = 'localhost' 
os.environ['MONGODB_DB_PORT'] = '27017'
 
from logic_server import application

SERVER_NAME = 'LifecycleConcepts'
httpd = make_server('localhost', 3006, application, server_class=ThreadedWSGIServer)
httpd.serve_forever() 

I'm guessing the answer to your question may lie in the line:  httpd = make_server('localhost', PORT, application, server_class=ThreadedWSGIServer)

>> The port forwarding wont won't work if your application can't be seen on the external address.

I'm not clear on the role of port forwarding in my scenario. The documentation is clear on the use of port forwarding to allow messages sent to a host port to be forwarded to a guest port. My scenario is the inverse - I'm trying to forward from a guest port to a host port where the python app is running.

Thanks again for your interest.

Martin Nally

unread,
Sep 6, 2013, 5:30:37 PM9/6/13
to vagra...@googlegroups.com
I stated that last part poorly. I should have said "My scenario is the inverse - from within a guest vm, I'm trying to access a host port where the python app is running."

Martin Nally

unread,
Sep 9, 2013, 11:31:03 AM9/9/13
to vagra...@googlegroups.com
Inspired by Andrew's comment, I put my hosts file back the way it had been, and changed the way my python application is started from this:
 
httpd = make_server('localhost', 3006, application, server_class=ThreadedWSGIServer)
httpd.serve_forever() 

to this:

httpd = make_server('0.0.0.0', 3006, application, server_class=ThreadedWSGIServer)
httpd.serve_forever() 
 
Happily, this worked as I had hoped. [I also tried '127.0.0.1', but that did not work.] I assume that to someone who has better networking understanding than I have it would be  perfectly obvious why these different cases do or don't work, but I have to admit it's a mystery to me. If anyone can provide any pointers to some reading that would help explain this sort of thing, I would be grateful. Thanks to Andrew for the hint that got me unstuck.
Reply all
Reply to author
Forward
0 new messages