Log visits in shared memory
The best way is to use Lua with shared memory to store log entries, then create a timer which checks the size of logged entries every X seconds and uses a co-socket to dump the cache to a file or sql database. Should all be non-blocking. And yes you can pass requests to Lua before passing it to PHP.You could also use nginx default logfile caching(buffer) and parse the logs separately.