apache .htaccess 重定向性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/209209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:53:05  来源:igfitidea点击:

.htaccess redirect performance

performanceapache.htaccessredirect

提问by deadprogrammer

Are htaccess redirects any slower/put any more strain on Apache than redirects in configuration files? Also, how big can a number of redirects in htaccess get before they start decreasing performance?

htaccess 重定向是否比配置文件中的重定向更慢/给 Apache 带来更多压力?此外,htaccess 中的重定向数量在开始降低性能之前可以达到多大?

回答by Vinko Vrsalovic

Yes, it slows the server because it has to access the file each time a resource in that directory or any subdirectory thereof is accessed.

是的,它会减慢服务器的速度,因为每次访问该目录或其任何子目录中的资源时,它都必须访问该文件。

The amount of redirects is not relevant, because the main performance hit is the file access itself. This within reasonable constraints (ie a 5 Kb htaccess file will take more or less the same time to be parsed than a 1 Kb one, different story is a 1Mb htaccess, though I've never seen that monstrosity and hope I never will)

重定向的数量无关紧要,因为主要的性能影响是文件访问本身。这在合理的限制范围内(即 5 Kb htaccess 文件的解析时间与 1 Kb 文件的解析时间差不多,不同的故事是 1Mb htaccess,尽管我从未见过这种怪物,希望我永远不会看到)

回答by bmdhacks

While it's true that the .htaccess is parsed on each request, and is thus technically slower than putting your rules in the main config file, in reality it doesn't matter. The apache configuration engine is fairly optmized C code that's embedded in the web server. Unless you're only serving small static files with no database accesses at all, the extra overhead of .htaccess and redirects is negligible.

虽然 .htaccess 在每个请求上都被解析,因此在技术上比将规则放在主配置文件中慢,但实际上这并不重要。apache 配置引擎是嵌入在 Web 服务器中的经过优化的 C 代码。除非您只提供没有数据库访问的小型静态文件,否​​则 .htaccess 和重定向的额外开销可以忽略不计。

Modern processors are so fast that you'd really have to be doing a massive amount of traffic to worry about this. If you're doing this much traffic, and since it's all static content, go ahead and buy yourself a second server to share the load.

现代处理器速度如此之快,以至于您真的必须处理大量流量才能担心这一点。如果您的流量如此之大,而且由于都是静态内容,请继续为自己购买第二台服务器来分担负载。

回答by Zxaos

Using a .htaccess file is slower than using a configuration file - a .htaccess file is parsed whenever a request is made to a directory it affects - this allows for changing the file without restarting the server. Since a configuration file is parsed only once at server start, it's faster.

使用 .htaccess 文件比使用配置文件慢——每当向其影响的目录发出请求时,都会解析 .htaccess 文件——这允许在不重新启动服务器的情况下更改文件。由于配置文件仅在服务器启动时解析一次,因此速度更快。

The amount of directives you can have in a .htaccess file without significant performance impact will be based on the complexity of the rules and your server's specifics, although the main performance hit will be from using the .htaccess file at all.

您可以在 .htaccess 文件中使用的指令数量不会对性能产生重大影响,这取决于规则的复杂性和您的服务器的具体情况,尽管主要的性能影响将来自于使用 .htaccess 文件。