【问题标题】:uvicorn error on AWS EC2 with uvicorn + fastapi使用 uvicorn + fastapi 在 AWS EC2 上出现 uvicorn 错误
【发布时间】:2021-04-12 16:10:18
【问题描述】:

我有一台在本地运行的服务器。当我在 AWS EC2 上运行它并在端口 8000 上从外部发送请求时,我收到以下错误:

$ uvicorn sql_app.main:app --host="0.0.0.0" --port=8000
INFO:     Started server process [9806]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
WARNING:  Invalid HTTP request received.
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 170, in handle_events
    event = self.conn.next_event()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 443, in next_event
    exc._reraise_as_remote_protocol_error()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_util.py", line 76, in _reraise_as_remote_protocol_error
    raise self
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 425, in next_event
    event = self._extract_next_receive_event()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 367, in _extract_next_receive_event
    event = self._reader(self._receive_buffer)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_readers.py", line 73, in maybe_read_from_IDLE_client
    request_line_re, lines[0], "illegal request line: {!r}", lines[0]
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_util.py", line 88, in validate
    raise LocalProtocolError(msg)
h11._util.RemoteProtocolError: illegal request line: bytearray(b'\x16\x03\x01\x02\x00\x01\x00\x01\xfc\x03\x03\x91\xa5\xe2Y\xf0\xa1\xdd\x1d+\x08\x1c\r\x15X\x1d@\x1e/\xb1N\x00\xb5\xe5\xec\xf3F\x1fm\x03\xa1{> \xa80\xb4\x14\x1aUs\xaa\xcd\xc3<s\xcd\xd1\x17\xdf3\x0e\xdbh\xd1c\x88}\x8c\x1f\xa5\x15\x9aa\x14I\x00 ')
WARNING:  Invalid HTTP request received.
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 170, in handle_events
    event = self.conn.next_event()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 443, in next_event
    exc._reraise_as_remote_protocol_error()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_util.py", line 76, in _reraise_as_remote_protocol_error
    raise self
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 425, in next_event
    event = self._extract_next_receive_event()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 367, in _extract_next_receive_event
    event = self._reader(self._receive_buffer)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_readers.py", line 68, in maybe_read_from_IDLE_client
    raise LocalProtocolError("illegal request line")
h11._util.RemoteProtocolError: illegal request line
WARNING:  Invalid HTTP request received.
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 170, in handle_events
    event = self.conn.next_event()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 443, in next_event
    exc._reraise_as_remote_protocol_error()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_util.py", line 76, in _reraise_as_remote_protocol_error
    raise self
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 425, in next_event
    event = self._extract_next_receive_event()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 367, in _extract_next_receive_event
    event = self._reader(self._receive_buffer)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_readers.py", line 68, in maybe_read_from_IDLE_client
    raise LocalProtocolError("illegal request line")
h11._util.RemoteProtocolError: illegal request line
WARNING:  Invalid HTTP request received.

如果您能告诉我如何在端口 80 上进行操作,那就太好了。

【问题讨论】:

  • ``` raise LocalProtocolError(msg) h11._util.RemoteProtocolError: 非法请求行: bytearray(b'\x16\x03\x01\x02\x00\x01\x00\x01\xfc\x03 \x03\x18\xc6\x16Xm\x87\x96`\xff\x97\xd4\x86\xa6d\x8b\x0e\xc0\xb3Dwh\xc0\xd7K\x90\xcfj\x1e\x1fr\xe1\xfe \x15\ xa0\xbd\xda\x8f\xc5~\xbd\x16\x11\xc2\xbd\x8b\x9a\x06\xe5P\xab\x8a\xff\xc8\xc0\xe3m\x10\xecOU\x91_U\x8f\x00 "\x9a\x9a\x13\x01\x13\x02\x13\x03\xc0+\xc0/\xc0,\xc00\xcc\xa9\xcc\xa8\xc0\x13\xc0\x14\x00\x9c\x00\ x9d\x00/\x005\x00') ```

标签: python amazon-web-services fastapi uvicorn


【解决方案1】:

我得到了同样神秘的WARNING: Invalid HTTP request received. 错误,堆栈跟踪没有帮助。我尝试了所有推荐的环境变量调整,但都没有奏效(请参阅FastAPI issue #680uvicorn issue #441)。

我的问题是,当我调用我的 FastAPI 微服务时,我使用的是 https,而我的微服务不支持 HTTPS。我将网址从https 更改为http,它开始按预期工作。

请注意,如果您的服务需要 HTTPS 支持,您可以按照 Ilgizar Murzakov 的建议add HTTPS support

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并通过添加 HTTPS 支持解决了它。我用 LetsEncrypt 证书配置了 NGINX。确保您使用的端口在您的安全组设置中处于打开状态。

    【讨论】:

      【解决方案3】:

      有同样的问题。在应用程序负载均衡器上禁用 http2 对我有帮助。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案4】:

      尝试将默认 timeout 更改为 0。

      在启动 uvicorn 时使用这个标志。

      --timeout-keep-alive 0 
      

      【讨论】:

      • 您能补充一下您的理由吗?为什么您认为这可能有助于解决问题?
      猜你喜欢
      • 2021-10-29
      • 2023-03-18
      • 2022-12-21
      • 2021-04-02
      • 2021-10-14
      • 2021-04-12
      • 2021-07-30
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多