Skip to content

v3.0.0-alpha.2

Pre-release
Pre-release

Choose a tag to compare

@matsumotory matsumotory released this 27 Nov 06:13
2d87575

What 3.0.0-alpha.2 brings

ngx_mruby 3.0 is the next major version. In addition to ordinary web services, it aims to run Ruby rules in front of AI applications, model APIs and MCP servers. This second pre-release changes how a Content-Length that a script sets is sent, and tests the handlers over HTTP/2 and HTTP/3 as well; the AI gateway features are still planned for the pre-releases after it ("What comes next" below).

  • One Content-Length that matches the body. Nginx::Headers_out#[]= with Content-Length sets nginx's own length. Before the headers go out, ngx_mruby compares it with the body that nginx sends: a value that differs is replaced by the body's length and logged at the warn level, and a response whose length is not known is sent chunked. #[] returns the value that the script set, or else the length that nginx has, and #delete removes the length. In 3.0.0-alpha.1 the script's value was sent as a second Content-Length line; the published advisory GHSA-2cf7-qh69-j29q has the details (#874, #876). The migration guide has examples.
  • Instructions per request as in 3.0.0-alpha.1. Before the tag, this release was compared with 3.0.0-alpha.1 under callgrind (#877). Every scenario changed by 0.2% or less: a handler that returns hello by +0.16% (16 instructions per request), a subrequest by +0.14% (60 instructions), and the proxied scenarios by -0.10% or less. These are instruction counts, not wall-clock times; #877 has the table.
  • Handlers tested over HTTP/2 and HTTP/3. The nginx of the tests is built with the HTTP/2 and HTTP/3 modules, and the suite sends the same requests over HTTP/1.1, HTTP/2 and HTTP/3 and expects the same responses from ngx_mruby's handlers, filters and SSL handshake handlers (#871). The tests send their requests from CRuby (#870).
  • Memory over time. The memory soak test also records the Pss and Private_Dirty of each worker, and fails when either grows faster than its threshold (#867).
  • Builds with the make of macOS. A build with GNU make 3.81, the make that macOS ships, links (#872, a fix planned for 2.7.15, which this pre-release already contains).
  • Try it locally. examples/start uses Docker Compose to run an nginx that routes requests by model to two mock APIs. No provider API key is needed.

What comes next

The next pre-releases are planned to add the parts of an AI gateway that 2.x does not have. None of them is in this pre-release.

  • A dictionary that all workers share, for budgets and rate-limit counters.
  • A filter that reads the server-sent events (SSE) of a streamed model response one event at a time, for example to count token usage.
  • Re-selecting the upstream, and a timeout for the first byte from the upstream, for model API fallback.
  • A reference agent proxy built on these parts.

Step 7 of the v3 plan lists the parts and their order. The uses they serve include MCP tool authorization, limits on bursts of agent calls, model API fallback and AI crawler control; the home page and the list of uses show the design status of each.

Behavior changes: read before upgrading

This section copies the entries of "Behavior changes" added or changed in meaning since 3.0.0-alpha.1: the new entry of #874, and the entry of #855, whose line "Not changed by this release" now points to it. The other changes of the section since 3.0.0-alpha.1 are the paths of links to proposals that moved to docs/proposals/archive/, and the wording of parenthesized clauses in the Japanese file (#864); they do not change what the entries say. The release notes of this tag have all 79 entries.

Content-Type, Location, Date and Server set through Nginx::Headers_out are nginx's own (#855)

  • Before: headers_out["Content-Type"] = value added a header line next to the Content-Type that nginx keeps for the response (the type of a static file, the Content-Type of a proxied response, or default_type), so the response could carry two Content-Type lines, and nginx's filters (charset, gzip_types, sub_filter_types and the others) went on with nginx's type. headers_out["Date"] = value added a second Date line next to nginx's. A Location line was invisible to nginx's header filter, so a value that starts with / was sent as it was. Only the name Server, spelled exactly so, replaced nginx's Server: server in lower case added a second line, and delete("Server") after setting it left the response without any Server line. headers_out[name] read only the lines that scripts and modules had added, so it returned nil for nginx's Content-Type of the response.
  • Now: these four names, in any case, are the headers that nginx keeps in fields of its own (r->headers_out.content_type, .location, .date and .server). Content-Type sets nginx's type of the response, which nginx's filters read and nginx's header filter sends in one line; it is not a line of headers_out.all. As for a Content-Type from an upstream, a charset= parameter is nginx's charset of the response, and nginx's filters (gzip_types, charset_types, sub_filter_types and the others) test the type before it: text/plain; charset=utf-8 matches gzip_types text/plain. Location, Date and Server add the line and point nginx's field at it, as nginx's own modules do, so the response has that line only. Like the Location of nginx's own redirects, a Location that starts with / is made absolute by nginx when absolute_redirect is on (the default): headers_out["Location"] = "/next" sends Location: http://<host>:<port>/next, after server_name_in_redirect and port_in_redirect. headers_out[name] returns nginx's Content-Type, and the Location, Date and Server that a script or a module (proxy_pass among them) set; nginx writes its own Date and Server into the response header without keeping them in these fields, so [] returns nil for them. delete(name) clears the header: nginx then sends its own Date and Server, and no Content-Type or Location. With an Array, every element is a value: Location, Date and Server get one line each, nginx's field points at the last one, and Content-Type takes the last element.
  • Why it changed:
    • The defect: Content-Type and Date are headers that a response carries once (RFC 9110, sections 8.3 and 6.6.1), and a response with two of them, or with a type that nginx's filters did not see, did not say what the script set. The design is section 2.2 of docs/proposals/archive/core-alpha1.md (2026-11-09).
    • Why the old behavior could not be kept: nginx and its modules read these headers from the fields, not from the lines of the list, so a line that a script added was either sent next to nginx's value or not seen by the module that should act on it.
    • What the documentation promised: Nginx::Headers_out of 2.7.11 said that []= sets the value of the response header, and said nothing about the headers that nginx sends itself.
  • Affected: Nginx::Headers_out#[]=, #[] and #delete with Content-Type, Location, Date or Server in any handler or filter that can change the response head, header filters on static and proxied responses included, and Nginx::Headers_out#all, which no longer lists a Content-Type that a script set. A script that set Content-Type "for show" now changes what charset, gzip and the other filters do with the response.
  • What to do: for a relative Location, set absolute_redirect off in the location. A script that removed the Server line by setting it and deleting it sets the value it wants sent instead. A script that read Content-Type from headers_out.all reads headers_out["Content-Type"].
  • Not changed by this release: the other headers that nginx keeps in fields (Last-Modified, ETag, Content-Encoding and the others) are lines as before, and Nginx::Request#content_type= is unchanged. Content-Length set through Nginx::Headers_out, which this change left a header line as in 2.x, is now one of nginx's own fields as well; see Content-Length set through Nginx::Headers_out is nginx's own length below.
  • Tests: port 18095: Content-Type in a content handler, read back and deleted, with a charset= parameter under gzip_types text/plain, and replaced by a header filter on a static file, which reads nginx's text/plain first; Date; Location relative and absolute with Nginx.return 302; Server in lower case, and set then deleted. The case /output_filter_builtin_header of test/t/ngx_mruby.rb passes unchanged.

Content-Length set through Nginx::Headers_out is nginx's own length (#874)

  • Before: Nginx::Headers_out#[]= with Content-Length added a response header line of the name next to the one nginx writes from the response body's length, so a response could carry two Content-Length lines with different values; #[] read only a line a script had added (nil otherwise), and #delete removed only such a line, never nginx's length.
  • Now: Content-Length is one of nginx's own response fields, as Content-Type, Location, Date and Server are. #[]= parses a non-negative decimal and sets that field; a value that is not a non-negative decimal, or an Array of more than one element, raises ArgumentError. Before the response headers go out, ngx_mruby reconciles the length a script set with the body nginx frames: a handler that writes output sends the output's length, a header or phase handler on a proxied or static response sends the length nginx has for it, and a value that differs from either is replaced by that length with a line in error.log at the warn level. When that length is not known (a streamed, close-delimited or compressed upstream), the response is sent chunked rather than with the script's value. #delete and #[]= nil remove the length, so the response is chunked (HTTP/1.1) or close-delimited (HTTP/1.0). #[] returns the value set, or the length nginx knows for the response as a String, or nil. A handler that writes no output and sets Content-Length sends Content-Length: 0, and a 204 or 304 sends no Content-Length. When an mruby output body filter runs on a response whose length is known, the response's Content-Length is the length of the body the filter produces, and the reconciliation above does not apply.
  • Why it changed:
    • The reason: v3 makes Content-Length behave as the ngx_lua and headers-more modules of OpenResty do, one line with the script's value in nginx's field, which operators coming from OpenResty expect, and removes a response that carries two Content-Length lines with different values, which nginx as a client answers 502 for and browsers reject (docs/proposals/archive/content-length.md). The published advisory is GHSA-2cf7-qh69-j29q.
    • Why the old behavior could not be kept: a second Content-Length line, and a value a script set that differs from the body, give the response no single body length; v3 sends the length nginx frames the body by, so the one Content-Length always matches the body.
    • What the documentation promised: docs/class_and_method/README.md said a Content-Length a script set "is sent as one more line"; that line is removed.
  • Affected: a script that sets, reads or deletes Content-Length through Nginx::Headers_out, in a content handler, a phase handler or an output header filter.
  • What to do: set Content-Length to the length of the body you send; a value that differs is replaced by the real length and logged. To send a response without a Content-Length (chunked), use #delete. A value that is not a non-negative decimal now raises, so convert it (for example the to_s of a non-negative Integer).

3.0.0-alpha.2で得られるもの

ngx_mruby 3.0は、次のメジャーバージョンです。通常のWebサービスに加えて、AIアプリケーション、モデルAPI、MCPサーバーの前でRubyのルールを動かすことを目指しています。この2つ目のプレリリースでは、スクリプトが設定したContent-Lengthの送り方を変え、ハンドラをHTTP/2とHTTP/3でもテストするようにしました。AIゲートウェイの機能は、引き続きこの後のプレリリースで足す予定です。下の「これから入るもの」を見てください。

  • Content-Lengthが1行になり、本文と一致します。 Nginx::Headers_out#[]=で設定したContent-Lengthは、nginxが持つ長さのフィールドに入ります。ヘッダを送る前に、ngx_mrubyはその値を、nginxが送る本文と照合します。違う値は本文の長さに置き換え、warnのレベルでログに出します。長さが分からないレスポンスは、chunkedで送ります。#[]は、スクリプトが設定した値を返し、設定が無ければnginxが持つ長さを返します。#deleteは長さを外します。3.0.0-alpha.1では、スクリプトの値が2行目のContent-Lengthとして送られていました。詳しくは、公開済みのアドバイザリGHSA-2cf7-qh69-j29qにあります(#874、#876)。移行の手引きに例があります。
  • リクエストあたりの命令数は、3.0.0-alpha.1とほぼ同じです。 タグを付ける前の確認で、このリリースと3.0.0-alpha.1をcallgrindで比べました(#877)。どのシナリオも、変化は0.2%以下でした。helloを返すハンドラで0.16%(1リクエストあたり16命令)、サブリクエストで0.14%(60命令)増え、proxyのシナリオは0.10%以下の範囲で減りました。これは命令数で、実行時間ではありません。表は#877にあります。
  • ハンドラをHTTP/2とHTTP/3でもテストします。 テストのnginxをHTTP/2とHTTP/3のモジュール付きでビルドし、HTTP/1.1、HTTP/2、HTTP/3で同じリクエストを送ります。どのプロトコルでも、ngx_mrubyのハンドラ、フィルタ、SSLハンドシェイクのハンドラが同じレスポンスを返すことを確かめます(#871)。テストのリクエストは、CRubyから送ります(#870)。
  • 長く動かしたときのメモリの増え方を測ります。 メモリのソークテストは、各workerのPssとPrivate_Dirtyも記録します。どちらかの増え方が閾値を超えると、テストは失敗します(#867)。
  • macOSのmakeでビルドできます。 macOSに入っているGNU make 3.81でビルドしても、リンクが通るようになりました(#872。2.7.15で出す予定の修正で、このプレリリースにも入っています)。
  • 手元で試せます。 examples/startは、Docker Composeで、リクエストをモデルごとに2つのモックAPIへ振り分けるnginxを動かします。プロバイダのAPIキーは不要です。

これから入るもの

この後のプレリリースでは、2.xに無いAIゲートウェイの機能を足す予定です。どれも、このプレリリースにはまだありません。

  • すべてのworkerから使える共有辞書を足します。予算やレート制限のカウンターに使います。
  • ストリーミングで返るモデルの応答から、SSE(server-sent events)のイベントを1つずつ読むフィルタを足します。トークンの使用量を数えるときなどに使います。
  • 中継先を選び直す仕組みと、upstreamから最初のバイトを受け取るまでのタイムアウトを足します。モデルAPIのフォールバックに使います。
  • これらの機能で作る、リファレンス実装のエージェントプロキシを足します。

機能とその順序は、v3の計画のステップ7にあります。これらの機能を使う用途には、MCPのツール呼び出しの認可、エージェントが短時間に繰り返す呼び出しの制限、モデルAPIのフォールバック、AIクローラーの制御などがあります。用途ごとの設計の状態は、トップページと用途の一覧にあります。

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

3.0.0-alpha.1から追加された項目と、内容が変わった項目を写します。#874の項目は新しく追加されました。#855の項目は、「このリリースで変わらない挙動」の行が#874の項目を指すようになりました。それ以外の変更は、docs/proposals/archive/に移った提案書へのリンクのパスと、和文の括弧書きの言い回し(#864)だけで、項目の内容は変わりません。全79項目は、このタグのリリースノートにあります。

Nginx::Headers_outで設定したContent-Type、Location、Date、Serverを、nginxが自分のヘッダとして扱う(#855)

  • 変更前:headers_out["Content-Type"] = valueは、nginxがレスポンスについて持つContent-Typeとは別に、ヘッダの行を足しました。nginxが持つContent-Typeは、静的なファイルの種類、中継したレスポンスのContent-Type、default_typeのいずれかです。そのため、レスポンスがContent-Typeの行を2つ持つことがあり、charset、gzip_types、sub_filter_typesなどのnginxのフィルタは、nginxの種類のまま動きました。headers_out["Date"] = valueは、nginxのDateの隣に2つ目のDateの行を足しました。Locationの行はnginxのヘッダフィルタから見えず、/で始まる値はそのまま送られました。nginxのServerを置き換えたのは、Serverと正確に書いた名前だけでした。小文字のserverは2つ目の行を足し、Serverを設定した後にdelete("Server")を呼ぶと、レスポンスからServerの行がなくなりました。headers_out[name]は、スクリプトとモジュールが足した行だけを読んだので、nginxが持つレスポンスのContent-Typeにはnilを返しました。
  • 変更後:この4つの名前は、大文字と小文字を区別せずに、nginxが自分のフィールド(r->headers_out.content_type、.location、.date、.server)で持つヘッダとして扱います。Content-Typeは、nginxが持つレスポンスの種類を設定します。nginxのフィルタはこの値を読み、nginxのヘッダフィルタが1行で送ります。headers_out.allの行には入りません。中継先から受けたContent-Typeと同じく、charset=の引数はnginxが持つレスポンスの文字コードになり、gzip_types、charset_types、sub_filter_typesなどのnginxのフィルタは、引数の前の種類で判定します。たとえばtext/plain; charset=utf-8はgzip_types text/plainに一致します。Location、Date、Serverは、nginxのモジュールと同じように、行を足してnginxのフィールドをその行に向けるので、レスポンスはその行だけを持ちます。nginx自身のリダイレクトのLocationと同じく、/で始まるLocationは、absolute_redirectが既定のonのときにnginxが絶対URLに書き換えます。headers_out["Location"] = "/next"は、server_name_in_redirectとport_in_redirectに従って、Location: http://<host>:<port>/nextを送ります。headers_out[name]は、nginxが持つContent-Typeと、スクリプトかモジュールが設定したLocation、Date、Serverを返します。このモジュールには、proxy_passで中継するproxyモジュールも含まれます。nginxは自分のDateとServerを、これらのフィールドに持たずにレスポンスのヘッダへ書くので、[]はそれらにnilを返します。delete(name)はそのヘッダを消します。消した後、nginxは自分のDateとServerを送り、Content-TypeとLocationは送りません。値が配列のときは、要素をそれぞれ値として扱います。Location、Date、Serverは要素ごとに1行になり、nginxのフィールドは最後の行を指します。Content-Typeは最後の要素を使います。
  • 変更の理由:
    • 不具合:RFC 9110の8.3節と6.6.1節のとおり、Content-TypeとDateは、レスポンスが1つだけ持つヘッダです。2つ持つレスポンスや、nginxのフィルタが知らない種類を持つレスポンスは、スクリプトが設定した内容を伝えていませんでした。設計はdocs/proposals/archive/core-alpha1.mdの2.2節(2026-11-09)にあります。
    • 前の挙動を残せなかった理由:nginxとそのモジュールは、これらのヘッダをリストの行ではなくフィールドから読みます。スクリプトが足した行は、nginxの値と並んで送られるか、その行を扱うはずのモジュールから見えないかのどちらかでした。
    • ドキュメントが示していた挙動:2.7.11のNginx::Headers_outは、[]=がレスポンスヘッダの値を設定すると書いていました。nginxが自分で送るヘッダについては、何も書いていませんでした。
  • 影響の範囲:レスポンスのヘッダを変えられるハンドラとフィルタで、Content-Type、Location、Date、Serverに対して呼ぶNginx::Headers_out#[]=、#[]、#deleteです。静的なファイルと中継したレスポンスのヘッダフィルタも含みます。スクリプトが設定したContent-Typeを一覧に含めなくなったNginx::Headers_out#allも対象です。見た目のためだけにContent-Typeを設定していたスクリプトは、charsetやgzipなどのフィルタがそのレスポンスをどう扱うかも変えます。
  • 必要な対応:相対のLocationを送るには、そのlocationでabsolute_redirect offを設定してください。Serverを設定してから削除してServerの行を消していたスクリプトは、送りたい値を設定してください。Content-Typeをheaders_out.allから読んでいたスクリプトは、headers_out["Content-Type"]で読んでください。
  • このリリースで変わらない挙動:Last-Modified、ETag、Content-Encodingなど、nginxがフィールドで持つほかのヘッダは、これまでどおり行として扱い、Nginx::Request#content_type=も変わりません。Nginx::Headers_outで設定したContent-Lengthは、この変更では2.xと同じくヘッダの行のままでしたが、いまはnginx自身のフィールドになりました。下の「Nginx::Headers_outで設定したContent-Lengthが、nginx自身の長さになる」を参照してください。
  • テスト:ポート18095。コンテンツハンドラで設定したContent-Typeを読み戻す場合と削除する場合、gzip_types text/plainの下でcharset=の引数を付けた場合、静的なファイルのヘッダフィルタがnginxのtext/plainを読んでから置き換える場合、Date、Nginx.return 302と組み合わせた相対と絶対のLocation、小文字のserverと、設定してから削除したServerを確かめます。test/t/ngx_mruby.rbの/output_filter_builtin_headerのケースは、変えずに通ります。

Nginx::Headers_outで設定したContent-Lengthが、nginx自身の長さになる(#874)

  • 変更前:Nginx::Headers_out#[]=のキーにContent-Lengthを指定すると、nginxが応答の本文の長さをもとに出力する行とは別に、その名前の応答ヘッダ行が追加されていました。そのため、値の異なる2つのContent-Lengthの行を持つ応答になることがありました。#[]はスクリプトが追加した行だけを読み取り(無ければnil)、#deleteはその行だけを削除し、nginxが持つ長さは削除しませんでした。
  • 変更後:Content-Lengthは、Content-Type・Location・Date・Serverと同じく、nginx自身の応答のフィールドになりました。#[]=は非負の十進をパースしてそのフィールドを設定します。非負の十進でない値や、要素が2つ以上のArrayはArgumentErrorになります。応答のヘッダが出る前に、スクリプトが設定した長さを、nginxが本文を区切る長さと照合します。出力を書き込むハンドラでは出力の長さを、proxyや静的ファイルの応答に対するヘッダフィルタやフェーズのハンドラでは、nginxがその応答について把握している長さを送ります。いずれとも異なる値は、その長さに置き換え、error.logにwarnの行を出力します。長さが分からないとき(ストリーム・close区切り・圧縮のupstream)は、スクリプトの値ではなくchunkedで送ります。#deleteと#[]= nilは長さを外すので、応答はchunked(HTTP/1.1)かclose区切り(HTTP/1.0)になります。#[]は、設定した値、またはnginxがその応答に持つ長さを文字列で、無ければnilを返します。出力を書かずContent-Lengthを設定したハンドラはContent-Length: 0を送り、204と304はContent-Lengthを送りません。長さの分かっている応答でmrubyの出力ボディフィルタが動くときは、応答のContent-Lengthはフィルタが作ったボディの長さになり、上記の照合は行いません。
  • 変更の理由:
    • 背景:v3では、Content-LengthをOpenRestyのngx_luaやheaders-moreと同じ挙動(1行で、スクリプトの値をnginxのフィールドに置く)にします。OpenRestyから来た運用者はこれを期待します。あわせて、値の異なる2つのContent-Lengthの行を持つ応答をなくします。このような応答に対して、nginxはクライアントとして502を返し、ブラウザも拒否します(docs/proposals/archive/content-length.md)。公開済みのアドバイザリはGHSA-2cf7-qh69-j29qです。
    • 前の挙動を残せなかった理由:2行目のContent-Lengthや、スクリプトが設定したが本文と違う値は、応答の本文の長さを1つに定めません。v3はnginxが本文を区切る長さを送るので、1つのContent-Lengthが常に本文と一致します。
    • ドキュメントが示していた挙動:docs/class_and_method/README.mdは、スクリプトが設定したContent-Lengthは「もう1行として送られる」と書いていました。その記述を削除します。
  • 影響の範囲:contentハンドラ、フェーズのハンドラ、出力ヘッダフィルタのいずれかで、Nginx::Headers_outを通じてContent-Lengthを設定・読み取り・削除するスクリプト。
  • 必要な対応:Content-Lengthは、送る本文の長さに設定してください。違う値は実際の長さに置き換えられ、ログに出ます。Content-Lengthを付けない(chunkedの)応答には#deleteを使ってください。非負の十進でない値は例外になるので、非負のIntegerのto_sなどに直してください。