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.

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

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计