在使用sudo mn在mini-net中搭建了一个简单的网络之后,我在host1中使用nginx构建了一个web服务器。
我使用host1 xterm中的systemctl start nginx来构建web服务器。但它似乎在我的本地主机上启动了一个web服务器,而不是在迷你网络中。我无法通过迷你网络中的火狐访问host1和host2中的web服务器。
我的手术有什么问题吗?
发布于 2020-09-09 20:42:59
您无法连接到host1上的服务器的原因正如您所说的-主机不在那里,它运行在您的主机机器的127.0.0.1 (本地主机)上,而不是您的任何mininet主机上。
解决这个问题的方法是告诉nxinx通过服务器conf文件显式地在主机的(本地) IP上运行。
下面是一个适用于我的示例。( 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()这样,我们创建了nginx conf文件( nginx -conf.conf),然后告诉nginx使用该文件进行配置。
或者,如果您希望从主机上的终端启动它,请创建conf文件,然后使用命令告诉nginx使用该文件运行,如上面的代码所示。
https://stackoverflow.com/questions/44073906
复制相似问题