简体   繁体   English

Django-无法发布Linux服务器

[英]Django - unable to publish Linux server

First of all, I can access the webpage from local host. 首先,我可以从本地主机访问该网页。 But if I were to access it from outside machines, I am unable to access the page. 但是,如果要从外部计算机访问它,则无法访问该页面。

This is what I get when I run python manage.py runserver 0.0.0.0:8000 这是我运行python manage.py runserver 0.0.0.0:8000

System check identified no issues (0 silenced).
September 18, 2018 - 05:45:59
Django version 2.0.6, using settings 'sage.settings'
Starting ASGI/Channels version 2.1.2 development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
2018-09-18 05:45:59,176 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-09-18 05:45:59,179 - INFO - server - Configuring endpoint tcp:port=8000:interface=0.0.0.0
2018-09-18 05:45:59,180 - INFO - server - Listening on TCP address 0.0.0.0:8000

Lets assume that my IP address for my Linux server is 70.111.222.333 . 假设我的Linux服务器IP地址为70.111.222.333 Then I should be able to access it from other computers by typing http://70.111.222.333:8000 on a browser. 然后,我应该能够通过在浏览器上键入http://70.111.222.333:8000从其他计算机访问它。 But it doesn't work. 但这是行不通的。

In my settings.py , this is how it looks right now: 在我的settings.py ,这是现在的样子:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['', '*']

The Linux server has a few virtual machines installed, and I've correctly set up fort forwarding for my application virtual machine, port 8000 , Local IP 192.168.1.100 . Linux服务器安装了一些虚拟机,并且我已经为我的应用程序虚拟机port 8000Local IP 192.168.1.100正确设置了堡垒转发。 And yet, I am still unable to publish my Linux server. 但是,我仍然无法发布我的Linux服务器。 It says that the site can't be reached. 它说无法访问该站点。

Any idea how to fix this? 任何想法如何解决这个问题?

I'm using Django-Channels, FYI 我正在使用Django频道,仅供参考

First of all you should use a python gateway server interface like gunicorn or uwsgi to serve your app in production. 首先,您应该使用像gunicorn或uwsgi之类的python网关服务器接口在生产中为您的应用程序提供服务。 Don't use the ./manage.py runserver command. 不要使用./manage.py runserver命令。

Second, if you run an app in production (that means DEBUG=False ), you need to serve static resources on your own, which means you need a webserver (like nginx or apache2). 其次,如果您在生产环境中运行应用程序(这意味着DEBUG=False ),则需要自己提供静态资源,这意味着您需要一个Web服务器(例如nginx或apache2)。

As soon as you set up a webserver to serve your static resources, create a proxy to the Django app. 设置网络服务器以提供静态资源后,请立即创建Django应用程序的代理。

For Apache: 对于Apache:

ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/

Also set the preserve host header to use the hostname of your virtual host. 还要将保留主机标头设置为使用虚拟主机的主机名。

ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto https

Here is a complete example of a virtual host: 这是虚拟主机的完整示例:

<VirtualHost *:80>
    ServerName example.ch
    ServerAlias www.example.ch

    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/

    ProxyPreserveHost On
    RequestHeader set X-Forwarded-Proto https
</VirtualHost>

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

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