Freigeben über


Hosten und Bereitstellen von ASP.NET Core Blazor WebAssembly mit Apache

Hinweis

Dies ist nicht die neueste Version dieses Artikels. Die aktuelle Version finden Sie in der .NET 10-Version dieses Artikels.

Warnung

Diese Version von ASP.NET Core wird nicht mehr unterstützt. Weitere Informationen finden Sie in der .NET- und .NET Core-Supportrichtlinie. Informationen zum aktuellen Release finden Sie in der .NET 9-Version dieses Artikels.

In diesem Artikel wird erläutert, wie Blazor WebAssembly unter Verwendung von Apache gehostet und bereitgestellt wird.

So stellen Sie eine Blazor WebAssembly App in Apache bereit:

Erstellen Sie die Apache-Konfigurationsdatei. Das folgende Beispiel ist eine vereinfachte Konfigurationsdatei (blazorapp.config):

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias *.example.com

    DocumentRoot "/var/www/blazorapp"
    ErrorDocument 404 /index.html

    AddType application/wasm .wasm

    <Directory "/var/www/blazorapp">
        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 /var/log/httpd/blazorapp-error.log
    CustomLog /var/log/httpd/blazorapp-access.log common
</VirtualHost>

Erstellen Sie die Apache-Konfigurationsdatei. Das folgende Beispiel ist eine vereinfachte Konfigurationsdatei (blazorapp.config):

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias *.example.com

    DocumentRoot "/var/www/blazorapp"
    ErrorDocument 404 /index.html

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

    <Directory "/var/www/blazorapp">
        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 /var/log/httpd/blazorapp-error.log
    CustomLog /var/log/httpd/blazorapp-access.log common
</VirtualHost>

Platzieren Sie die Apache-Konfigurationsdatei in das /etc/httpd/conf.d/ Verzeichnis.

Platzieren Sie die veröffentlichten Ressourcen der App (/bin/Release/{TARGET FRAMEWORK}/publish/wwwroot, bei dem der {TARGET FRAMEWORK}-Platzhalter das Zielframework ist) in das /var/www/blazorapp-Verzeichnis (der Speicherort, der in der Konfigurationsdatei für DocumentRoot angegeben ist).

Starten Sie den Apache-Dienst neu.

Weitere Informationen finden Sie unter mod_mime und mod_deflate.

Gehostete Bereitstellung unter Linux (Apache)

Konfigurieren Sie die App mit ForwardedHeadersOptions, um die X-Forwarded-For- und X-Forwarded-Proto-Kopfzeilen weiterzuleiten, indem Sie den Leitfaden in Konfigurieren von ASP.NET Core für die Arbeit mit Proxyservern und Lastenausgleichsmodulen befolgen.

Weitere Informationen zum Festlegen des Basispfads der App, einschließlich der Konfiguration des Unter-App-Pfads, finden Sie unter ASP.NET Core-App-BasispfadBlazor.

Im folgenden Beispiel wird die App unter einer Stamm-URL gehostet (kein Unter-App-Pfad):

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

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass         / http://localhost:5000/
    ProxyPassReverse  / http://localhost:5000/
    ProxyPassMatch    ^/_blazor/(.*) http://localhost:5000/_blazor/$1
    ProxyPass         /_blazor ws://localhost:5000/_blazor
    ServerName        www.example.com
    ServerAlias       *.example.com
    ErrorLog          ${APACHE_LOG_DIR}helloapp-error.log
    CustomLog         ${APACHE_LOG_DIR}helloapp-access.log common
</VirtualHost>

Um den Server zu konfigurieren, damit die App in einem Unter-App-Pfad gehostet wird, ist der {PATH} Platzhalter in den folgenden Einträgen der Unter-App-Pfad:

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

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass         / http://localhost:5000/{PATH}
    ProxyPassReverse  / http://localhost:5000/{PATH}
    ProxyPassMatch    ^/_blazor/(.*) http://localhost:5000/{PATH}/_blazor/$1
    ProxyPass         /_blazor ws://localhost:5000/{PATH}/_blazor
    ServerName        www.example.com
    ServerAlias       *.example.com
    ErrorLog          ${APACHE_LOG_DIR}helloapp-error.log
    CustomLog         ${APACHE_LOG_DIR}helloapp-access.log common
</VirtualHost>

Für eine App, die auf Anforderungen antwortet, unter /blazor:

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

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass         / http://localhost:5000/blazor
    ProxyPassReverse  / http://localhost:5000/blazor
    ProxyPassMatch    ^/_blazor/(.*) http://localhost:5000/blazor/_blazor/$1
    ProxyPass         /_blazor ws://localhost:5000/blazor/_blazor
    ServerName        www.example.com
    ServerAlias       *.example.com
    ErrorLog          ${APACHE_LOG_DIR}helloapp-error.log
    CustomLog         ${APACHE_LOG_DIR}helloapp-access.log common
</VirtualHost>

Weitere Ressourcen