Nginx refactoring

Please merge this two patches =)