My personal blog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1.6 KiB

title author date template
Using Nginx for PHP MVC Routing Brett Langdon 2014-03-17 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
$controllerName = $_SERVER['HTTP_PHP_CONTROLLER'];
$urlParams = array();
foreach($_SERVER as $name => $value){
    if(substr($name, 0, 9) == 'HTTP_URL_'){
        $urlParams[substr($name, 0, 9)] = $value;
    }
}

$controller = new $controllerName();
$controller->handleRoute($urlParams);
upstream php {
    server 127.0.0.1:8000;
    }

server {
    listen 80;
    server_name localhost;

    location ~* /model/(?<modelId>[0-9]+)(/.*?\.html)? {
        proxy_set_header PHP_CONTROLLER "ModelViewController";
        proxy_set_header URL_MODELID $modelId;
        proxy_pass http://php;
    }

    location ~* ^(?<url>/.*)$ {
        proxy_set_header PHP_CONTROLLER "DefaultController";
        proxy_set_header URL_URL $url;
        proxy_pass http://php;
    }
}