【问题标题】:uWSGI 400 Bad RequestuWSGI 400 错误请求
【发布时间】:2022-08-09 19:21:06
【问题描述】:

我正在尝试通过 uWSGI/nginx 为 python 应用程序提供服务。 Ubuntu 21.10uWSGI 2.0.20

该应用程序有几个端点:

  1. 用于测试的 GET(它只返回字符串 \"hello\")
  2. 具有不同路由的 POST 端点,它接受一些 json 数据并返回一些输出。

    该应用程序在托管环境中运行。

    • 当我尝试将它放在 nginx 服务器后面时,GET 端点工作正常,但 POST 重新调整为 400
    • 然后我尝试仅通过烧瓶运行应用程序(无 uWSGI/nginx),两个端点都工作。
    • 然后我关闭flask进程并通过命令行运行uWSGI:

    uwsgi --socket 0.0.0.0:5555 --protocol=http -w runserver:app

    在这种情况下,GET 可以正常工作,

    [pid: 19308|app: 0|req: 5/5] 103.113.137.67 () {36 vars in 705 bytes} [Tue Nov 23 08:21:12 2021] GET / => generated 5 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 78 bytes (1 switches on core 0)
    

    但 POST 失败

    [pid: 19308|app: 0|req: 2/2] 103.113.137.67 () {26 vars in 394 bytes} [Tue Nov 23 07:52:40 2021] POST /distance/term => generated 187 bytes in 1 msecs (HTTP/1.1 400) 2 headers in 89 bytes (1 switches on core 0)
    

    (nginx作为反向代理被配置为监听55555端口,所以5555上的uWSGI不会导致端口冲突)

    所以:

    • 应用程序没有问题。否则烧瓶也会崩溃
    • Nginx 配置似乎没有问题; GET 端点正常工作

    我该如何调试这个问题?

    标签: python nginx flask uwsgi


    【解决方案1】:

    重复你的错误

    由于您没有发布有关您的 POST 端点以及您如何访问您的应用程序的代码。所以我假设你的代码如下:

    # runserver.py
    from flask import Flask
    from flask import request
    
    app = Flask(__name__)
    
    @app.route('/post',methods=['POST'])
    def post():
        data=request.form['data']
        return data
    
    @app.route('/get',methods=['GET'])
    def get():
        return "hello"
    

    然后像你一样启动 uwsgi

    uwsgi --socket 0.0.0.0:5555 --protocol=http -w runserver:app
    

    假设您正在通过 bash 访问您的应用程序

    curl -X POST http://localhost:5555/post -d '{"data": "someData"}' -H 'Content-Type: application/json'
    

    然后您可以在 bash 中看到以下指示 400 bad request 错误。

    <!doctype html>
    <html lang=en>
    <title>400 Bad Request</title>
    <h1>Bad Request</h1>
    <p>The browser (or proxy) sent a request that this server could not understand.</p>
    

    同时,如果您通过curl http://localhost:5555/get 访问GET 端点,您可以正确收到“hello”。

    原因:

    如果你真的在做上面的事情,那么原因是错配在您发送的数据类型和 Web 应用要解析的数据类型之间。 request.form用于读取格式为key1=value1&amp;key2=value2的数据及以上请求发送格式为{key:value}的数据

    解决方案:

    如果您真的在做上述事情,那么您有两种解决方案:

    • 第一个解决方案:更改您发送的数据类型。在上述情况下,您可以访问您的应用,例如

    curl -X POST http://localhost:5555/post -d "data=someData" -H 'Content-Type:application/x-www-form-urlencoded'

    ,发送格式为key=valuerequest.form 的数据可以正确解析。您可以在 bash 中看到结果 someData

    • 第二种解决方案:更改 Web 应用程序解析数据的方式。如果您发送格式为 json 的数据,那么您可以在 runserver.py 中将 request.form 替换为 request.json,例如
    @app.route('/post',methods=['POST'])
    def post():
        data=request.json['data']
        return data
    

    然后访问您的应用

    curl -X POST http://localhost:5555/post -d '{"data": "someData"}' -H 'Content-Type: application/json'

    ,您将在 bash 中看到结果 someData

    【讨论】:

      猜你喜欢
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      相关资源
      最近更新 更多