Django/Pythonhttp服务器通过代理访问404,在本地工作

2024-05-01 22:02:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我以前遇到过这样一个问题:我无法确定Django应用程序中的罪魁祸首,导致所有请求都达到404。See the previous Stackoverflow question.

从那以后,我试图缩小这个问题的范围,因此开始了一个新的非常基本的Django应用程序。在

项目包括:

$ django-admin startproject tempor

我已经添加了test目录并导入了test函数

^{pr2}$

在视图.py以及测试功能

$ vi tempor/tempor/views.py

from django.http import HttpResponse

def test(request):
        return HttpResponse("OKAY")

然后我迁移了这个项目——正如Django建议的那样:

$ python manage.py check

$ python manage.py migrate

现在我运行服务器:

$ python3 manage.py runserver 0.0.0.0:8282

从本地服务器访问:

$ curl localhost:8282/test/
OKAY
$ curl <server-public-IP>:8282/test/
OKAY

(通过外部系统访问)

$ curl <server-public-IP>:8282/test/

<!DOCTYPE html>
<html lang="en">
[...]
<h1>Page not found <span>(404)</span></h1>
[...]
</html>

在设置.py我有:

ALLOWED_HOSTS = ['*']

如果我不使用'*',则会相应地通知外部系统,因为正在进行调试:

$ curl <server-public-IP>:8282/test/
[...]
DisallowedHost at http://<server-public-IP>:8282/test/
[...]

如果我在一个简单的pythonhttp服务器上尝试相同的方法,也会发生这种情况管理员py基本上是有用的。在

echo "OKAY" > /tmp/test/index.html
cd /tmp/test/
python -m http.server 8282
[...]

本地访问:

$ curl localhost:8282/index.html
OKAY
$ curl <server-public-IP>:8282/index.html
OKAY

远程访问:

$ curl <server-public-IP>:8282/index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
[...]
<p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>
[...]
</html>

在http服务器日志:

Serving HTTP on 0.0.0.0 port 8282 ...
<server-public-ip> - - [<timestamp>] "GET /index.html HTTP/1.1" 200 -
<proxy-ip> - - [<timestamp>] code 404, message File not found
<proxy-ip> - - [<timestamp>] "GET http://<server-public-ip>:8282/index.html HTTP/1.1" 404 -

目前,代理是一个简单的SSH端口转发-稍后将被NGINX取代。在


编辑:如果我为Django应用程序运行uwsgi,则同样适用:

$ uwsgi --http :8282 --module tempor.wsgi

本地访问工作-外部请求404。在


为什么要通过404代理请求我?如何解决此问题?


Tags: djangopytestip服务器httpindexmanage
1条回答
网友
1楼 · 发布于 2024-05-01 22:02:32

解决了,但我不明白为什么。在

问题是使用SSH端口转发直接访问Python服务器。中间Nginx解决了这个问题。在

See my ServerFault question for further details.

有关服务器设置,请参阅ServerFault问题。在

客户端设置可以归结为:

# SSH port forwarding, port 80 for Nginx access, port 8282 for direct Python webserver access
ssh -L 8181:<farawayhost-ip>:<80 or 8282> <sshuser>@<remotehost-ip>

# on a second terminal
export http_proxy=127.0.0.1:8181
curl <farawayhost-ip>

相关问题 更多 >