簡単なオプションは、バックエンド プロセスを続行しながらクライアント接続をできるだけ早く終了することです。
server {
location /test {
# map 402 error to backend named location
error_page 402 = @backend;
# pass request to backend
return 402;
}
location @backend {
# close client connection after 1 second
# Not bothering with sending gif
send_timeout 1;
# Pass the request to the backend.
proxy_pass http://127.0.0.1:8080;
}
}
上記のオプションは単純ですが、接続が切断されたときにクライアントがエラー メッセージを受け取る可能性があります。ngx.say ディレクティブは、"200 OK" ヘッダーが送信されることを保証し、これは非同期呼び出しであるため、処理が遅れることはありません。これには ngx_lua モジュールが必要です。
server {
location /test {
content_by_lua '
-- send a dot to the user and transfer request to backend
-- ngx.say is an async call so processing continues after without waiting
ngx.say(".")
res = ngx.location.capture("/backend")
';
}
location /backend {
# named locations not allowed for ngx.location.capture
# needs "internal" if not to be public
internal;
# Pass the request to the backend.
proxy_pass http://127.0.0.1:8080;
}
}
より簡潔な Lua ベースのオプション:
server {
location /test {
rewrite_by_lua '
-- send a dot to the user
ngx.say(".")
-- exit rewrite_by_lua and continue the normal event loop
ngx.exit(ngx.OK)
';
proxy_pass http://127.0.0.1:8080;
}
}
間違いなく興味深い挑戦です。