简体   繁体   中英

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

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 . Then I should be able to access it from other computers by typing http://70.111.222.333:8000 on a browser. But it doesn't work.

In my settings.py , this is how it looks right now:

# 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 . And yet, I am still unable to publish my Linux server. It says that the site can't be reached.

Any idea how to fix this?

I'm using Django-Channels, FYI

First of all you should use a python gateway server interface like gunicorn or uwsgi to serve your app in production. Don't use the ./manage.py runserver command.

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).

As soon as you set up a webserver to serve your static resources, create a proxy to the Django app.

For 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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