Repository navigation
v3.0.0-alpha.1
Pre-releaseWhat 3.0.0-alpha.1 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 first pre-release brings the new core; the AI gateway features are planned for the pre-releases after it ("What comes next" below).
- Fewer instructions per request. Before the tag, this release was compared with 2.7.13 under callgrind (#862). The total instructions per request fell in 15 of the 16 measured scenarios: by 18% in the scenario that reads and sets headers, by 11% for a subrequest and by 7% for a handler that returns hello. One scenario, an output body filter, rose by 2.7%. These are instruction counts, not wall-clock times; #862 has the table, including the figures that the check gates on.
- Bundles mruby 4.1.0-rc2 instead of 3.3.0: scripts can use pattern matching (
case/in),itin blocks, and the methods added in mruby 3.4 through 4.1. - One defined behavior for handlers that wait. A handler that waits in
Nginx::Async.sleeporNginx::Async::HTTP.sub_requestresumes and ends the request in the same way in every phase.sub_requestworks in content handlers and returns the response of the subrequest (#712, #714, #715, #720, #722, #727, #728, #735). - Mistakes show up. A method that would have no effect in the handler that calls it now raises a
RuntimeError, so the mistake reacheserror.loginstead of being ignored (#826). - Values passed between handlers.
Nginx::Request#storepasses values between the handlers of one request (#832), andmruby_variabledeclares variables for scripts to assign (#780). - Headers in nginx's own fields.
Content-Type,Location,DateandServerset throughNginx::Headers_outare stored in nginx's own fields, so nginx's filters use them, and request headers added withNginx::Headers_in#[]=are recognized byproxy_pass(#855). - Try it locally.
examples/startuses 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 first-byte timeout, 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 is the first pre-release of 3.0.0, so the section lists every behavior change since 2.7.13. It has 78 entries; each heading below links to its full text (before, now, why it changed, what is affected, what to do) in the release notes of this tag.
- The local start example binds to loopback and adds mock model routing (#672)
- The bundled mruby is 4.1.0-rc2 instead of 3.3.0 (#561, #678)
- A
build_config.rbof your own needs the core gem changes of mruby 4.1 (#561, #678) - Building the bundled mruby needs Ruby 3.0 or later (#678)
- The third-party mrbgems are built at the commits that
build_config.rb.lockrecords (#558) Hash#inspectandHash#to_suse a new format (#561)Exception#inspect, and so themrb_run failedlines oferror.log, use a new format (#561)- The messages logged while nginx reads the configuration are at the
infolevel instead ofnotice(#565) Nginx.module_versionand themechanism enabledline give 3.0.0 (#823)- The message of
NoMethodErrornames the class of the receiver (#561) privateandprotectedare enforced (#561, #678)Module#remove_const,#private,#public,#protected,#module_functionand the hooks are private (#561)- Overrides of
append_features,prepend_featuresandextend_objectare not called (#561) allbits?,anybits?andnobits?are methods ofIntegeronly (#561)FileTestis a module (#561)- Assigning a constant in a method body is a
SyntaxError(#561) "#@ivar","#@@cvar"and"#$global"in a string interpolate the variable (#678)- Syntax errors have the messages of Prism (#678)
- An
Array, aHashor aStructthat holds itself can be compared (#678) - Float values have no bit operations (#561)
String#valid_encoding?is not in the default build (#561)- A seeded
Randomgives other numbers (#561) Set#inspectandSet#to_suse a new format, andSet#flatten_mergeis removed (#561)Hash#default_proc=raisesTypeErrorfor an object that is not a Proc (#561)yieldwith keyword arguments to a block with keyword parameters works (#561)- A
returninside a block or aforbody of handler code ends the code (#561) - A content handler that ends without output answers 200 with an empty body instead of 500 (#574)
- An output body filter that ends with an empty body sends the response instead of closing the connection (#574)
- The accessors of
Nginx::Varbecome methods ofNginx::Varat their first use in a worker (#610) Nginx::Connection,Nginx::ServerandNginx::Requestread their variables without callingNginx::Var(#610)Dircomes from mruby's core gem, and a closedDirraisesIOErrorinstead ofRuntimeError(#559)ENVcomes from mruby's core gem:ENV.to_hashis gone, andENVisEnumerable(#766)Processcomes from mruby's core gem:Process.fork,Kernel#forkandKernel#systemare gone (#766)exitin a handler raisesSystemExitinstead of ending the worker process (#766)sleepwithout an argument raisesArgumentError(#766)- The default build leaves out
VedisandCache(mruby-vedis and mruby-localmemcache) (#766) DigestandSecureRandomcome from gems of ngx_mruby that need OpenSSL 3.0 or later (#771)- nginx 1.30 (stable) and 1.31 (mainline) are the oldest supported versions (#789)
- C extensions: an object of a class not declared
MRB_TT_DATAcannot wrap C data (#561) - C extensions: mruby checks the argument count of a C method against its spec (#561)
- C extensions: renamed headers and functions, and
mrb_open()on failure (#561) - C extensions: sources that include
mruby.hneed the generated headers (#561) - The default build leaves out the auto-ssl mrbgem and the classes that came with it (#556)
- The auto-ssl mrbgem and
NGX_MRUBY_AUTO_SSLare removed (#640) Nginx::Upstream#server=andNginx::Stream::Connection#upstream_server=raiseArgumentErrorfor a string that cannot be a server (#680)Nginx::Upstream#server=raisesArgumentErrorfor a host name instead of resolving it (#680)- An access or post_read handler that waits in
Nginx::Async.sleepends as without the wait (#715) - An access or post_read handler that writes output ends the request after the response (#715)
- A handler that sets no result does not take the result of an earlier handler (#715)
- An
mruby_sethandler that waits inNginx::Async.sleepsets its variable, and the request goes on (#715) - A rewrite or server rewrite handler that ends without a result goes on to the next phase handler (#720)
Nginx::Async::HTTP.sub_requestworks in a content handler, also afterNginx::Async.sleep(#722)- A handler that waits for
Nginx::Async::HTTP.sub_requestruns its code once and ends as without the wait (#722) - A UDP stream session runs its code once: datagrams that arrive while the code waits do not start it again (#727)
- A stream session that waits when
worker_shutdown_timeoutexpires ends then (#727) Nginx.redirectwith a local URI takes effect when the handler ends (#728)Nginx.rputs,Nginx.echo,Nginx.redirectandNginx::Filter#body=raiseTypeErrorwhento_sof the argument does not return a String (#776)Nginx::Async::HTTP.sub_requestraises aRuntimeErrorin anmruby_sethandler (#735)- An
mruby_setvalue that cannot be converted to a String answers 500 (#735) access_logwrites the variables that mruby log handlers set, and the status that was sent (#770)Nginx.redirectwith a local URI raises aRuntimeErrorin a log handler (#770)- Output is sent with the status that the handler set, and
error_pagedoes not apply to it (#779, #783) - A stream session or stream hook that raises logs the backtrace of the exception in a build with
--with-debug(#796) - An exception whose
inspectraises or does not return a String is logged with the name of its class (#799) - Methods that have no effect in a handler raise a
RuntimeErrorthere (#826) Nginx.redirectwith a local URI raises aRuntimeErrorinmruby_sethandlers and output filters (#833)- Assigning a variable that nginx does not know, or does not let scripts change, raises (#854)
nginx -trefuses a stream hook given twice in one context (#834)- Output filters given at
httporserverlevel, or in a location, run in the locations below them (#835) - A location with an mruby header filter and no body filter does not read files into memory (#847)
- The measurement builds include stream sessions (#758)
- A HEAD response through an mruby header filter alone, or through a body filter without
Content-Length, keeps its connection available (#848) - A 204 or 304 response through an mruby output filter keeps its connection available (#849)
- The integration test build enables
Nginx::Debug(#751) Nginx::Headers_in#deleteandNginx::Headers_out#deleteremove every header of the name (#855)Content-Type,Location,DateandServerset throughNginx::Headers_outare nginx's own (#855)- A header name can be a Symbol, and a value that is not a String is converted with
to_s(#855) - A request header that
Nginx::Headers_in#[]=adds is one thatproxy_passrecognizes (#855)
3.0.0-alpha.1で得られるもの
ngx_mruby 3.0は、次のメジャーバージョンです。通常のWebサービスに加えて、AIアプリケーション、モデルAPI、MCPサーバーの前でRubyのルールを動かすことを目指しています。この最初のプレリリースには、新しいコアが入っています。AIゲートウェイの機能は、この後のプレリリースで足す予定です。下の「これから入るもの」を見てください。
- リクエストあたりの命令数が減りました。 タグを付ける前の確認で、このリリースと2.7.13をcallgrindで比べました(#862)。測った16のシナリオのうち15で、リクエストあたりの命令数の合計が減りました。ヘッダを読んで設定するシナリオで18%、サブリクエストで11%、helloを返すハンドラで7%減っています。出力ボディフィルターのシナリオだけは、2.7%増えました。これは命令数で、実行時間ではありません。確認で合否の判定に使う値を含む表は、#862にあります。
- mruby 4.1.0-rc2を同梱します。 同梱のmrubyが3.3.0から上がったので、スクリプトでパターンマッチ(
case/in)、ブロックのit、mruby 3.4から4.1で増えたメソッドを使えます。 - 待つハンドラの動き方が1つに定まりました。
Nginx::Async.sleepやNginx::Async::HTTP.sub_requestで待ったハンドラは、どのフェーズでも同じ形で再開し、リクエストを終えます。sub_requestはcontentハンドラでも使え、サブリクエストの応答を返します(#712、#714、#715、#720、#722、#727、#728、#735)。 - 誤りが見えるようになりました。 呼んだハンドラで効果の無いメソッドは、
RuntimeErrorを発生させます。誤りは無視されずに、error.logに出ます(#826)。 - ハンドラの間で値を渡せます。
Nginx::Request#storeで、1つのリクエストのハンドラの間で値を渡せます(#832)。mruby_variableで、スクリプトが代入する変数を宣言できます(#780)。 - ヘッダがnginxのフィールドに入ります。
Nginx::Headers_outで設定したContent-Type、Location、Date、Serverは、nginxが持つフィールドに入り、nginxのフィルターもその値を使います。Nginx::Headers_in#[]=で足したリクエストヘッダは、proxy_passが認識します(#855)。 - 手元で試せます。
examples/startは、Docker Composeで、リクエストをモデルごとに2つのモックAPIへ振り分けるnginxを動かします。プロバイダのAPIキーは不要です。
これから入るもの
この後のプレリリースでは、2.xに無いAIゲートウェイの部品を足す予定です。どれも、このプレリリースにはまだありません。
- すべてのworkerが共有する辞書を足します。予算やレート制限のカウンターに使います。
- ストリーミングで返るモデルの応答から、SSE(server-sent events)のイベントを1つずつ読むフィルターを足します。トークンの使用量を数えるときなどに使います。
- 中継先を選び直す仕組みと、最初のバイトまでのタイムアウトを足します。モデルAPIのフォールバックに使います。
- これらの部品で作る、リファレンス実装のエージェントプロキシを足します。
部品とその順序は、v3の計画のステップ7にあります。部品が支える用途には、MCPのツール呼び出しの認可、エージェントが短時間に繰り返す呼び出しの制限、モデルAPIのフォールバック、AIクローラーの制御などがあります。用途ごとの設計の状態は、トップページと用途の一覧にあります。
更新前に確認する挙動の変更
3.0.0の最初のプレリリースなので、2.7.13からの挙動の変更をすべて挙げます。項目は78個あります。各見出しは、このタグのリリースノートにある項目の全文へのリンクです。全文には、変更前、変更後、変更の理由、影響の範囲、必要な対応を書いています。
- ローカルの導入例をループバックに限定し、mockのモデル振り分けを加える(#672)
- 同梱のmrubyが3.3.0から4.1.0-rc2になる(#561, #678)
- 自前の
build_config.rbは、mruby 4.1のコアgemの変更に合わせる必要がある(#561, #678) - 同梱のmrubyのビルドにRuby 3.0以降が要る(#678)
- サードパーティのmrbgemを
build_config.rb.lockが記録するコミットでビルドする(#558) Hash#inspectとHash#to_sの形式が変わる(#561)Exception#inspectの形式が変わり、error.logのmrb_run failedの行も変わる(#561)- nginxが設定を読む間のメッセージのレベルが
noticeからinfoになる(#565) Nginx.module_versionとmechanism enabledの行が3.0.0を示す(#823)NoMethodErrorのメッセージがレシーバのクラスを示す(#561)privateとprotectedの制限が効く(#561, #678)Module#remove_const、#private、#public、#protected、#module_functionとフックがprivateになる(#561)append_features、prepend_features、extend_objectのオーバーライドが呼ばれなくなる(#561)allbits?、anybits?、nobits?がIntegerだけのメソッドになる(#561)FileTestがモジュールになる(#561)- メソッドの本体で定数に代入すると
SyntaxErrorになる(#561) - 文字列の中の
"#@ivar"、"#@@cvar"、"#$global"が変数を展開する(#678) - 構文エラーのメッセージがPrismのものになる(#678)
- 自分自身を持つ
Array、Hash、Structを比べられる(#678) - Floatの値にビット演算が無くなる(#561)
String#valid_encoding?が既定のビルドに無くなる(#561)- シードを与えた
Randomが別の数を返す(#561) Set#inspectとSet#to_sの形式が変わり、Set#flatten_mergeが無くなる(#561)Hash#default_proc=がProcでないオブジェクトにTypeErrorを出す(#561)- キーワード引数を受けるブロックに、キーワード引数を付けて
yieldすると動く(#561) - ハンドラのコードで、ブロックや
forの本体の中のreturnがコードを終える(#561) - 出力なしで終わるcontentハンドラが、500でなく、空のボディの200で応答する(#574)
- 空のボディで終わる出力のボディフィルタが、接続を閉じずにレスポンスを送る(#574)
Nginx::Varのアクセサが、ワーカーで最初に使われたときにNginx::Varのメソッドになる(#610)Nginx::Connection、Nginx::Server、Nginx::Requestが、Nginx::Varを呼ばずに変数を読む(#610)Dirをmrubyのコアgemが定義するようになり、閉じたDirがRuntimeErrorでなくIOErrorを出す(#559)ENVをmrubyのコアgemが定義するようになり、ENV.to_hashが無くなり、ENVがEnumerableになる(#766)Processをmrubyのコアgemが定義するようになり、Process.fork、Kernel#fork、Kernel#systemが無くなる(#766)- ハンドラの中の
exitが、ワーカープロセスを終わらせずにSystemExitを出す(#766) - 引数なしの
sleepがArgumentErrorを出す(#766) - 既定のビルドが、mruby-vedisの
Vedisとmruby-localmemcacheのCacheを含まなくなる(#766) DigestとSecureRandomを、OpenSSL 3.0以降が要るngx_mrubyのgemが定義する(#771)- nginx 1.30のstableと1.31のmainlineが、サポートする最も古いバージョンになる(#789)
- C拡張:
MRB_TT_DATAと宣言していないクラスのオブジェクトでは、Cのデータをラップできない(#561) - C拡張:mrubyが、Cのメソッドに渡す引数の数を引数仕様で検査する(#561)
- C拡張:ヘッダと関数の名前が変わり、失敗したときの
mrb_open()の戻り値が変わる(#561) - C拡張:
mruby.hをインクルードするソースに、生成されたヘッダが必要になる(#561) - 既定のビルドからauto-sslのmrbgemと、それに伴うクラスが無くなる(#556)
- auto-sslのmrbgemと
NGX_MRUBY_AUTO_SSLが無くなる(#640) Nginx::Upstream#server=とNginx::Stream::Connection#upstream_server=が、serverにできない文字列にArgumentErrorを出す(#680)Nginx::Upstream#server=が、ホスト名を名前解決せずにArgumentErrorを出す(#680)Nginx::Async.sleepで待つaccessハンドラとpost_readハンドラが、待たないときと同じように終わる(#715)- 出力を書くaccessハンドラとpost_readハンドラが、レスポンスの後にリクエストを終える(#715)
- 結果を設定しないハンドラが、前のハンドラの結果を引き継がない(#715)
Nginx::Async.sleepで待つmruby_setのハンドラが変数を設定し、リクエストが先へ進む(#715)- 結果なしで終わるrewriteハンドラとserver rewriteハンドラが、次のフェーズのハンドラへ進む(#720)
Nginx::Async::HTTP.sub_requestがcontentハンドラで、Nginx::Async.sleepの後でも動く(#722)Nginx::Async::HTTP.sub_requestで待つハンドラが、コードを1回だけ実行し、待たないときと同じように終わる(#722)- UDPのstreamのセッションがコードを1回だけ実行し、コードが待っている間に届いたデータグラムでは始め直さない(#727)
worker_shutdown_timeoutが切れたときに待っているstreamのセッションが、そこで終わる(#727)- ローカルのURIへの
Nginx.redirectが、ハンドラが終わったときに効く(#728) - 引数の
to_sがString以外を返すと、Nginx.rputs、Nginx.echo、Nginx.redirect、Nginx::Filter#body=がTypeErrorを出す(#776) mruby_setのハンドラではNginx::Async::HTTP.sub_requestがRuntimeErrorを出す(#735)mruby_setの値をStringに変換できないと500で応答する(#735)access_logが、mrubyのlogハンドラが設定した変数と、送ったステータスを書く(#770)- logハンドラでは、ローカルのURIへの
Nginx.redirectがRuntimeErrorを出す(#770) - ハンドラが設定したステータスで出力を送り、
error_pageは効かない(#779, #783) --with-debugのビルドでは、例外を出したstreamのセッションとフックも、例外のbacktraceを書く(#796)inspectが例外を出すかString以外を返す例外は、クラス名でerror.logに書く(#799)- ハンドラで効果の無いメソッドは、そのハンドラで
RuntimeErrorを出す(#826) mruby_setのハンドラと出力フィルタでは、ローカルのURIへのNginx.redirectがRuntimeErrorを出す(#833)- nginxが知らない変数と、スクリプトからの変更を許さない変数への代入が例外を出す(#854)
- 1つのコンテキストに2回書いたstreamのフックを、
nginx -tが断る(#834) httpかserverのレベル、またはlocationに書いた出力フィルタが、その下のlocationで動く(#835)- mrubyのヘッダフィルタだけを持ち、ボディフィルタを持たないlocationが、ファイルをメモリに読み込まない(#847)
- 測定用のビルドがstreamのセッションを含む(#758)
- mrubyのヘッダフィルタだけを通るHEADのレスポンスの後と、
Content-Lengthの無いレスポンスでmrubyのボディフィルタを通るHEADのレスポンスの後も、接続を使える(#848) - mrubyの出力フィルタを通る204と304のレスポンスの後も、接続を使える(#849)
- 結合テストのビルドが
Nginx::Debugを有効にする(#751) Nginx::Headers_in#deleteとNginx::Headers_out#deleteが、その名前のヘッダをすべて取り除く(#855)Nginx::Headers_outで設定したContent-Type、Location、Date、Serverを、nginxが自分のヘッダとして扱う(#855)- ヘッダの名前にシンボルを使え、文字列でない値を
to_sで変換する(#855) Nginx::Headers_in#[]=で足したリクエストヘッダを、proxy_passが見分ける(#855)