我在 Vagrant 的实例中运行以下命令:printf 'HTTP/1.1 302 Moved\r\nLocation: https://www.eff.org/' | nc -l 2345在我的主机上,我想访问<ip of my vagrant server>:2345并重定向到https://www.eff.org/
重定向不会发生,浏览器只会继续加载。
我的Vagrantfile:

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/trusty64"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "public_network"

end
如何设置Vagrantfile并确定vagrant Linux服务器的IP地址,以便在使用端口2345的主机上的浏览器中使用?
我试过了:
  • curl ifconfig.me,得到:46.128.200.193
  • hostname -i,得到:2a02:2455:25f:e000:a00:27ff:febd:cd6c%4 10.0.2.15 192.168.33.10 192.168.0.16
  • ifconfig,得到:
  • eth0      Link encap:Ethernet  HWaddr 08:00:27:5f:bb:e6
              inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fe5f:bbe6/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:815 errors:0 dropped:0 overruns:0 frame:0
              TX packets:590 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:83632 (83.6 KB)  TX bytes:80621 (80.6 KB)
    
    eth1      Link encap:Ethernet  HWaddr 08:00:27:c0:4e:f3
              inet addr:192.168.33.10  Bcast:192.168.33.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fec0:4ef3/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:7 errors:0 dropped:0 overruns:0 frame:0
              TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:819 (819.0 B)  TX bytes:2040 (2.0 KB)
    
    eth2      Link encap:Ethernet  HWaddr 08:00:27:bd:cd:6c
              inet addr:192.168.0.16  Bcast:192.168.0.255  Mask:255.255.255.0
              inet6 addr: 2a02:2455:25f:e000:a00:27ff:febd:cd6c/64 Scope:Global
              inet6 addr: fe80::a00:27ff:febd:cd6c/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:412 errors:0 dropped:0 overruns:0 frame:0
              TX packets:103 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:31706 (31.7 KB)  TX bytes:8356 (8.3 KB)
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:12 errors:0 dropped:0 overruns:0 frame:0
              TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:888 (888.0 B)  TX bytes:888 (888.0 B)
    
    在主机上,我尝试在浏览器中进行访问:
  • 192.168.33.10:2345,得到:
  • GET / HTTP/1.1
    Host: 192.168.33.10:2345
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:77.0) Gecko/20100101 Firefox/77.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Connection: keep-alive
    Upgrade-Insecure-Requests: 1
    
  • 10.0.2.15:2345,什么也没有得到
  • 46.128.200.193:2345,什么也没有得到

  • 但是到目前为止,没有Vagrantfile的设置以及IP地址与端口2345的组合都将我重定向到eff.org。

    最佳答案

    我有不同的方法。
    我正在使用python简单的http服务器模块。
    代码来自other answer

    #!/usr/bin/python
    import SimpleHTTPServer
    import SocketServer
    
    class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
       def do_GET(self):
           self.send_response(301)
           self.send_header('Location','http://www.eff.org')
           self.end_headers()
    
    theport = 1234
    Handler = myHandler
    pywebserver = SocketServer.TCPServer(("", theport), Handler)
    
    print "Python based web server. Serving at port", theport
    pywebserver.serve_forever()
    
    关于多个接口(interface)。
    确保输出
    sudo lsof -i -P -n | grep LISTEN
    
    *:1234,即所有接口(interface)均用于此端口。
    web_fwd.p 2594         dev    3u  IPv4  60354      0t0  TCP *:1234 (LISTEN)
    

    关于linux - 无用的IP地址设置用于端口监听,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62508992/

    10-13 01:33