简体   繁体   English

Apache 上的 Blazor WASM 根本找不到我的 API 端点

[英]Blazor WASM on Apache is not able to find my API endpoints at all

So, I have a Blazor WASM solution that I've compiled for Linux and am running behind Apache.所以,我有一个 Blazor WASM 解决方案,我已经为 Linux 编译,并且在 Apache 后面运行。 I am using my publish\wwwroot folder as the document root in Apache, with the following VirtualHosts and Directory config for Apache (with SSL paths and the site's name redacted):我正在使用我的 publish\wwwroot 文件夹作为 Apache 中的文档根目录,并使用 Apache 的以下 VirtualHosts 和目录配置(使用 SSL 路径和红色名称)

<Directory /etc/apache2/blazor/publish/wwwroot>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<VirtualHost *:80> 
  ServerName SITENAME.com
  
  Redirect permanent / https://SITENAME.com/
</VirtualHost>

<VirtualHost *:80> 
  ServerName www.SITENAME.com
  
  Redirect permanent / https://SITENAME.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName SITENAME.com
        SSLEngine on
        SSLCertificateFile /SSLPATH
        SSLCertificateKeyFile /SSLPATH
        SSLCertificateChainFile /SSLPATH
        
        
    DocumentRoot "/etc/apache2/blazor/publish/wwwroot"
    ErrorDocument 404 /index.html

    AddType application/wasm .wasm
    AddType application/octet-stream .dll

    <Directory "/etc/apache2/blazor/publish/wwwroot">
        Options -Indexes
        AllowOverride None
    </Directory>

    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE application/octet-stream
        AddOutputFilterByType DEFLATE application/wasm
        <IfModule mod_setenvif.c>
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4.0[678] no-gzip
        BrowserMatch bMSIE !no-gzip !gzip-only-text/html
    </IfModule>
    </IfModule>

    ErrorLog /etc/apache2/blazor/blazorapp-error.log
    CustomLog /etc/apache2/blazor/blazorapp-access.log common
</VirtualHost>

When I go to my site, it loads up just fine, it seems to load all the.razor pages and execute client-side code just perfectly well.当我将 go 加载到我的站点时,它加载得很好,它似乎加载了所有.razor 页面并完美地执行客户端代码。

However, I cannot reach - through the browser or through Postman or through anything - any of the API endpoints running on the server.但是,我无法访问 - 通过浏览器或通过 Postman 或通过任何东西 - 在服务器上运行的任何 API 端点。 It simply cannot find/reach them.它根本无法找到/到达他们。 I have tried opening the ports explicitly in ufw (I'm running on Ubuntu 20.10), specifying different ports (and opening them) for the endpoints in my Blazor's launchsettings, I've tried having them listen on port 443 on the off chance that the API requests are being redirected to port 443 by Apache, and nothing works - I literally cannot get them, I just get the Error 404 index.html in response (which of course is not good) with body text of "An unhandled error has occurred."我尝试在 ufw 中显式打开端口(我在 Ubuntu 20.10 上运行),在 Blazor 的启动设置中为端点指定不同的端口(并打开它们),我尝试让它们在端口 443 上侦听,以防万一the API requests are being redirected to port 443 by Apache, and nothing works - I literally cannot get them, I just get the Error 404 index.html in response (which of course is not good) with body text of "An unhandled error has发生了。”

My launchsettings:我的启动设置:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:52679/",
      "sslPort": 44376
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "PROJECTNAME": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

The ports on the bottom, for the last applicationUrl, are what is actually used in production, and despite opening and trying to reach those specific ports (5001 and 5000), or changing those ports to 443 and 80 respectively, nothing works.最后一个 applicationUrl 的底部端口是实际在生产中使用的端口,尽管打开并尝试访问这些特定端口(5001 和 5000),或者将这些端口分别更改为 443 和 80,但没有任何效果。

There's no problem with the actual routing of the controllers that makes itself known during debugging, debugging on my development machine works just fine, everything is able to be reached.在调试过程中使自己知道的控制器的实际路由没有问题,在我的开发机器上调试工作正常,一切都可以达到。 I have no idea what's going on here to make the published version on Apache not able to reach its own Endpoints, and it's driving me absolutely crazy.我不知道这里发生了什么使 Apache 上的已发布版本无法到达自己的端点,这让我非常抓狂。

I have also tried using the following configuration lines in my Apache configuration, and it actually just seemed to make the entire site unreachable:我还尝试在我的 Apache 配置中使用以下配置行,它实际上似乎使整个站点无法访问:

<VirtualHost *:*>
    RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
</VirtualHost>

<VirtualHost *:443>
    ProxyPreserveHost On
    ProxyPass / https://127.0.0.1:5001/
    ProxyPassReverse / https://127.0.0.1:5001/
...
...
etc...

I am completely out of ideas.我完全没有想法。 If anyone else has compiled a Blazor WASM project to run on Linux with Apache, please share your knowledge, to spare me from my pain, lol.如果其他人编译了 Blazor WASM 项目以在 Linux 和 Apache 上运行,请分享您的知识,以免我痛苦,哈哈。

Links to things I've tried or written off as "this problem isn't what I'm having":链接到我尝试或注销的“这个问题不是我遇到的问题”:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-5.0 (where I got the Proxy config ideas from - which merely made my site unreachable, didn't help me reach my API endpoints) https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-5.0 (我从那里得到了代理配置的想法——这只是让我的网站无法访问,没有帮助我到达我的 API 端点)

ASP.NET Core 3.0 Controller Routing Not Working (the code in the answer is already in my solution, and debugging the controllers works just fine, it's only when deployed that issues arise) ASP.NET Core 3.0 Controller 路由不起作用(答案中的代码已经在我的解决方案中,并且调试控制器工作正常,只有在部署时才会出现问题)

In Blazor WebAssembly solution, how to host the Blazor WASM client app on a different port to the server API (This guy is just trying to add "/api" to his controller routing, which is already part of my routing scheme and not the issue as far as I can tell) In Blazor WebAssembly solution, how to host the Blazor WASM client app on a different port to the server API (This guy is just trying to add "/api" to his controller routing, which is already part of my routing scheme and not the issue据我所知)

Asp.net core deployment not working on server but works on machine (I am already compiling for "anyCPU") Asp.net 核心部署在服务器上不起作用但在机器上工作(我已经为“anyCPU”编译)

Likely others that I can't readily find currently... Been debugging and trying to fix this for over 10 hours.可能是我目前无法轻易找到的其他人......一直在调试并尝试修复这个问题超过 10 个小时。 I can't tell what the problem is to even begin to be able to fix it, and I can't find anyone online having the same problem.我不知道问题是什么,甚至开始能够修复它,而且我在网上找不到任何人有同样的问题。

Lastly, an example of the actual error itself, with mentions of the site name redacted - this is what happens when I make an API call, and get the HTML for my error page in response, and then try to convert that to an INT since it wasn't caught by any error handling prior to the conversion in a.razor page:最后,一个实际错误本身的示例,其中提到了站点名称已编辑 - 这是当我进行 API 调用时发生的情况,并为我的错误页面获取 HTML 作为响应,然后尝试将其转换为 INT,因为在 a.razor 页面中转换之前,它没有被任何错误处理捕获:

System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) <0x35ec788 + 0x0002a> in <filename unknown>:0 at System.Number.ParseInt32 (System.ReadOnlySpan`1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) <0x35c2c58 + 0x00048> in <filename unknown>:0 at System.Int32.Parse (System.String s, System.IFormatProvider provider) <0x35ebd38 + 0x00030> in <filename unknown>:0 at System.Convert.ToInt32 (System.String value) <0x35ebcf0 + 0x00016> in <filename unknown>:0 at REDACTED.Client.Pages.MainApplication.OnInitializedAsync () [0x00157] in C:\Users\codef\source\repos\REDACTED\REDACTED\Client\Pages\MainApplication.razor:100

RETURNED VALUE FROM API CALL: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>REDACTED</title> <meta property="og:title" content="REDACTED" /> <meta property="og:type" content="website" /> <meta property="og:url" content="https://REDACTED.com" /> <meta property="og:image" content="https://REDACTED.com/Promotional/Screenshots/Raw/Analysis%20Portfolio_7.png" /> <base href="/" /> <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" /> <link href="css/app.css" rel="stylesheet" /> <link href="css/Table.css" rel="stylesheet" /> <link href="_content/Blazor.ContextMenu/blazorContextMenu.min.css" rel="stylesheet" /> <link rel="stylesheet" href="_content/Radzen.Blazor/css/default-base.css"> <link href="//cdn-images.mailchimp.com/embedcode/slim-10_7.css" rel="stylesheet" type="text/css"> </head> <body> <app>Loading...</app> <div id="blazor-error-ui"> An unhandled error has occurred. <a href="" class="reload">Reload</a> <a class="dismiss">🗙</a> </div> <script src="_framework/blazor.webassembly.js"></script> <script src="_content/Blazor.ContextMenu/blazorContextMenu.min.js"></script> <script src="plotly-latest.min.js"></script> <script src="JavaScript.js"></script> <script src="_content/Radzen.Blazor/Radzen.Blazor.js"></script> <script> window.scrollToElementId = (elementId) => { console.info('scrolling to element', elementId); var element = document.getElementById(elementId); if (!element) { console.warn('element was not found', elementId); return false; } element.scrollIntoView(); return true; } </script> <style type="text/css"> #mc_embed_signup { background: #fff; clear: left; font: 14px Helvetica,Arial,sans-serif; } </style> </body> </html>

I did have trouble running on apache.我在 apache 上运行时确实遇到了麻烦。 In my case was because the Blazor index.html was not at the server root.在我的情况下是因为 Blazor index.html 不在服务器根目录中。

My index.html was at /app我的 index.html 在 /app

So I prefixed each page directive with Pages/, like so:所以我在每个页面指令前加上 Pages/,如下所示:

@page "/Pages/SupportRequest"

then in httpd.conf然后在 httpd.conf

RewriteEngine on
RewriteRule "^/app/Pages/.*$" "/app/index.html"

and index.html和索引。html

    <base href="/app/" />

NOTE: in the index.html in your project it will still need to be:注意:在项目的 index.html 中,它仍然需要:

    <base href="/" />

so you will have to make the change during deployment因此您必须在部署期间进行更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM