python - nginx转发x-mixed-replace请求遇到的问题
本文介绍了python - nginx转发x-mixed-replace请求遇到的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
后端的服务返回的response类型是x-mixed-replace,也就是会需要和客户端建立一个长连接,持续不断的推送数据。
但通过nginx做代理转发后,这个数据推送不是连续的,而是等nginx缓冲区满了之后才会把数据返回给客户端。
请问如何配置nginx让后端服务返回的数据不经过nginx缓冲区直接返回给客户端?
代码:
@expose('/get_log/')
def get_log(self):
def gen(consumer):
while True:
for message in consumer:
yield b'--frame\r\n' + str(message.value) + b'\r\n'
break
return Response(gen(consumer=get_kafka_consumer()),
mimetype='multipart/x-mixed-replace; boundary=frame')
解决方案
已经有人帮忙解决了:
_headers = dict()
_headers['X-Accel-Buffering'] = 'no' # nginx不对此请求做缓冲
return Response(gen(consumer=get_kafka_consumer()), headers=_headers)
这篇关于python - nginx转发x-mixed-replace请求遇到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文