Unity与Nginx代理运行webGL程序

本文介绍了如何下载并配置Nginx,包括修改端口号,以及如何将WebGL项目部署到Nginx服务器,包括从优快云和百度网盘获取示例项目并进行整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载Nginx

在这里插入图片描述在这里插入图片描述
下载解压就是Nginx的文件。

Nginx配置

在这里插入图片描述

然后去conf文件夹下打开nginx.conf
创建一个文件

common_server_config.conf

粘贴以下内容

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$ {
    sendfile off;
    gzip off;
    add_header Content-Encoding br;
    default_type application/octet-stream;
}

# On-disk Brotli-precompressed JavaScript code files:
location ~ \.js\.br$ {
    sendfile off;
    gzip off;
    add_header Content-Encoding br;
    default_type application/javascript;
}

# On-disk Brotli-precompressed WebAssembly files:
location ~ \.wasm\.br$ {
    sendfile off;
    gzip off;
    add_header Content-Encoding br;
    default_type application/wasm;
}

# On-disk gzip-precompressed data files should be served with compression enabled:
location ~ \.(data|symbols\.json)\.gz$ {
    sendfile off;
    gzip off;
    add_header Content-Encoding gzip;
    default_type application/octet-stream;
}

# On-disk gzip-precompressed JavaScript code files:
location ~ \.js\.gz$ {
    sendfile off;
    gzip off;
    add_header Content-Encoding gzip;
    default_type application/javascript;
}

# On-disk gzip-precompressed WebAssembly files:
location ~ \.wasm\.gz$ {
    sendfile off;
    gzip off;
    add_header Content-Encoding gzip;
    default_type application/wasm;
}

location / {
    try_files $uri $uri/ =404;
}

打开nginx.conf,粘贴以下内容

worker_processes auto;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8901;
        server_name  localhost;
        root   html;
        index  index.html index.htm;

#导入相同的配置文件
        include common_server_config.conf;
    }
}

粘贴完成后双击nginx.exe运行。
在任务管理器中也可以看到运行的nginx.exe

WebGL运行

在这里插入图片描述nginx解压后的文件

打开html文件夹,将webGL文件拷贝进去,这里以后我的一个webGL的测试项目为例子
webGL示例项目的下载地址(优快云)

https://download.youkuaiyun.com/download/GoodCooking/24339451?spm=1001.2014.3001.5503

百度网盘

https://pan.baidu.com/s/1F-hXWwFxyGLiBeV-Tdhq8Q?pwd=mr7i

在这里插入图片描述

然后再启动nginx.exe就可以再浏览器的地址栏输入localhost:8901访问了。

在这里插入图片描述

另外一种配置文件

这个是将上面的两个文件nginx.confcommon_server_config.conf 合并到一个文件了。
使用上面两个文件的方式优点是:当需要使用nginx配置两个文件的时候,不需要复制一遍配置文件,只需要修改nginx.conf 中的文件,再添加一个server块即可。

worker_processes  1;

events {
    worker_connections  1024;
}

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

    server {
    listen       58231;
    server_name  localhost;
  
        location / {
            root   html;
            index  index.html index.htm;
        }


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

不懂私信,资源没有找我要

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值