先生沉默先 2024-02-24 23:26 采纳率: 50%
浏览 49
已结题

Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面

Unity发布webgl之后让浏览器可以正常显示画面

我使用unity发布webgl之后,设置压缩方式为gzip(设置为Disabled我会设置),然后我需要添加相应头有:Content-Encoding: gzip,可能还有其他的响应头
我想知道我如何添加对应的响应头,我使用的是nginx作为web服务器,我的unity版本是unity2021.3
文档链接:WebGL:压缩构建和服务器配置;https://docs.unity.cn/cn/2020.3/Manual/webgl-deploying.html
文档链接:WebGL:服务器配置代码示例:https://docs.unity.cn/cn/2020.3/Manual/webgl-server-configuration-code-samples.html
浏览器需要适配主流的浏览器:谷歌火狐,360等。

我想看到完整的流程,有完整的配置文件

我的当前nginx的配置文件部分内容是(拷贝自Unity的文档)
按照下面的配置文件然后使用 localhost:7963直接就可以访问:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       7963;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
             
        }

        # 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/octet-stream;
        }

        # 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;
            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;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
  • 写回答

10条回答 默认 最新

  • 码农阿豪@新空间 优质创作者: Java、后端开发技术领域 2024-02-24 23:27
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如需一对一服务私聊我哦
    解答: 要在使用nginx作为web服务器的情况下,实现在Unity发布webgl后让浏览器可以正常显示画面,需要进行以下步骤:
    1. 设置压缩方式为gzip,可以在Unity的发布设置中完成,也可以在nginx的配置文件中进行设置。 例如,在nginx的配置文件中加入以下内容: gzip on; gzip_comp_level 2; gzip_types text/plain application/json application/javascript text/css application/xml text/xml application/xml+rss text/javascript; gzip_vary on;
    2. 添加响应头Content-Encoding: gzip,以启用gzip压缩方式。 例如,在nginx的配置文件中,可以在对应的location块中加入以下内容: add_header Content-Encoding gzip;
    3. 针对特定类型的文件添加特定的响应头,以满足不同文件的需求。 例如,对于Unity发布后的数据文件、JavaScript和WebAssembly文件,可以添加以下响应头: location ~ .+.(data|symbols.json).(br|gz)$ { gzip off; add_header Content-Encoding br; default_type application/octet-stream; } location ~ .+.js.(br|gz)$ { gzip off; add_header Content-Encoding br; default_type application/javascript; } location ~ .+.wasm.(br|gz)$ { gzip off; add_header Content-Encoding br; default_type application/wasm; } 以上代码示例是根据Unity官方文档中提供的代码进行修改的,具体实现方式可以根据自己的需求进行修改。 总之,要在Unity发布webgl后让浏览器可以正常显示画面,需要在nginx的配置文件中进行相应的设置,即设置压缩方式、添加相应响应头,并针对不同的文件类型添加相应的响应头,以满足其需求。
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月25日
  • 修改了问题 2月25日
  • 修改了问题 2月24日
  • 创建了问题 2月24日