设置 Nginx 服务器配置文件以将 Nginx 服务器与 Unity Web 构建结合使用。
可以使用 Nginx 快速高效地提供使用 Unity 创建的 Web 内容。
有关包含所有文件类型和压缩类型的完整代码示例,请参阅包含所有文件类型的完整代码示例。
有关 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;
}
如果在播放器设置中启用了启用原生 C/C++ 多线程 (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) 是一项 Web 浏览器安全功能,用于控制 Web 应用程序如何请求资源。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 *;
}
}
}