简体   繁体   English

mininet中的Web服务器(nginx)

[英]Web server(nginx) in mininet

After using sudo mn to build a simple network in mini-net, I use nginx to build a web server in host1.在mini-net中使用sudo mn搭建了一个简单的网络后,我在host1中使用nginx搭建了一个web服务器。

I use systemctl start nginx in host1 xterm to build a web server.我在 host1 xterm 中使用systemctl start nginx来构建一个 web 服务器。 But it seems it starts a web server on my localhost, not in the mini-net.但它似乎在我的本地主机上启动了一个 Web 服务器,而不是在迷你网中。 I cannot access the web server in host1 and host2 by Firefox in mini-net.我无法在 mini-net 中通过 Firefox 访问 host1 和 host2 中的 Web 服务器。

Is there anything wrong in my operation?我的操作有什么问题吗?

the reason why you cannot connect to the server on host1 is as you said - the host isn't there it's running on 127.0.0.1 (localhost) of your hosts's machine not any of your mininet hosts.您无法连接到主机 1 上的服务器的原因正如您所说 - 主机不在那里,它运行在主机机器的 127.0.0.1(本地主机)上,而不是您的任何 mininet 主机上。

The way to get around this is by telling nxinx to run on your host's (local) IP explicitly via the server conf file.解决这个问题的方法是通过服务器 conf 文件明确告诉 nxinx 在主机的(本地)IP 上运行。

Here's an example that works for me.这是一个对我有用的例子。 (Tested with nginx 1.4.6, mininet 2.3.0 and ubuntu 18.04) (使用 nginx 1.4.6、mininet 2.3.0 和 ubuntu 18.04 测试)

from mininet.topo import Topo
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.net import Mininet
import time

class DumbbellTopo(Topo):
    def build(self, bw=8, delay="10ms", loss=0):
        switch1 = self.addSwitch('switch1')
        switch2 = self.addSwitch('switch2')
        appClient = self.addHost('aClient')
        appServer = self.addHost('aServer')
        crossClient = self.addHost('cClient')
        crossServer = self.addHost('cServer')
        self.addLink(appClient, switch1)
        self.addLink(crossClient, switch1)
        self.addLink(appServer, switch2)
        self.addLink(crossServer, switch2)
        self.addLink(switch1, switch2, bw=bw, delay=delay, loss=loss, max_queue_size=14)

def simulate():
    dumbbell = DumbbellTopo()
    network = Mininet(topo=dumbbell, host=CPULimitedHost, link=TCLink, autoPinCpus=True)
    network.start()

    appClient = network.get('aClient')
    appServer = network.get('aServer')

    wd = str(appServer.cmd("pwd"))[:-2]

    appServer.cmd("echo 'b a n a n a s' > available-fruits.html")
    appServer.cmd("echo 'events { } http { server { listen " + appServer.IP() + "; root " + wd + "; } }' > nginx-conf.conf") # Create server config file
    appServer.cmd("sudo nginx -c " + wd + "/nginx-conf.conf &") # Tell nginx to use configuration from the file we just created

    time.sleep(1) # Server might need some time to start

    fruits = appClient.cmd("curl http://" + appServer.IP() + "/available-fruits.html")
    print(fruits)

    appServer.cmd("sudo nginx -s stop")
    network.stop()

if __name__ == '__main__':
    simulate()

This way we create the nginx conf file (nginx-conf.conf), then tell nginx to use this for its configuration.这样我们创建了 nginx conf 文件(nginx-conf.conf),然后告诉 nginx 使用它进行配置。

Alternatively if you want to start it from a terminal on the host, create the conf file and then use the command to tell nginx to run with this file as shown in the code above.或者,如果您想从主机上的终端启动它,请创建 conf 文件,然后使用命令告诉 nginx 使用此文件运行,如上面的代码所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM