Nextcloud behind reverse Apache proxy - 401 unauthorized

I had a similar or the same problem when I was experimenting with Apache2+php-fpm in containers. I could fix it using this rewrite rule: https://help.nextcloud.com/t/client-login-fails-with-401/29378/7