简体   繁体   English

使用 Nginx 时 SSE 事件数据被截断

[英]SSE event data gets cut off when using Nginx

I am implementing a web interface using React and Flask.我正在使用 React 和 Flask 实现一个 Web 界面。 One component of this interface is a server sent event that is used to update data in the front end whenever it is updated in a database.此接口的一个组件是服务器发送的事件,用于在数据库中更新数据时更新前端中的数据。 This data is quite large and one event could contain over 16000 characters.这个数据非常大,一个事件可能包含超过 16000 个字符。

The React front end uses a reverse proxy to the flask back end in order to forward API requests to it. React 前端使用一个到 Flask 后端的反向代理,以便将 API 请求转发给它。 When accessing this back end directly, this works fine with the SSEs and the data is pushed as expected.当直接访问这个后端时,这对 SSE 工作正常,数据按预期推送。 However, when using Nginx to serve the reverse proxy, something weird happens.但是,当使用 Nginx 为反向代理提供服务时,会发生一些奇怪的事情。 It seems like nginx buffers and chunks the event stream and does not send the results until it has filled around 16000 characters.似乎 nginx 对事件流进行缓冲和分块,并且在填充大约 16000 个字符之前不会发送结果。 If the data from the event is smaller than this, it means the front end will have to wait until more events are sent.如果来自事件的数据小于此值,则意味着前端将不得不等待,直到发送更多事件。 If the data from the event is larger, it means the new-lines that tell the EventSource that a message has been received aren't part of the event.如果来自事件的数据较大,则意味着告诉 EventSource 已收到消息的换行符不是事件的一部分。 This results in the front end receiving event X when event X+1 is sent (that is, when the new-lines actually appear in the stream).这导致前端在发送事件 X+1 时(即当新行实际出现在流中时)接收事件 X。

This is the response header when using nginx:这是使用 nginx 时的响应头:

HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Thu, 19 Nov 2020 13:10:49 GMT
Content-Type: text/event-stream; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive

This is the response header when running flask run and accessing the port directly (I'm going to use gunicorn later but I tested with flask run to make sure nginx was the problem and not gunicorn):这是运行flask run并直接访问端口时的响应头(我稍后将使用gunicorn但我使用flask run进行了测试以确保nginx是问题而不是gunicorn):

HTTP/1.0 200 OK
Content-Type: text/event-stream; charset=utf-8
Cache-Control: no-transform
Connection: keep-alive
Connection: close
Server: Werkzeug/1.0.1 Python/3.7.6
Date: Thu, 19 Nov 2020 13:23:54 GMT

This is the nginx config in sites-available :这是sites-available的 nginx 配置:

upstream backend {
        server 127.0.0.1:6666;
}

server {
        listen 7076;
        root <path/to/react/build/>;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }

        location /api {
                include proxy_params;
                proxy_pass http://backend;
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_buffering off;
                proxy_cache off;
                chunked_transfer_encoding off;
        }
}

This config is based on the answer mentioned here .此配置基于此处提到的答案。

As you can see, both proxy_buffering and chunked_transfer_encoding is off, so I don't understand why this is happening.如您所见, proxy_bufferingchunked_transfer_encoding关闭,所以我不明白为什么会发生这种情况。 I have also tried changing the buffer sizes but without any luck.我也尝试过更改缓冲区大小,但没有任何运气。

Can anybody tell me why this is happening?谁能告诉我为什么会这样? How do I fix it such that using nginx results in the same behaviour as when I don't use it?如何修复它以便使用 nginx 导致与不使用它时相同的行为? Thank you.谢谢你。

The above mentioned configuration actually did work.上面提到的配置实际上确实有效。 However, the server I was using contained another nginx configuration that was overriding my configuration.但是,我使用的服务器包含另一个覆盖我的配置的 nginx 配置。 When the configuration parameters specific for SSEs were added to that configuration as well, things started working as expected.当特定于 SSE 的配置参数也添加到该配置时,事情开始按预期工作。 So this question was correct all along.所以这个问题一直是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM