Skip to content

v2.7.12

Latest

Choose a tag to compare

@matsumotory matsumotory released this 17 Nov 17:56
49d26b8

Behavior changes: read before upgrading

1. A HEAD, 204 or 304 response through an mruby output filter keeps its connection available (#853)

  • Before: a response that ends at its header, that is the response to a HEAD request, a 304 that nginx answers to a GET with If-Modified-Since or If-None-Match, or a 204 such as that of return 204;, received its header with Connection: keep-alive when it passed an mruby output filter, and nginx then closed the connection. A request that the client sent next on that connection got no response. This happened in a location with mruby_output_header_filter or mruby_output_header_filter_code and no mruby body filter, for HEAD, 204 and 304. It happened in a location with mruby_output_body_filter or mruby_output_body_filter_code for a response without Content-Length, such as a listing of autoindex or the 304 of a static file, whose length nginx removes, and for a 204 or 304 with a Content-Length, such as the 204 of return 204;, which nginx gives the length 0. A HEAD response with a known length through a body filter kept its connection since 2.7.5 (#752).
  • Now: after nginx has sent the header of a response that ends at it (HEAD, 204 or 304), ngx_mruby returns the result of sending it and passes no body on, as nginx's own handlers do when the header ends the response. nginx keeps an otherwise reusable connection for the next request. The status and the header that the client receives are the same, and no body bytes follow the header. GET responses, a result of NGX_ERROR from sending the header, and an error of the code of a filter keep their handling.
  • Why it changed:
    • The defect: nginx's header filter sets r->header_only when it sends the header of a HEAD, 204 or 304 response. The three places where ngx_mruby passes the header on (after the code of a header filter in a location without a body filter; in the body filter, for a response without a length; and after the code of a body filter, in ngx_mrb_finalize_body_filter) returned NGX_ERROR when that flag was set, although the header had been sent; the last place did so for every method but HEAD since 2.7.5. nginx's handlers, such as the static file handler, return and autoindex, return that result, so nginx ended the request as failed and closed the connection. The same defect was fixed on the development line of 3.0.0 by #848 and #849.
    • Why the old behavior could not be kept: a HEAD, 204 or 304 response that nginx sent successfully is not an output error, and closing the connection lost the next request that keep-alive announced. A header filter that adds a header to the responses of a location closed the connection after every conditional GET of a static file there that the client could answer from its cache.
    • What the documentation promised: the directives of 2.7.11 said nothing about HEAD, 204 or 304 responses in a location with an output filter. Nginx::Filter#body= said that a HEAD response with a known content length through the body filter keeps its connection, and that other header-only responses kept their behavior.
  • Affected: HEAD, 204 and 304 responses that pass an mruby output header filter or body filter, when the connection can otherwise be kept alive.
  • What to do: nothing. To close a connection deliberately, configure nginx's keep-alive policy (keepalive_timeout, keepalive_requests) instead of relying on this error.
  • Tests: port 18112 (test/conf/conf.d/20-filter-connection.conf, test/t/cases/head_header_filter.rb and test/t/cases/header_only_keepalive.rb).

2. A request header that Nginx::Headers_in#[]= replaces is sent to the upstream with the script's value, also when it was the last header of the request (#858)

  • Before: when a script replaced a request header with Nginx::Headers_in#[]= and the header that it replaced was the last header of the request (a request with Host alone, for example), then, depending on the configuration, proxy_pass and grpc_pass did not send the value of the script, and sent only the value of proxy_set_header (or grpc_set_header) or nginx's own value for that name. When the replaced header was followed by another header, or the script added a new name, the value of the script was sent as well, next to that of proxy_set_header or nginx, unless the worker process ended, which, depending on the configuration, could happen (see "Fixes").
  • Now: the value of the script is sent as well in every one of these cases, wherever the replaced header was in the request.
  • Why it changed:
    • The defect: a header that []= adds had no lowcase_key and no next set, and nginx read them when it sent the request with proxy_pass or grpc_pass (see "Fixes"). The value that nginx read decided whether the module left the script's header out.
    • Why the old behavior could not be kept: the fix sets both fields, and the value that it sets makes the result the same in every case: the 2.x line does not match a header that a script adds against the names of the headers that those modules set, which is what happened for every header but the replaced last one.
    • What the documentation promised: Nginx::Headers_in#[]= of 2.7.11 said that it sets the value of the request header, and nothing about proxy_pass or grpc_pass.
  • Affected: a location with proxy_pass or grpc_pass where a script replaces, with Nginx::Headers_in#[]=, a request header that proxy_set_header (or grpc_set_header) or the module itself also sets, such as Host, and the header is the last one of some requests. With Host, the upstream now receives two Host lines for such requests, as it already did for the other requests; nginx as the upstream answers 400 to them.
  • What to do: to send the value of the script alone, do not set the header with Nginx::Headers_in#[]=: put the value in a variable, for example with mruby_set_code $backend_host '...'; or with Nginx::Var on a variable that set declares, and give it to proxy_set_header Host $backend_host; (or grpc_set_header). For a name that the module does not set itself, removing its proxy_set_header also works.
  • Tests: ports 18099 and 18100 (test/conf/conf.d/74-headers-in-proxy.conf, test/t/cases/headers_in_proxy.rb): the replaced header followed by another header, as the last header and as the only header; Host replaced; the variable given to proxy_set_header.

更新前に確認する挙動の変更

1. mrubyの出力フィルタを通るHEAD、204、304のレスポンスの後も、接続を再利用できる(#853)

  • 変更前:ヘッダだけで終わるレスポンスがmrubyの出力フィルタを通ると、クライアントはConnection: keep-aliveの付いたヘッダを受け取り、その後にnginxが接続を閉じていました。こうしたレスポンスには、HEADのリクエストへのレスポンス、If-Modified-SinceかIf-None-Matchの付いたGETにnginxが返す304、return 204;のような204があります。クライアントがその接続で次に送ったリクエストには、レスポンスが返りませんでした。mruby_output_header_filterかmruby_output_header_filter_codeを持ち、mrubyのボディフィルタを持たないlocationでは、HEAD、204、304のすべてで起きていました。mruby_output_body_filterかmruby_output_body_filter_codeを持つlocationでは、autoindexの一覧や、Content-Lengthをnginxが取り除く静的なファイルの304のようにContent-Lengthの無いレスポンスで起きていました。nginxがContent-Lengthを0にするreturn 204;の204のように、Content-Lengthのある204と304でも起きていました。ボディフィルタを通る、長さの分かっているHEADのレスポンスの後は、2.7.5から接続を再利用できていました(#752)。
  • 変更後:HEADのリクエストへのレスポンスと、204と304のレスポンスは、ヘッダで終わります。このレスポンスのヘッダをnginxが送った後、ngx_mrubyは、ヘッダの送信の結果をそのまま返し、ボディを次のフィルタに渡しません。ヘッダでレスポンスが終わるときにnginx自身のハンドラが行うのと同じ扱いです。nginxは、再利用できる接続を次のリクエストのために開いておきます。クライアントが受け取るステータスとヘッダは変わらず、ヘッダの後にボディのバイトは続きません。次の3つは、これまでどおりに扱います。GETのレスポンス、ヘッダの送信の結果がNGX_ERRORの場合、フィルタのコードがエラーになった場合です。
  • 変更の理由:
    • 不具合:nginxのヘッダフィルタは、HEAD、204、304のレスポンスのヘッダを送るときにr->header_onlyを設定します。ngx_mrubyがヘッダを次のフィルタに渡す3か所は、ヘッダを送れていても、このフラグが立っているとNGX_ERRORを返していました。1つ目は、ボディフィルタの無いlocationで、ヘッダフィルタのコードを動かした後です。2つ目は、ボディフィルタが、長さの無いレスポンスを扱うときです。3つ目は、ボディフィルタのコードを動かした後のngx_mrb_finalize_body_filterで、2.7.5からはHEAD以外のメソッドでNGX_ERRORを返していました。静的なファイルのハンドラ、return、autoindexのようなnginxのハンドラは、そのNGX_ERRORをそのまま返すので、nginxはリクエストを失敗として終え、接続を閉じていました。3.0.0の開発ブランチでは、同じ不具合を#848と#849で直しています。
    • 前の挙動を残せなかった理由:nginxが正常に送ったHEAD、204、304のレスポンスは、出力のエラーではありません。nginxが接続を閉じていたので、keep-aliveで接続が続くと見込んでクライアントが送った次のリクエストが失われていました。locationのレスポンスにヘッダを足すヘッダフィルタがあると、そのlocationの静的なファイルへの条件付きGETのうち、クライアントがキャッシュを使えるものは、すべて接続を閉じられていました。
    • ドキュメントが示していた挙動:2.7.11のディレクティブのドキュメントは、出力フィルタのあるlocationでのHEAD、204、304のレスポンスについて、何も書いていませんでした。Nginx::Filter#body=は、ボディフィルタを通る長さの分かっているHEADのレスポンスの後は接続を使えること、ヘッダだけで終わるほかのレスポンスの挙動は変わらないことを書いていました。
  • 影響の範囲:mrubyの出力ヘッダフィルタか出力ボディフィルタを通るHEAD、204、304のレスポンスです。挙動が変わるのは、接続を再利用できる場合です。
  • 必要な対応:対応は不要です。接続を意図して閉じたいときは、これまでのように接続が閉じられることに頼らず、nginxのkeep-aliveの設定(keepalive_timeout、keepalive_requests)を使ってください。
  • テスト:ポート18112です(test/conf/conf.d/20-filter-connection.conf、test/t/cases/head_header_filter.rb、test/t/cases/header_only_keepalive.rb)。

2. Nginx::Headers_in#[]=で置き換えたリクエストヘッダは、リクエストの最後のヘッダだった場合も、スクリプトの値が中継先へ送られる(#858)

  • 変更前:スクリプトがNginx::Headers_in#[]=でリクエストヘッダを置き換え、置き換えたヘッダがリクエストの最後のヘッダだった場合(たとえばHostだけのリクエスト)、設定によっては、proxy_passとgrpc_passはスクリプトの値を送らず、その名前についてproxy_set_header(かgrpc_set_header)の値か、nginx自身の値だけを送りました。置き換えたヘッダの後ろにほかのヘッダがあった場合と、スクリプトが新しい名前を足した場合は、スクリプトの値も、proxy_set_headerかnginxの値と並んで送られていました。ただし、設定によっては、workerプロセスが終了することもありました(「不具合の修正」を参照)。
  • 変更後:これらのどの場合も、置き換えたヘッダがリクエストのどこにあったかによらず、スクリプトの値も送られます。
  • 変更の理由:
    • 不具合:[]=で足したヘッダにはlowcase_keyとnextが設定されておらず、proxy_passかgrpc_passでリクエストを送るときに、nginxがそれを読んでいました(「不具合の修正」を参照)。スクリプトのヘッダをモジュールが外すかどうかは、nginxが読んだ値で決まっていました。
    • 前の挙動を残せなかった理由:修正では2つのフィールドを設定し、その値によって、どの場合も同じ結果になります。2.xでは、スクリプトが足したヘッダを、これらのモジュールが設定するヘッダの名前と照らし合わせません。最後のヘッダを置き換えた場合を除けば、これまでもそうなっていました。
    • ドキュメントが示していた挙動:2.7.11のNginx::Headers_in#[]=は、リクエストヘッダの値を設定すると書いていました。proxy_passとgrpc_passについては、何も書いていませんでした。
  • 影響の範囲:proxy_passかgrpc_passを使うlocationで、proxy_set_header(かgrpc_set_header)かモジュール自身も設定するリクエストヘッダ(Hostなど)を、スクリプトがNginx::Headers_in#[]=で置き換えていて、そのヘッダが最後のヘッダになるリクエストがある場合です。Hostの場合は、そのようなリクエストでも、ほかのリクエストと同じく、中継先がHostの行を2つ受け取り、中継先がnginxなら400を返します。
  • 必要な対応:スクリプトの値だけを送るには、そのヘッダをNginx::Headers_in#[]=で設定せず、値を変数に入れてproxy_set_headerに渡してください。たとえばmruby_set_code $backend_host '...';か、setで宣言した変数へのNginx::Varの代入で値を入れ、proxy_set_header Host $backend_host;(かgrpc_set_header)と書きます。モジュール自身が設定しない名前なら、その名前のproxy_set_headerを消しても、スクリプトの値だけが送られます。
  • テスト:ポート18099と18100です(test/conf/conf.d/74-headers-in-proxy.conf、test/t/cases/headers_in_proxy.rb)。置き換えたヘッダの後ろにほかのヘッダがある場合、最後のヘッダの場合、ただ1つのヘッダの場合と、Hostを置き換える場合、変数をproxy_set_headerに渡す場合を確かめます。