2026年3月2日月曜日

Autonomous AI Database向けNGINXリバース・プロキシの作成

Autonomous AI Database向けのnginxを使ったリバース・プロキシの作成作業を記述します。Always Freeのインスタンスをバックエンドのサーバーとして使用します。

Always Freeのインスタンスはプライベート・エンドポイントでは構成できないため、APEXにアクセスするリバース・プロキシは構成できません。リバース・プロキシの対象は、リモートMCPサーバーを実装しているORDSのRESTサービスに限定します。

以下の記事より、APEXの構成を除いた手順になります。


コンピュート・インスタンスの準備



Oracle Cloudでコンピュート・インスタンスを作成します。

シェイプにAlways FreeのVM.Standard.E2.1.Microを選択します。実行するのはnginxだけなので、検証用としては使用できるでしょう。


イメージにはパッケージが少なそうな、Ubuntu 24.04 Minimalを選択します。


この後は通常の手順で、コンピュート・インスタンスを作成します。


リバース・プロキシの構成



インスタンスが作成できたら、ユーザーubuntuでインスタンスにSSH接続します。

接続した後に以下のスクリプトを実行して、nginxのリバース・プロキシ向けに環境を構成します。


ubuntu@ordsmcp2:~$ sh prepare-ubuntu-for-nginx-reverse-proxy.sh 

37 packages can be upgraded. Run 'apt list --upgradable' to see them.

The following NEW packages will be installed:

  hwdata libnuma1 libpython3.12t64 libslang2 libtraceevent1

  linux-headers-6.17.0-1007-oracle linux-image-6.17.0-1007-oracle

  linux-modules-6.17.0-1007-oracle linux-modules-extra-6.17.0-1007-oracle

  linux-oracle-6.17-headers-6.17.0-1007 linux-oracle-6.17-tools-6.17.0-1007

  linux-tools-6.17.0-1007-oracle linux-tools-common wireless-regdb

The following packages will be upgraded:

  base-files cloud-init curl fwupd gcc-14-base gir1.2-glib-2.0 initramfs-tools

  initramfs-tools-bin initramfs-tools-core libc-bin libc6 libcurl3t64-gnutls

  libcurl4t64 libdrm-common libdrm2 libexpat1 libfwupd2 libgcc-s1

  libglib2.0-0t64 libgnutls30t64 libldap2 libpng16-16t64 libpython3.12-minimal

  libpython3.12-stdlib libssh-4 libssl3t64 libstdc++6 linux-headers-oracle

  linux-image-oracle linux-oracle openssl python-apt-common python3-apt

  python3-distupgrade python3.12 python3.12-minimal

  ubuntu-release-upgrader-core

37 upgraded, 14 newly installed, 0 to remove and 0 not upgraded.

23 standard LTS security updates

Need to get 228 MB of archives.

After this operation, 331 MB of additional disk space will be used.

debconf: delaying package configuration, since apt-utils is not installed

(Reading database ... 48207 files and directories currently installed.)

Preparing to unpack .../gcc-14-base_14.2.0-4ubuntu2~24.04.1_amd64.deb ...

Unpacking gcc-14-base:amd64 (14.2.0-4ubuntu2~24.04.1) over (14.2.0-4ubuntu2~24.04) ...

Setting up gcc-14-base:amd64 (14.2.0-4ubuntu2~24.04.1) ...

(Reading database ... 48207 files and directories currently installed.)

Preparing to unpack .../libgcc-s1_14.2.0-4ubuntu2~24.04.1_amd64.deb ...

Unpacking libgcc-s1:amd64 (14.2.0-4ubuntu2~24.04.1) over (14.2.0-4ubuntu2~24.04) ...

Setting up libgcc-s1:amd64 (14.2.0-4ubuntu2~24.04.1) ...

(Reading database ... 48207 files and directories currently installed.)

Preparing to unpack .../libstdc++6_14.2.0-4ubuntu2~24.04.1_amd64.deb ...

Unpacking libstdc++6:amd64 (14.2.0-4ubuntu2~24.04.1) over (14.2.0-4ubuntu2~24.04) ...


[中略]


Setting up nginx (1.24.0-2ubuntu7.6) ...

 * Upgrading binary nginx                                                [ OK ] 

Setting up libnginx-mod-http-headers-more-filter (1:0.37-2build1) ...

Setting up python3-acme (2.9.0-1) ...

Setting up python3-certbot (2.9.0-1) ...

Setting up certbot (2.9.0-1) ...

Created symlink /etc/systemd/system/timers.target.wants/certbot.timer → /usr/lib/systemd/system/certbot.timer.

Processing triggers for libc-bin (2.39-0ubuntu8.7) ...

Processing triggers for nginx (1.24.0-2ubuntu7.6) ...

Triggering nginx reload ...

Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.

Executing: /usr/lib/systemd/systemd-sysv-install disable nginx

Removed "/etc/systemd/system/multi-user.target.wants/nginx.service".

Rules updated

Rules updated (v6)

Rules updated

Rules updated (v6)

Rules updated

Rules updated (v6)

Firewall is active and enabled on system startup

Firewall reloaded

ubuntu@ordsmcp2:~$ 


システムの更新を反映するため、一旦インスタンスを再起動します。

sudo shutdown -r now

buntu@ordsmcp2:~$ sudo shutdown -r now


Broadcast message from root@ordsmcp2 on pts/1 (Mon 2026-03-02 02:26:58 UTC):


The system will reboot now!


nginxはホスト名指定でHTTPS接続ができるように構成します。そのため、DNSにホスト名とIPアドレスをあらかじめ登録しておきます。

コンピュート・インスタンスにユーザーubuntuで接続し、rootユーザーに切り替えます。

sudo -s
cd ~

ubuntu@ordsmcp2:~$ sudo -s

root@ordsmcp2:/home/ubuntu# cd ~

root@ordsmcp2:~# 


certbotを実行し、Let's Encryptより証明書を取得します。

以下の実行例ではホスト名にords.example.comを指定していますが、作業の際には割り当てているホスト名に置き換えます。

certbot certonly --standalone -d ホスト名

root@ordsmcp2:~# certbot certonly --standalone -d ords.example.com

Saving debug log to /var/log/letsencrypt/letsencrypt.log

Enter email address (used for urgent renewal and security notices)

 (Enter 'c' to cancel): メール・アドレス


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Please read the Terms of Service at

https://letsencrypt.org/documents/LE-SA-v1.6-August-18-2025.pdf. You must agree

in order to register with the ACME server. Do you agree?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(Y)es/(N)o: Y


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Would you be willing, once your first certificate is successfully issued, to

share your email address with the Electronic Frontier Foundation, a founding

partner of the Let's Encrypt project and the non-profit organization that

develops Certbot? We'd like to send you email about our work encrypting the web,

EFF news, campaigns, and ways to support digital freedom.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(Y)es/(N)o: N

Account registered.

Requesting a certificate for ords.example.com


Successfully received certificate.

Certificate is saved at: /etc/letsencrypt/live/ords.example.com/fullchain.pem

Key is saved at:         /etc/letsencrypt/live/ords.example.com/privkey.pem

This certificate expires on 2026-05-31.

These files will be updated when the certificate renews.

Certbot has set up a scheduled task to automatically renew this certificate in the background.


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

If you like Certbot, please consider supporting our work by:

 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate

 * Donating to EFF:                    https://eff.org/donate-le

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

root@ordsmcp2:~# 


nginxの構成ファイルを置き換えます。パブリック・エンドポイントのAutonomous AI DatabaseのAPEXにはリバース・プロキシを構成できないため、50-ords.conf60-apex-static-files.confは使用しません。

Oracle Linux 10向けに作成した設定ファイルを若干修正します。Oracle Linux 10に含まれるnginxは1.26、Ubuntu 24.04は1.24なので、設定ファイルにサポートされていない構文があります。また、Ubuntuでは設定ファイルを保存するディレクトリ/etc/nginx/default.dを作成します。

mkdir /etc/nginx/default.d

root@ordsmcp2:~# mkdir /etc/nginx/default.d

root@ordsmcp2:~# 


以下のnginx.conf/etc/nginx/nginx.confとして配置します。usernginxからwww-dataに変更します。またモジュールの構成ファイルとして、/usr/share/nginx/modules-available/*.confを参照する様に変更します。

以下の01-server.conf/etc/nginx/conf.d/01-server.confとして配置します。http2およびssl_ciphersの指定をコメントアウトし、server_nameおよびssl_certificatessl_certificate_keyに含まれるホスト名を変更します。

以下の10-root.conf/etc/nginx/default.d以下に配置します。

以下の90-error.conf/etc/nginx/default.d以下に配置します。

WWW-Authenticateヘッダーを返すための設定ファイル40-www-auth.confを、/etc/nginx/default.d以下に配置します。

リモートMCPサーバーを実装したRESTサービスへのリバース・プロキシ設定となる、30-mcp-adb.conf/etc/nginx/default.d以下に配置します。ords_upstream_hostAutonomous AI Datatabaseに接続するホスト名を設定します。

設定ファイルを配置後、書式を確認します。

nginx -t

root@ordsmcp2:~# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

root@ordsmcp2:~# 


nginxの自動起動を有効にし、実行します。

systemctl enable nginx
systemctl start nginx

root@ordsmcp2:~# systemctl enable nginx

Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.

Executing: /usr/lib/systemd/systemd-sysv-install enable nginx

Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

root@ordsmcp2:~# systemctl start nginx

root@ordsmcp2:~# 


以上でnginxによるリバース・プロキシの構成は完了です。


テスト用のRESTサービスの作成



動作確認に使用するAutonomous AI Databaseとして、Always Freeワークロード・タイプレイクハウスデータベースのバージョン19cのデータベースを使用します。


データベースが作成されたらツール構成より、Oracle APEXパブリック・アクセスURLをコピーし、ブラウザから開きます。


管理者ユーザーADMINでサインインします。パスワードは、Autonomous Database作成時に指定しています。


管理サービスが開きます。ワークスペースの作成を開始します。


新規のスキーマを選択します。


ワークスペース名ワークスペース・ユーザー名を指定します。今回の作業では両方ともapexdevとしました。

ワークスペースの作成をクリックします。


ワークスペースapexdevが作成されました。apexdevへのリンクをクリックします。


管理者ユーザーADMINでのワークスペース選択画面に移ります。サインアウトをクリックし、ADMINからサインアウトします。


ORDSのホームに戻ります。Oracle APEX実行します。


ワークスペース作成時に指定した、ワークスペース・ユーザー名およびパスワードでサインインします。


ユーザーAPEXDEVでサインインできるワークスペースが一覧されます。APEXDEVを選択します。


APEXのトップページが開きます。


以下の記事にそって、テストで使用するORDS RESTサービスを作成します。


以下の2つのAPEXアプリケーションをインポートします。

https://github.com/ujnak/apexapps/blob/master/exports/chat-with-generative-ai-hc-242.zip
https://github.com/ujnak/apexapps/blob/master/exports/mcp_handler.zip

これらにアプリケーションをインポートすると、リモートMCPサーバーとして呼び出せるツールとRESTサービスが作成されます。

SQLワークショップRESTfulサービスを開き、モジュールsampleserverを確認します。


モジュールsampleserverに含まれるテンプレートmcp完全なURLをコピーします。

MCP Inspectorを起動します。

npx @modelcontextprotocol/inspector

Transport TypeStreamable HTTPを選択し、URLに先ほどコピーしたRESTサービスの完全なURLを設定します。

Connectをクリックします。


Autonomous DatabaseにMCPで接続されます。上部のタブよりToolsを選択し、List Toolsを実行します。


ツールよりget_schemaを選択し、schemaSHを指定します。

Run Toolを実行します。スキーマSHが持つ表の一覧が取得されます。


ツールよりrun_sqlを選択し、sqlとして以下を記述します。

select * from sh.customers fetch first 10 rows only

Run Toolを実行します。表SH.CUSTOMERSから10行だけ取得します。


リバース・プロキシ経由での呼び出しを確認します。

nginxによりリバース・プロキシを実装したコンピュート・インスタンスにログインします。

rootユーザーで/etc/nginx/default.d/30-mcp-adb.confを編集します。

sudo vi /etc/nginx/default.d/30-mcp-adb.conf

set $ords_upstream_hostのホスト名の部分を更新します。

nginxを再起動します。

sudo systemctl restart nginx

ubuntu@ordsmcp2:~$ sudo systemctl restart nginx

ubuntu@ordsmcp2:~$ 


URLのホスト名の部分をnginxのリバース・プロキシが動作しているホストの名前で置き換え、MCP Inspectorから接続できることを確認します。


nginxではリクエストを転送しているだけなので、Autonomous Databaseを直接呼び出しているときと同様に、MCPのツール呼び出しができます。

プロキシ・サーバーの設定にはORDSの不具合の対応(DELETEメソッドを受け付けない)を含んでいるため、Disconnectが動作します。また、nginxのリバース・プロキシを実装している理由ですが、以下の認証サーバーによるOpen ID Connect認証も実装できます。(Microsoft Entra IDのみ実際に確認作業を実施)。


今回の記事は以上になります。