在 NGINX 上托管 Blazor 独立 WASM

问题描述 投票:0回答:1

我在 nginx 后面作为反向代理托管 blazor WASM 独立主机(没有 asp.net core 项目作为主机)时遇到一些问题。

这是我的 Nginx 默认配置文件:

server {
        listen 80;
        listen [::]:80;

        server_name localhost;

        location / {
                root /var/www/web/BlazorApp/wwwroot;
                try_files $uri $uri/ index.html =404;

                include /etc/nginx/mime.types;
                types {
                        application/wasm wasm;
                }
                default_type application/octet-stream;
        }

        location /service1/ {
                proxy_pass http://localhost:5001/;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection keep-alive;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
        }

        location /service2/ {
                proxy_pass http://localhost:5002/;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection keep-alive;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
        }

        location /service3/ {
                proxy_pass http://localhost:5003/;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection keep-alive;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
        }
}

此配置的工作原理是我可以使用访问我的 blazor 应用程序

http://{server-ip-address}

以及我使用的其他服务

http://{server-ip-address}/serviceX

其中 X 分别指服务 1,2 和 3

第一个问题:当我在 blazor 应用程序中导航到

http://{server-ip-address}/My-Blazor-Page
并刷新页面时,出现 404 未找到错误。

为了让它再次工作,我需要返回基址

http://{server-ip-address}
并导航回
My-Blazor-Page
。 我无法刷新页面并返回同一页面。

第二个问题:我希望我的 blazor 应用程序有一个不同的位置。我想使用

http://{server-ip-address}/Blazor
而不是
http://{server-ip-address}/

我尝试了一切以使其正确,但这是唯一半有效的配置

非常感谢您的帮助!

asp.net-core nginx web-hosting blazor-webassembly nginx-config
1个回答
2
投票

来自 ASP.NET 文档

以下 nginx.conf 文件经过简化以显示如何配置 Nginx 在找不到索引时发送 index.html 文件 磁盘上有相应的文件。

使用 limit_req 设置 NGINX 突发速率限制时,Blazor WebAssembly 应用程序可能需要较大的突发参数值 容纳应用程序发出的相对大量的请求。 最初,将该值设置为至少 60:

如果浏览器开发人员工具或网络流量工具指示请求正在接收 503 - 服务,请增加该值 不可用状态代码。

有关生产 Nginx Web 服务器配置的更多信息,请参阅 创建 NGINX Plus 和 NGINX 配置文件。

上面将尝试 URL,如果没有文件匹配,它将改为提供 index.html。就是这样。

对于第二个问题,您应该将基本属性值设置为“Blazor”并将所有文件放入 Blazor 目录中(配置需要与此匹配)。您也可以选择不同的路线,但这是最简单的。

© www.soinside.com 2019 - 2024. All rights reserved.