Unity Web ビルドで Nginx サーバーを使用するように、Nginx サーバー設定ファイルを設定します。
Nginx を使用すると、Unity で作成したウェブコンテンツを迅速かつ効率的に配信できます。
すべてのファイルタイプと圧縮タイプを含む完全なコード例については、すべてのファイルタイプを含む完全なコード例 を参照してください。
Nginx サーバーとそのインストール方法については、Nginx のドキュメント を参照してください。
既存の HTTP サーバー設定内で以下の設定を追加します。
http {
# ...
server {
# Nest inside existing location rule
location / {
# ...
これで、ビルドに固有のコードや、サーバーで使用するファイルタイプと圧縮タイプを追加する準備が整います。
Brotli 形式で圧縮済みのあらゆる種類のファイルがクライアントに効率的に配信されるようにするには、サーバー設定にコードを追加する必要があります。ただし、コードはファイルの種類 (データファイル、JavaScript ファイル、WebAssembly ファイル) によって異なります。
ディスク上で Brotli 形式で圧縮済みのデータファイル (データ、シンボル、.json ファイル) の場合は、以下のコードを追加します。
location ~ .+\.(data|symbols\.json)\.br$ {
gzip off;
add_header Content-Encoding br;
default_type application/octet-stream;
}
このファイルはディスク上ですでに圧縮済みなので、このコードにより、そのファイルに対するオンデマンド圧縮を無効にします。これを無効にしないと、Nginx は二重圧縮しようとします。
ディスク上で Brotli 形式で圧縮済みの JavaScript コードファイルの場合は、以下のコードを追加します。
location ~ .+\.js\.br$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding br;
default_type application/javascript;
}
ディスク上で Brotli 形式で圧縮済みの WebAssembly ファイルの場合は、以下のコードを追加します。
location ~ .+\.wasm\.br$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding br;
# Enable streaming WebAssembly compilation by specifying the correct MIME type for
# Wasm files.
default_type application/wasm;
}
gzip 形式で圧縮済みのあらゆる種類のファイルがクライアントに効率的に配信されるようにするには、サーバー設定にコードを追加する必要があります。ただし、コードはファイルの種類 (データファイル、JavaScript ファイル、WebAssembly ファイル) によって異なります。
ディスク上で gzip 形式で圧縮済みのデータの場合は、以下のコードを追加します。
location ~ .+\.(data|symbols\.json)\.gz$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding gzip;
default_type application/gzip;
}
ディスク上で gzip 形式で圧縮済みの JavaScript ファイルの場合は、以下のコードを追加します。
location ~ .+\.js\.gz$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding gzip; # The correct MIME type here would be application/octet-stream, but due to Safari bug https://bugs.webkit.org/show_bug.cgi?id=247421, it's preferable to use MIME Type application/gzip instead.
default_type application/javascript;
}
ディスク上で gzip 形式で圧縮済みの WebAssembly ファイルの場合は、以下のコードを追加します。
location ~ .+\.wasm\.gz$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding gzip;
# Enable streaming WebAssembly compilation by specifying the correct MIME type for
# Wasm files.
default_type application/wasm;
}
Player 設定 で Enable Native C/C++ Multithreading が有効になっている場合は、以下のコードをサーバー設定に追加します。
location ~ .+\.(htm|html|js|js\.gz|js\.br)$ {
add_header Cross-Origin-Opener-Policy same-origin;
add_header Cross-Origin-Embedder-Policy require-corp;
add_header Cross-Origin-Resource-Policy cross-origin;
}
オリジン間リソース共有 (CORS) は、ウェブアプリケーションがリソースをリクエストする方法を制御するウェブブラウザーのセキュリティ機能です。悪意のあるウェブサイトからの不正なリクエストを防ぎ、機密データを保護するのに役立ちます。
サーバーとのインタラクションで CORS リクエストを許可するには、以下のコードを設定ファイルに追加します。
add_header Access-Control-Allow-Origin *;
以下のコードは、すべてのファイルタイプを追加したサンプルコードです。
http {
# ...
server {
# Add the following config within the existing http server configuration
# Nest inside existing location rule
location / {
# ...
# On-disk Brotli-precompressed data files should be served with compression enabled:
location ~ .+\.(data|symbols\.json)\.br$ {
# Because this file is already pre-compressed on disk, disable the on-demand compression on it.
# Otherwise nginx would attempt double compression.
gzip off;
add_header Content-Encoding br;
default_type application/octet-stream;
}
# On-disk Brotli-precompressed JavaScript code files:
location ~ .+\.js\.br$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding br;
default_type application/javascript;
}
# On-disk Brotli-precompressed WebAssembly files:
location ~ .+\.wasm\.br$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding br;
# Enable streaming WebAssembly compilation by specifying the correct MIME type for
# Wasm files.
default_type application/wasm;
}
# On-disk gzip-precompressed data files should be served with compression enabled:
location ~ .+\.(data|symbols\.json)\.gz$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding gzip;
default_type application/gzip;
}
# On-disk gzip-precompressed JavaScript code files:
location ~ .+\.js\.gz$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding gzip; # The correct MIME type here would be application/octet-stream, but due to Safari bug https://bugs.webkit.org/show_bug.cgi?id=247421, it's preferable to use MIME Type application/gzip instead.
default_type application/javascript;
}
# On-disk gzip-precompressed WebAssembly files:
location ~ .+\.wasm\.gz$ {
gzip off; # Do not attempt dynamic gzip compression on an already compressed file
add_header Content-Encoding gzip;
# Enable streaming WebAssembly compilation by specifying the correct MIME type for
# Wasm files.
default_type application/wasm;
}
# Uncomment the following lines if build was created with "Enable Native C/C++ Multithreading" player settings
# location ~ .+\.(htm|html|js|js\.gz|js\.br)$ {
# add_header Cross-Origin-Opener-Policy same-origin;
# add_header Cross-Origin-Embedder-Policy require-corp;
# add_header Cross-Origin-Resource-Policy cross-origin;
# }
# Uncomment the following line to allow CORS requests
# add_header Access-Control-Allow-Origin *;
}
}
}