Blazor / Kestrel / Apache: How to configure properly?

Viewed 727

I know, I know, Apache is not the best tool to use as HTTP proxy, however I need it on my server.

Here's my virtual host configuration:

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

<VirtualHost *:80>
    ServerName my.public.domain
    Redirect / https://my.public.domain/
</VirtualHost>

<VirtualHost *:443>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:50001/ 
    ProxyPassReverse / http://127.0.0.1:50001/
    ServerName my.public.domain
    ErrorLog ${APACHE_LOG_DIR}my-app-error.log
    CustomLog ${APACHE_LOG_DIR}my-app-access.log common
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/my-cert/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/my-cert/privkey.pem
</VirtualHost>

In UseUrls method i have http://localhost:50001 configured as main URL, and this is redirected by Apache to HTTPS @443.

It works as charm, however I see this in logs:

warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect.

In my configuration Apache handles https traffic, BTW, I can't communicate my app with Apache locally over HTTPS, it just doesn't work. I also think it's pointless to encrypt local internal traffic.

Unfortunately my solution requires some hacking to work 100% properly - I need to provide my public site URL in my app configuration - otherwise the app doesn't know what it's external address is. I mean - I build some links manually, because this is the core of my question - I don't know where the framework would keep such information. For example NavigationManager thinks my site URL is "http://localhost:50001", so if I need absolute URL in my app I can't use NavigationManager directly, I need to "manually" create the URL in app.

Links generated by Identity have "http" instead of "https", but it works because apache redirects everything to https.

Is there a way (and HOW) to do it more properly - a mean to officially tell the AspNET.Core it has specific external URL?

1 Answers

Where you have ServerName my.public.domain, use the following:

For port 80:

ServerName http://my.public.domain:80

For port 443:

ServerName https://my.public.domain:443
Related