Short version : change either the client or the server layout to US English, and it will mostly work (don't change both, of course; only one of them).
To quote the end of the excellent article which Xv linked to in his comment:
The usual advice [...] is to change the keyboard mapping on either the
client or the server.
The reason for this weird Mac behavior is well explained in the linked article.
So, after connecting to a Mac server from my Windows or Linux client, I change the layout on the Mac to US English. Then, my foreign layout sort-of mostly works as expected on the Mac. Some characters still don't work or are unavailable.
The alternative solution is to leave the Mac layout as it is, but switch the client to the US layout instead. That also sort-of mostly works, but slightly differently. The resulting layout is again similar to your normal foreign layout, but some characters still don't work as expected or are unavailable. However, they are not exactly the same as when switching the client layout.
Another possible solution could be to not use VNC at all, but try the NX NoMachine server and client instead. (I haven't tried this myself, so I'm not sure if it would solve the layout problem)