<p>Assuming I have an open source web server or proxy I can enhance, let's say apache or squid. </p>

<p>Is there a way to determine the time each client spends on a web page?</p>

<p>HTTP is of course stateless, so it's not trivial, but maybe someone has an idea on how to approach this problem?</p>

Not without having some javascript constantly hit your server on the client side and then checking when it stops (but of course that assumes the user has javascript enabled). There are also various (ugly) ways to detect windows being closed with javascript, but of course these won't always trigger. eg. browser crash.
I sort of wonder why you want this anyway. What if a person looks at the web page for 3 seconds, gets distracted by another tab/window but leaves your page open for 2 hours? The answer you get is 2 hours, the answer you (probably) want is 3 seconds.