1
我知道Gunicorn中的on_exit
鉤子,但不知道如何或是否可以用這個鉤子在最終響應中添加一個「Connection:Close」HTTP標頭。在Gunicorn中,如何添加連接:關閉服務器關閉後的響應?
我之所以需要這個是告知關機的上游Nginx的代理,否則Nginx的賦予「502錯誤網關」的錯誤。
我知道Gunicorn中的on_exit
鉤子,但不知道如何或是否可以用這個鉤子在最終響應中添加一個「Connection:Close」HTTP標頭。在Gunicorn中,如何添加連接:關閉服務器關閉後的響應?
我之所以需要這個是告知關機的上游Nginx的代理,否則Nginx的賦予「502錯誤網關」的錯誤。
Gunicorn提供了工人掛鉤,可以在這個實例中使用它來在工人關閉時發送Connection:close標題。請嘗試以下鉤在gunicorn.conf
文件:
def pre_request(worker, req):
if not worker.alive:
header_dict = dict(req.headers)
header_dict['CONNECTION'] = 'close'
req.headers = header_dict.items()
Gunicorn店頭的元組的列表,所以很簡單的轉換成dictonary,覆蓋/插入連接頭和放回的對象的列表元組一旦完成。