包含此页的版本:
不含此页的版本:
设置 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 代码文件,请添加以下代码:lang-nginx
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++ 多线程时构建了项目,则需要设置特定的服务器标头。这些标头包括跨域打开器策略 (COOP)、跨域嵌入式策略 (COEP) 和跨域资源策略 (CORP)。
将此代码添加到配置文件中:
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 *;
}
}
}