使用daphne+pagekite部署FastAPI服务访问公网域名返回404而非JSON
问题排查及解决方法
核心问题原因
从你提供的pagekite运行日志可以看出,请求并没有转发到本地8000端口运行的FastAPI服务:日志显示Flying builtin HTTPD,说明你使用的pagekite启动命令参数格式错误,pagekite没有识别到转发到本地8000的规则,启用了自带的内置HTTP服务,因此所有公网请求都返回内置服务的404响应。
同时日志中的ValueError: not enough values to unpack报错也是参数格式错误导致的。
解决步骤
1. 修正pagekite启动命令
使用简化的pagekite命令格式即可避免参数错误,直接指定本地服务端口和公网绑定的域名端口:
python3 pagekite.py 8000 something.pagekite.me:8080
如果要使用完整的--service_on参数,需要补全参数结构:
# 把命令中的<你的pagekite密码>替换为你注册pagekite时设置的密码 python3 pagekite.py --frontend=something.pagekite.me:8080 --service_on=http:something.pagekite.me:8080:0.0.0.0:8000:<你的pagekite密码>
2. 验证转发规则生效
启动修正后的pagekite后,确认日志中出现以下内容,说明转发规则配置正确:
~<> Flying localhost:8000 as https://something.pagekite.me:8080/
3. 可选优化(避免参数解析错误)
建议给FastAPI路由的参数添加类型标注,提升参数解析的稳定性:
# 把原有的getcoordinates函数修改为以下形式 @app.get("/coordinates/") def getcoordinates(direction_x: int, direction_y: int, gas: int): global directionX,directionY, Gas directionX = direction_x directionY = direction_y Gas = gas return {"data":(direction_x,direction_y,gas)}
完成以上操作后,再次访问公网地址即可正常拿到和本地访问一致的JSON响应。
内容的提问来源于stack exchange,提问作者Petr L.
相关产品推荐
相关产品推荐

