I open sourced our code for a certificate manger. OpenResty uses SNI to serve a certificate per domain; the lookup is in nginx with failovers to redis and a python app. This may point you in the right direction:
https://github.com/aptise/peter_sslers/blob/master/tools/nginx_lua_library/ssl_certhandler.lua