Problem:
I can’t:
1)use a local browser to open http://<public IP>:8000
2)use terminal to curl -v http://<public IP>:8000
3)Ping
I can:
1)use terminal to run curl -v http://127.0.0.1:8000
, the result indicates the django server is working.
Background:
I’m working on a MacOS. Python 3.9.13,Django 4.1.7, MySQL 8.0.31.
I setup my django project in my EC2 server, also setup a RDS which is working now.
Today, I try to open the project’s index page via my browser, but there is no response.
So I checked inbound rules:
1)All traffic :0.0.0.0/0
2)Custom TCP :port: 8000, 0.0.0.0/0
3)Custom TCP :port: 8000, 172.31.0.0/20 (subnet, IPv4 CIDR)
4)Custom TCP :port: 8000, 172.31.16.0/20 (subnet, IPv4 CIDR)
5)Custom TCP :port: 8000, 172.31.32.0/20 (subnet, IPv4 CIDR)
6)Custom ICMP:echo request, 0.0.0.0/0
7)All TCP : My IP, x.x.x.x/32
8)SSH: port 22, 0.0.0.0/0
Also I set
1)public IP in the settings.py
ALLOW_HOSTS = ['public IP', '127.0.0.1'] (no port)
2)firewall allows 8000 port
sudo firewall-cmd --add-port=8000/tcp --permanent
Finally, I run django server python manage.py runserver 0.0.0.0:8000
Result:
1)curl -v http://127.0.0.1:8000
,
* Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET /login/ HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 22 Mar 2023 11:43:26 GMT
< Server: WSGIServer/0.2 CPython/3.9.13
< Content-Type: text/html; charset=utf-8
< X-Frame-Options: DENY
< Vary: Cookie
< Content-Length: 10761
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< Cross-Origin-Opener-Policy: same-origin
< Set-Cookie: csrftoken="xxxx-I remove this token"; expires=Wed, 20 Mar 2024 11:43:26 GMT; Max-Age=31449600; Path=/; SameSite=Lax
<
<!DOCTYPE html>
<html lang="en">
<head>
...
end...
2)curl -v http://<public IP>:8000
curl: Failed to connect to <public IP> port 8000 after 130052 ms: Couldn't connect to server
When I remove ‘127.0.0.1’ from the settings.py, the curl -v http://127.0.0.1:8000
is also not working.
So I set ALLOW_HOSTS = [‘*’], the curl -v http://127.0.0.1:8000
is working ,but in terms of the the curl -v <public IP>:8000
is still not working.
That’s my problem.
Your help will be VERY APPRECIATED!
2
Answers
I solved my problem, thx to the author:Start Django development server on AWS EC2 Amazon Linux
I first test
curl http://ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com:8000
, and it's working, and then I changed the security group and add it.You need to add a security group rule to allow traffic on port 8000 from your router’s IP address.
Also, make sure you have internet gateway in place to allow connections from the outside world into the EC2.