从外部访问localhost?

时间:2016-04-13 14:23:35

标签: django localhost static-ip-address

这可能是一个非常基本的问题。我通过SSH连接到我的虚拟Ubuntu服务器并启动在localhost上运行的django webserver:

Downloading/unpacking django
  Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB): 6.6MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

April 13, 2016 - 14:16:19
Django version 1.9.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Ubuntu服务器有一个静态IP [x.xxx.xxx.xxx]所以从另一台网络上的另一台机器上我尝试使用该静态IP地址在浏览器中访问上述网站:

x.xxx.xxx.xxx:8000

但我明白了:

This site can’t be reached

x.xxx.xxx.xxx refused to connect.
ERR_CONNECTION_REFUSED

所以我假设我无法访问这样的网站,即使主机有外部/静态IP地址,也只能在主机上使用?

修改

答案也在: https://docs.djangoproject.com/en/1.9/intro/tutorial01/#the-development-server

python manage.py runserver 0.0.0.0:8000

1 个答案:

答案 0 :(得分:3)

您需要在0.0.0.0:8000上运行您的Web服务器,然后在外部使用ip或主机名来访问。使用0.0.0.0表示django服务将侦听所有已配置的网络接口。

检查blog post有关0.0.0.0的详细信息。

服务器故障上的另一个wikipedia可能会有所帮助。