简体   繁体   English

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

[英]Django on AWS EC2 unable to connect, regular Python connects

I am trying to run the development version of Django from the Github dev code .我正在尝试从Github 开发代码运行 Django 的开发版本。 The code is running on an AWS EC2 instance , and I'm running Firefox on my local desktop ( Kubuntu Linux ).该代码在AWS EC2 实例上运行,我在本地桌面 ( Kubuntu Linux ) 上运行 Firefox。 When I try to connect I get this error:当我尝试连接时,出现此错误:

Unable to connect无法连接

Firefox can't establish a connection to the server at 1.2.3.4:8000. Firefox 无法与 1.2.3.4:8000 的服务器建立连接。

I know that I have the firewall, AWS security groups, and VPS set up properly as I can access the port when starting a normal Python web server:我知道我已经正确设置了防火墙、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.

However, the Django server doesn't even register a rejected connection:然而,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.

What might be the cause of the connection issue?连接问题的原因可能是什么?

The development server is bound to localhost and not accessible from other machines. 开发服务器绑定到localhost,无法从其他计算机访问。 You can set the port via a parameter as documented here: https://docs.djangoproject.com/en/1.7/ref/django-admin/#runserver-port-or-address-port . 您可以通过此处记录的参数设置端口: https : //docs.djangoproject.com/en/1.7/ref/django-admin/#runserver-port-or-address-port

But of cause the best way would be to deploy Django properly: https://docs.djangoproject.com/en/1.7/howto/deployment/ 但是,最好的方法是正确部署Django: https//docs.djangoproject.com/en/1.7/howto/deployment/

On a hunch, I tried starting the server with the IP address and port of the server, that did not help: 凭直觉,我尝试使用服务器的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.

However, starting the Django server with the LAN IP address did help: 但是, 使用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

Thus, on AWS (and possibly all remote servers) one must start the Django server with the LAN IP address in order to access the remote test server. 因此,在AWS(可能还有所有远程服务器)上,必须使用LAN IP地址启动Django服务器才能访问远程测试服务器。

Might help a beginner or someone new to AWS可能对初学者或 AWS 新手有所帮助

I knew I had to add some rules to the security groups but I didn't knew exactly which ones so here are the rules that you should add我知道我必须向安全组添加一些规则,但我不确切知道哪些规则,所以这里是您应该添加的规则

Go To -> EC2 > Security Groups > your-security-group > Edit inbound rules转到 -> EC2 > 安全组 > your-security-group > 编辑入站规则

after this you have to start the django server using the following在此之后,您必须使用以下命令启动 django 服务器

python3 manage.py runserver 0.0.0.0:8000

And now you can access your django app on :8000 of your EC2 public domain现在您可以在您的 EC2 公共域的 :8000 上访问您的 django 应用程序

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

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