如何从外部访问本地Django Web服务器

问题:如何从外部访问本地Django Web服务器

我按照此处的说明使用内置的网络服务器运行Django,并能够使用成功运行它python manage.py runserver。如果我从Web服务器本地访问127.0.0.1:port,则将显示Django页面,表明它可以工作。

我意识到Django网络服务器不是生产服务器,但对我而言,测试至关重要的是,它可以从外部环境进行访问-即,不是从服务器上的Web浏览器而是从其他计算机上进行访问。

我试过了:

http://mywebserver:port_django_runs_on

但它没有用。我还尝试使用IP(基于ifconfig)访问:

http://myipaddress:port_django_runs_on 

这也不起作用。

Web服务器正在运行,因此必须从外部可以访问它,但我不确定如何。我没有在Apache上配置Django,但我在Apache上运行Linux。

有关如何执行此操作的任何想法?

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.

I realize the Django webserver is not a production server, but it’s important for me for testing purposes to be able to access it from the outside world — i.e. not from a web browser on the server, but from a different computer.

I tried:

http://mywebserver:port_django_runs_on

but it did not work. I also tried using the IP instead (based on ifconfig) to access:

http://myipaddress:port_django_runs_on 

which did not work either.

The web server is running so it must be accessible from the outside, I’m just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.

Any ideas on how to do this?


回答 0

您必须运行开发服务器,以使其侦听网络接口

例如

python manage.py runserver 0.0.0.0:8000

在端口8000的每个接口上侦听。

使用IP或主机名访问Web服务器都没有关系。我猜您仍在自己的局域网中。
如果您确实要从外部访问服务器,则还必须将路由器配置为将端口转发到例如8000服务器。


检查服务器上的防火墙,是否允许使用中端口的传入连接!

假设您可以成功地从外部访问Apache服务器,则还可以尝试以下操作:

  • 停止Apache服务器,以便该端口80可用。
  • 使用以下命令启动开发服务器 sudo python manage.py runserver 0.0.0.0:80

You have to run the development server such that it listens on the interface to your network.

E.g.

python manage.py runserver 0.0.0.0:8000

listens on every interface on port 8000.

It doesn’t matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.


Check your firewall on your server whether incoming connections to the port in use are allowed!

Assuming you can access your Apache server from the outside successfully, you can also try this:

  • Stop the Apache server, so that port 80 is free.
  • Start the development server with sudo python manage.py runserver 0.0.0.0:80

回答 1

我必须将此行添加到settings.py才能使其正常工作(否则从另一台计算机访问时显示错误)

ALLOWED_HOSTS = ['*']

然后使用以下命令运行服务器:

python manage.py runserver 0.0.0.0:9595

还要确保防火墙允许连接到该端口

I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)

ALLOWED_HOSTS = ['*']

then ran the server with:

python manage.py runserver 0.0.0.0:9595

Also ensure that the firewall allows connections to that port


回答 2

从以下选择一个或多个:

  • 您的应用程序未成功侦听预期的IP:PORT
    • 因为您尚未成功配置
    • 因为用户没有权限
  • 您的应用程序已在预期的IP:PORT上成功侦听,但客户端无法访问它,因为
    • 服务器本地iptables阻止了它。
    • 防火墙阻止了它。

因此,您可以通过lsof -i在计算机上以root用户身份运行来检查您的应用程序是否成功侦听,并查找python具有您指定的相应端口的条目。

非root用户通常不能绑定到<1024的端口。

您需要查看一下iptables -nvL是否存在阻止访问您要绑定应用程序的ip:port的规则。

如果有上游防火墙,而您对其了解不多,则需要与网络管理员联系。

Pick one or more from:

  • Your application isn’t successfully listening on the intended IP:PORT
    • Because you haven’t configured it successfully
    • Because the user doesn’t have permission to
  • Your application is listening successfully on the intended IP:PORT, but clients can’t reach it because
    • The server local iptables prevents it.
    • A firewall prevents it.

So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you’ve specified.

Non-root users generally cannot bind to ports < 1024.

You’ll need to look at iptables -nvL to see if there’s a rule that would prevent access to the ip:port that you are trying to bind your application to.

If there is an upstream firewall and you don’t know much about it, you’ll need to talk to your network administrators.


回答 3

只是这样做:

python manage.py runserver 0:8000

通过以上命令,您实际上是将其绑定到外部IP地址。因此,现在当您使用端口号访问IP地址时,就可以在浏览器中毫无问题地访问它。

只需在浏览器地址栏中输入以下内容:

<your ip address>:8000

例如:

192.168.1.130:8000

您可能需要编辑settings.py在最后一行的settings.py中添加以下内容:

ALLOWED_HOSTS = ['*']

希望这会有所帮助…

just do this:

python manage.py runserver 0:8000

by the above command you are actually binding it to the external IP address. so now when you access your IP address with the port number, you will be able to access it in the browser without any problem.

just type in the following in the browser address bar:

<your ip address>:8000

eg:

192.168.1.130:8000

you may have to edit the settings.py add the following in the settings.py in the last line:

ALLOWED_HOSTS = ['*']

hope this will help…


回答 4

对于AWS用户。

我必须按照以下步骤到达目的地。

1)确保在sudo级别安装了pip和django

  • 须藤apt-get install python-pip
  • sudo pip安装Django

2)确保安全组入站规则包括端口80上的http(0.0.0.0/0)

  • 通过AWS控制台配置

3)将公共IP和DNS添加到ALLOWED_HOSTS

  • ALLOWED_HOSTS是一个列表对象,您可以在settings.py中找到
  • ALLOWED_HOSTS = [“ 75.254.65.19”,“ ec2-54-528-27-21.compute-1.amazonaws.com”]

4)在端口80上使用sudo启动开发服务器

  • 须藤python manage.py runserver 0:80

现在可以从以下任一站点访问该站点(不需要:80,因为http默认是):

  • [公共DNS],即ec2-54-528-27-21.compute-1.amazonaws.com
  • [公共IP],即75.254.65.19

For AWS users.

I had to use the following steps to get there.

1) Ensure that pip and django are installed at the sudo level

  • sudo apt-get install python-pip
  • sudo pip install django

2) Ensure that security group in-bound rules includ http on port 80 for 0.0.0.0/0

  • configured through AWS console

3) Add Public IP and DNS to ALLOWED_HOSTS

  • ALLOWED_HOSTS is a list object that you can find in settings.py
  • ALLOWED_HOSTS = [“75.254.65.19″,”ec2-54-528-27-21.compute-1.amazonaws.com”]

4) Launch development server with sudo on port 80

  • sudo python manage.py runserver 0:80

Site now available at either of the following (no need for :80 as that is default for http):

  • [Public DNS] i.e. ec2-54-528-27-21.compute-1.amazonaws.com
  • [Public IP] i.e 75.254.65.19

回答 5

我要在这里添加:

  1. sudo python manage.py runserver 80

  2. 转到您的电话或计算机,然后192.168.0.12在浏览器中输入计算机的内部IP(例如)。

此时,您应该已连接到Django服务器。

这也应该在没有sudo的情况下起作用:

python manage.py runserver 0.0.0.0:8000

I’m going to add this here:

  1. sudo python manage.py runserver 80

  2. Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.

At this point you should be connected to the Django server.

This should also work without sudo:

python manage.py runserver 0.0.0.0:8000

回答 6

如果您使用的是Docker,则需要确保端口也已公开

If you are using Docker you need to make sure ports are exposed as well


回答 7

2020年更新方法

python manage.py runserver yourIp:8000

ALLOWED_HOSTS = ["*"]

UPDATED 2020 TRY THIS WAY

python manage.py runserver yourIp:8000

ALLOWED_HOSTS = ["*"]

回答 8

在终端中安装ngrok

sudo apt-get install -y ngrok-client

运行之后:

ngrok http 8000
or 
ngrok http example.com:9000 

install ngrok in terminal

sudo apt-get install -y ngrok-client

after that run:

ngrok http 8000
or 
ngrok http example.com:9000