doucong1992 2012-06-24 16:40
浏览 23
已采纳

MVC路由 - 使用NGINX路由而不是PHP(或Ruby等)

I'm currently messing around with making a custom MVC framework for educational (and, if it's good, actual practical) use, and I like to investigate different scenarios for possible performance boosts.

When it comes to URI routing, I'm familiar with the standard URI format of

/controller/action/id

And parsing the data out of this to control the routing wouldn't be too difficult. Now, what I'm moreso wondering about is the performance difference between having nginx parse this URI string out into a query string of some type to pass to a controller directly, so that would end up like

/foo/bar/12 => /application/foo.php?action=bar&id=12

instead of

/foo/bar/12 => /index.php?controller=foo&action=bar&id=12

or even

/foo/bar/12 => /index.php?uri=/foo/bar/12   (note that this would be encoded)

I'm aware that nginx passes the url, query string, and other things to php-fpm in other variables already, but this is just for illustrative purposes to show what I'm thinking.

Is this a stupid thing to do? I know that by defining routes explicitly in nginx would mean me needing to restart nginx every time I alter the routes in the config, which could be a downside.

So, to restate the question: When it comes to MVC routing, is there any worthwhile performance gain by having the actual webserver (in this case, nginx) itself handle the routing to the controller OR is using a standard landing script (like index.php in the root of the directory) and passing along the URI to be parsed for routing perfectly fine?

Thanks ahead of time. Also, I'm just learning about these things, so I wholeheartedly welcome suggestions on what I should be doing instead.

  • 写回答

1条回答 默认 最新

  • dquh37673 2012-06-27 20:47
    关注

    I wouldn't mix application logic (URL routing) into your HTTP server. Lots of PHP apps used to rely on Apache .htaccess files for this sort of thing. It ends up being a mess.

    As you mention, it would require restarting Nginx to change routes, and it would also tie your application to Nginx, unless you wanted to rewrite all your rules for another HTTP server at some future date. Even worse, if you decide to scale your app out over more than one server, you'll have to repeat these rules for each upstream.

    tl;dr Keep your layers separate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法