AWS EC2 上的 Django 无法连接,常规 Python 连接

Django on AWS EC2 unable to connect, regular Python connects

我正在尝试 运行 来自 Github dev code. The code is running on an AWS EC2 instance, and I'm running Firefox on my local desktop (Kubuntu Linux) 的 Django 开发版本。当我尝试连接时出现此错误:

Unable to connect

Firefox can't establish a connection to the server at 1.2.3.4:8000.

我知道我已经正确设置了防火墙、AWS 安全组和 VPS,因为我可以在启动正常的 Python Web 服务器时访问该端口:

$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...
bzq-241-14.net - - [16/Mar/2015 10:17:28] "GET / HTTP/1.1" 200 -
bzq-241-14.net - - [16/Mar/2015 10:17:29] code 404, message File not found
bzq-241-14.net - - [16/Mar/2015 10:17:29] "GET /favicon.ico HTTP/1.1" 404 -
bzq-241-14.net - - [16/Mar/2015 10:17:29] code 404, message File not found
bzq-241-14.net - - [16/Mar/2015 10:17:29] "GET /favicon.ico HTTP/1.1" 404 -
^C
Keyboard interrupt received, exiting.

然而,Django 服务器甚至没有注册一个被拒绝的连接:

$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2015 - 11:00:30
Django version 1.9, using settings 'Reboot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

连接问题的可能原因是什么?

开发服务器绑定到本地主机,无法从其他机器访问。您可以通过此处记录的参数设置端口:https://docs.djangoproject.com/en/1.7/ref/django-admin/#runserver-port-or-address-port .

但最好的方法当然是正确部署 Django:https://docs.djangoproject.com/en/1.7/howto/deployment/

凭直觉,我尝试使用服务器的 IP 地址和端口启动服务器,但没有帮助:

$ python manage.py runserver 1.2.3.4:8000
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2015 - 11:08:43
Django version 1.9, using settings 'Reboot.settings'
Starting development server at http://1.2.3.4:8000/
Quit the server with CONTROL-C.
Error: That IP address can't be assigned-to.

但是,使用 LAN IP 地址启动 Django 服务器确实有帮助:

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 2a:da:26:29:39:8e  
          inet addr:10.0.0.101  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::28da:26ff:fe29:398e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:535102 errors:0 dropped:0 overruns:0 frame:0
          TX packets:364585 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:333175535 (333.1 MB)  TX bytes:183546758 (183.5 MB)
          Interrupt:43 

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:16436  Metric:1
          RX packets:12760 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12760 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:988343 (988.3 KB)  TX bytes:988343 (988.3 KB)

$ python manage.py runserver 10.0.0.101:8000
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2015 - 11:09:08
Django version 1.9, using settings 'Reboot.settings'
Starting development server at http://10.0.0.101:8000/
Quit the server with CONTROL-C.
[16/Mar/2015 11:09:11]"GET / HTTP/1.1" 200 1767
[16/Mar/2015 11:10:04]"GET / HTTP/1.1" 200 1767
[16/Mar/2015 11:10:05]"GET /favicon.ico HTTP/1.1" 404 1941
[16/Mar/2015 11:10:05]"GET /favicon.ico HTTP/1.1" 404 1941

因此,在 AWS(可能还有所有远程服务器)上,必须使用 LAN IP 地址启动 Django 服务器才能访问远程测试服务器。

可能会对初学者或 AWS 新手有所帮助

我知道我必须向安全组添加一些规则,但我不知道具体是哪些规则,所以这里是您应该添加的规则

Go To -> EC2 > Security Groups > your-security-group > Edit inbound rules

在此之后,您必须使用以下命令启动 django 服务器

python3 manage.py runserver 0.0.0.0:8000

现在您可以在 EC2 public 域的 :8000 上访问您的 django 应用程序