无法从本地服务器外部连接到 Django

Cannot Connect to Django from outside the Local Server

我在Centos7上搭建了一个基本的Django项目和应用程序,但我无法从外部访问它。检查下面的配置。

[root@pytoncentos7 myproject]# cat settings.py | grep ALLOWED
ALLOWED_HOSTS = ['*']

我 运行 我的服务器如下。

[root@pytoncentos7 myproject]#  python3.6 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 24, 2018 - 15:03:04
Django version 2.0.5, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

另外,防火墙不是运行ning

[root@pytoncentos7 myproject]# firewall-cmd --state
not running

下面是我的端口列表。

[root@pytoncentos7 myproject]# netstat -lptun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
PID/Program name
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      
1643/python3.6
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
876/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      
982/master
tcp6       0      0 :::3306                 :::*                    LISTEN      
986/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      
876/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      
982/master
udp        0      0 127.0.0.1:323           0.0.0.0:*                           
636/chronyd
udp6       0      0 ::1:323                 :::*                                
636/chronyd

此外,当我尝试从外部连接到端口 8000 时,它失败了!!但是我当然可以在本地远程登录。同时,我在 80 上有 Apache 运行ning 并且可以从 internal/external 访问,但是当我在端口 80 上禁用 Apache 和 运行 Django 时,我仍然可以在本地连接但不能在外部连接

Django 服务器只是我本地计算机上的一个虚拟机 运行

来自https://docs.djangoproject.com/en/2.0/ref/django-admin/#runserver

Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).

您必须 运行 使用命令 python3.6 manage.py runserver 0.0.0.0:8000

然后找出 VM 的本地 IP 并导航到它

http://192.168.0.XXX:8000   <--- Use local IP of VM

runserver不指定IP只会监听loopback接口

您需要python3.6 manage.py runserver 123.123.123.123(假设您的网络接口IP为“123.123.123.123”)。