LoginSignup
5
3

More than 3 years have passed since last update.

[Unity]WebGL: Server configuration for Nginx

Last updated at Posted at 2021-01-03

Unityの公式には、WebGLのサーバ設定例はIISとApacheしか書いていません。
WebGL: Server configuration code samples

nginx向けに書いて動作確認したものを掲載しておきます。

環境など

  • Unity 2020.2.0f
  • 使用したDockerイメージ: nginx:alpine
  • Herokuにホスティングする都合上、ポート番号が環境変数になっています。
  • gzip圧縮にのみ対応。Brotli圧縮の場合でも同じように書けばよいはず。

nginx.conf

server {
  listen $PORT;

  location / {
    root   /webgl; # お好みで
    index  index.html;

    gzip off;

    location ~* \.gz$ {
      add_header Content-Encoding gzip;

      location ~* \.data\.gz$ {
          types { }
          default_type application/octet-stream;
      }
      location ~* \.wasm\.gz$ {
          types { }
          default_type application/wasm;
      }
      location ~* \.js\.gz$ {
          types { }
          default_type application/javascript;
      }
      location ~* \.symbols\.json\.gz$ {
          types { }
          default_type application/octet-stream;
      }
    }
  }
}

content-typeの設定について

若干ハック感ありますが、一応nginxの公式の記法に倣っています。
Module ngx_http_core_module#types

追加モジュールが必要ですが、以下の方法で設定した方が可読性は高くなると思います。
nginxでレスポンスヘッダを書き換える - Qiita

余談: Herokuデプロイ用のDockerfile

FROM nginx:alpine

# copy binary files
WORKDIR /webgl
COPY ./bin .

# configure and run in foreground
WORKDIR /etc/nginx/conf.d
RUN rm default.conf
COPY ./environments/webgl.conf.template .
CMD /bin/ash -c "envsubst '\$PORT' < webgl.conf.template > default.conf" && /usr/sbin/nginx -g "daemon off;"

全体を通して

もっとこうしたほうがいいぞ、等あればご指摘ください。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3