--- title: Using Nginx for PHP MVC Routing author: Brett Langdon date: 2014-03-17 template: article.jade --- An approach for using Nginx to handle all url routing for a PHP MVC Framework --- Last week I was profiling a PHP application and noticed the overhead of having the url routing written in PHP. It is not that the code was poorly written or anything, but just seemed like a lot of overhead for the application. So, I had an idea what if I moved the url routing to some other system that was made for handling url routing? Like, nginx. _Warning:_ My prototype makes the assumption that I am right and that nginx is actually faster than PHP for url routing. I am yet to do any performance testing, so I may actually be wrong here, but thought I'd share my musings anyways. ```php $value){ if(substr($name, 0, 9) == 'HTTP_URL_'){ $urlParams[substr($name, 0, 9)] = $value; } } $controller = new $controllerName(); $controller->handleRoute($urlParams); ``` ```cfg upstream php { server 127.0.0.1:8000; } server { listen 80; server_name localhost; location ~* /model/(?[0-9]+)(/.*?\.html)? { proxy_set_header PHP_CONTROLLER "ModelViewController"; proxy_set_header URL_MODELID $modelId; proxy_pass http://php; } location ~* ^(?/.*)$ { proxy_set_header PHP_CONTROLLER "DefaultController"; proxy_set_header URL_URL $url; proxy_pass http://php; } } ```