python - 如何在端口 443 和 80 linux - Ubuntu 或 Raspberry PI 上运行 Django Web 应用程序?
问题描述
我对 Django 很陌生。我认为我无法将 django 应用程序作为 sudo 运行,因为所有 pip 相关模块都是为用户安装的,而不是为 sudo 用户安装的。所以,这是一个基本问题,比如我如何运行可以监听端口 80 和端口 443 的 django 应用程序。
所以,到目前为止,我已经尝试了以下选项 - 即预路由 - NAT
我使用以下命令运行我的应用程序 -
$python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 13, 2018 - 03:04:41
Django version 2.1.1, using settings 'WebBlogger.settings'
Starting development server at http://127.0.0.1:8000/
接下来,这是我的 iptables 设置,但对我没有任何作用
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
$sudo iptables -t nat --line-numbers -n -L
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
1 DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 8000
Chain INPUT (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
1 DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
2 RETURN all -- 192.168.122.0/24 224.0.0.0/24
3 RETURN all -- 192.168.122.0/24 255.255.255.255
4 MASQUERADE tcp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
5 MASQUERADE udp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
6 MASQUERADE all -- 192.168.122.0/24 !192.168.122.0/24
Chain DOCKER (2 references)
num target prot opt source destination
1 RETURN all -- 0.0.0.0/0 0.0.0.0/0
我做了 http:// 并且我看到连接被拒绝。我不知道如何调试 NAT 的东西,无论它是否真的命中 NAT。我该如何调试以及正确的解决方案是什么?
解决方案
在理想世界中,您将需要一个网络服务来与您的 Django 对话。
Web server (port 80/443) -> gunicorn (wigs) -> Django (port 8000)
如果你只是想让 Django 开发服务器在 80 上运行,那么试试
python manage.py runserver 0.0.0.0:80
并确保没有其他进程正在使用端口 80。
推荐阅读
- angular - 专注于按钮 p-toast primeng modal
- .net-core - 运行设置文件不适用于 SpecFlow+MSTest+SpecRun.Specflow
- javascript - React-native 滑块不保存当前值,重置为默认值而不是新值
- django - 在另一个 Django Restframework 视图中调用 Django Rest Framework 视图
- sharepoint-2013 - CAML 查询空超链接字段导致列表不显示
- facebook - 如何让“Instagram 公共内容访问”功能在我的测试应用中运行?
- google-apps-script - 在选定位置插入新行并从上面的行中复制公式
- javascript - 如何修复 PHP 中的 'Included content of the div' 错误
- mysql - 将 MySQL 子查询中的额外列添加到查询结果中
- javascript - 试图找出这个 requestAnimationFrame 函数中的变量“now”来自哪里,或者如何动态获取它的值