> For AI agents: the complete documentation index is available at /resource/ucidm/docs/on-premise/llms.txt, the full documentation bundle is available at /resource/ucidm/docs/on-premise/llms-full.txt.

# Reverse Proxy

UCIDM API や管理画面にアクセスするときに HTTPS で通信するためのリバースプロキシを設定します。プロキシサーバーには [Apache](https://httpd.apache.org/) を使います。

あらかじめ、お客様のドメイン向けの SSL/TLS サーバー証明書をご用意ください。

サーバー証明書の自動更新を行う場合は [証明書の自動更新](/resource/ucidm/docs/on-premise/tech/mod_md.md) もあわせてご参照ください。

## インストール \{#installation}

```bash
# dnf install -y httpd mod_ssl
```

### SELinux の設定 \{#selinux-settings}

SELinux が有効な場合はリバースプロキシの振る舞いを許可する必要があります。

SELinux が有効かどうかは次のコマンドで調べられます。

```bash
# getenforce
Enforcing  # SELinux 有効
```

`httpd_can_network_connect` を有効にします。

```bash
# setsebool -P httpd_can_network_connect on
# getsebool httpd_can_network_connect
httpd_can_network_connect --> on
```

SELinux を有効にした状態で通信できないときは `/var/log/audit/audit.log` にログがでていないかをチェックしてください。

### SELinux で許可されているポート番号一覧を調べる \{#selinux-allowed-ports}

特定のポート番号で通信できないときは許可されているポート番号を試してみてください。

```bash
# dnf install -y policycoreutils-python-utils
# semanage port -l | grep http
```

## Apache の設定 \{#apache-settings}

```bash
# cd /etc/httpd
# cp -a /opt/osstech/share/ucidm/httpd/* .
```

サンプルの httpd.conf を使ってお客様の環境にあわせた内容に更新します。

### VirtualHost の設定 \{#virtualhost-settings}

[VirtualHost ディレクティブ](https://httpd.apache.org/docs/2.4/mod/core.html#virtualhost) でコンテナサービスのポート番号へプロキシする設定を行います。

- ID 連携管理画面やユーザープロファイル画面といった Web UI のプロキシ
- UCIDM API のプロキシ

また [Location ディレクティブ](https://httpd.apache.org/docs/2.4/mod/core.html#location) 単位にアクセス制限の設定を行います。

#### Web UI のプロキシ \{#web-ui-proxy}

```conf
<VirtualHost *:443>
  ServerName serverName.example.com

  <Location "/ui/user">
    ProxyPass http://localhost:5030/ui/user retry=10
    ProxyPassReverse http://localhost:5030/ui/user
    Require all granted
  </Location>

  <Location "/ui/admin">
    ProxyPass http://localhost:5030/ui/admin retry=10
    ProxyPassReverse http://localhost:5030/ui/admin
    Require all granted
  </Location>

  <Location "/ui">
    ProxyPass http://localhost:5030/ui retry=10
    ProxyPassReverse http://localhost:5030/ui
    Require all granted
  </Location>

  <Location "/sys">
    ProxyPass http://localhost:3030/sys retry=10
    ProxyPassReverse http://localhost:3030/sys
    Require all granted
  </Location>

  RewriteEngine On
  RewriteCond %{REQUEST_URI} !^/ui
  RewriteRule ^/$ /ui [R=301,L]
</VirtualHost>
```

#### API サーバーのプロキシ \{#api-server-proxy}

```conf
<VirtualHost *:8443>
  ServerName serverName.example.com

  <Location "/">
    ProxyPass http://localhost:18080/ retry=10
    ProxyPassReverse http://localhost:18080/
    Require all granted
  </Location>
</VirtualHost>
```

#### SSL/TLS サーバー証明書の設定 \{#ssl-tls-certificate}

動作確認テストのために sample-ca.crt, sample-server.crt, sample.key という自己証明書を使った設定をしています。お客様でご用意いただいた SSL/TLS サーバー証明書を配置してください。

```bash
# ls certs/ private/
certs/:
sample-ca.crt  sample-server.crt
private/:
sample.key
```

VirtualHost ディレクティブで次の設定を変更します。

```
SSLCertificateKeyFile /etc/httpd/private/sample.key
SSLCertificateFile /etc/httpd/certs/sample-server.crt
```

### Trusted Origins の設定 \{#trusted-origins}

クロスオリジンリクエストを許可する信頼済みオリジンを `/etc/httpd/conf.d/trusted-origins.conf` に設定します。このファイルはテンプレートとしてコメントアウトされた状態で配置されています。

少なくとも `TRUSTED_ORIGIN_0` を定義する必要があります。定義しない場合、httpd は起動時に以下のエラーとなり起動に失敗します。

```
At least TRUSTED_ORIGIN_0 must be defined in /etc/httpd/conf.d/trusted-origins.conf
```

`TRUSTED_ORIGIN_0` には UCIDM にアクセスするオリジン (スキームとホスト名) を設定します。

```conf
## /etc/httpd/conf.d/trusted-origins.conf
Define TRUSTED_ORIGIN_0 "https://ucidm.example.com"
```

複数のオリジンを許可する場合は連番で追加します。

```conf
Define TRUSTED_ORIGIN_0 "https://ucidm.example.com"
Define TRUSTED_ORIGIN_1 "https://sub.example.com"
Define TRUSTED_ORIGIN_2 "https://partner.example.co.jp"
```

### 設定の確認とサービスの起動 \{#verify-and-start-service}

httpd.conf の設定内容が正しいかをチェックします。

```bash
# apachectl configtest
Syntax OK
```

httpd サービスを起動し、OS の再起動時に httpd サービスを自動起動するように設定します。

```bash
# systemctl enable --now httpd
# systemctl is-enabled httpd
enabled
```

## リバースプロキシの疎通確認 \{#local-connection}

httpd の設定完了後、サービスを起動してローカルホストからの HTTPS の疎通確認を行います。

```bash
$ curl --head https://localhost/sys/
HTTP/1.1 200 OK
$ curl --head https://localhost/ui/
HTTP/1.1 200 OK
```

```bash
$ curl https://localhost:8443/status/version
{"ucidmVersion":"1.9.0","serverVersion":"814902d","idFederationClientVersion":"91a6c9e","mongoDBVersion":"8.2.4","rabbitMQVersion":"4.2.5"}
```

### リモートホストからの疎通確認 \{#remote-connection}

Web ブラウザで `https://${ホスト名}/sys/` へアクセスしてログイン画面が表示されることを確認します。

疎通できない場合はファイアウォールの設定などを確認してください。

```bash
# systemctl status firewalld
```
