iisnode 性能很慢

iisnode performance is very slow

我发现 iisnode 比使用 node 命令慢。

有很多benefits使用iisnode,但是性能不好

我正在使用来自 here 的配置文件。

知道如何加快速度吗?


更新:

我注意到每个页面调用都会重新连接一个新的 mongodb 连接。

我该如何预防?

这里有一个在 Windows 64 位服务器上将 Node.js 与 IIS7 集成的提示。以下提示还解决了 iisnode 的一些性能不佳问题,并演示了如何使用 Node.js 本机扩展。

总结:

Create a new Unmanaged Integrated 32-bit Application Pool dedicated for node.js. No other applications should use this pool.

这也适用于 64 位,但一些 node.js 本机扩展如 Coconut2D 需要 32 位,因为 SQLite 包装。如果您不使用本机扩展,那么您可以一直使用 64 位!

  1. 下载:iisnode-core-iis7-v0.1.19.0-x64.msi.

  2. 使用此命令安装它:msiexec /i iisnode-core-iis7-v0.1.19.0-x64.msi WOW=1。这将在 64 位计算机上安装 32 位版本的 iisnode。请注意 iisnode.dll 将安装在 C:\Program Files (x86)\iisnode\iisnode.dll.

  3. 下载 node.js 的 32 位版本(例如 node-v0.12.0-x86.msi)并将其安装在 C:\nodejs

  4. 创建新的应用程序池

    Name: node.js
    Managed pipeline mode: Integrated 
    
    .NET Framework Version: No Managed Code
    Enable 32-Bit Applications: True
    Identity: administrator
    
  5. 假设您的 Node.js 服务器脚本 文件是 server.js。转到 web 文件夹并创建文件 node_start.cmd。在命令文件中,您应该将当前路径更改为 wwwroot 并使用 server.js 文件启动 node.js。您应该使用双引号路径。

    C:
    cd "C:\HostingSpaces\...\wwwroot"
    "C:\nodejs\node.exe" "C:\HostingSpaces\...\wwwroot\server.js"
    
  6. 在你的 server.js 中确保你有 process.env.PORT

    var http = require('http');
    http.createServer(function (req, res) {
        ... your code here ...
    }).listen(process.env.PORT);
    
  7. (可选) 如果您正在使用任何 node.js Native Extensions 例如 Coconut2D、SQLite 、Cairo 或 WebKit 模块,您必须将 *.node 文件和 DLL 复制到 wwwroot\node_modules 文件夹中。确保您还设置了 NTFS 安全性以允许执行这些文件或提升应用程序池以模拟管理员。要加载本机扩展,请使用 require(),如下所示。

    var http = require('http');
    var Coconut2D = require("Coconut2D.node");
    http.createServer(function (req, res) {
        ... your code here ...
    }).listen(process.env.PORT);
    
  8. 将以下内容 web.config 放入您的网络根目录(例如 C:\HostingSpaces\...\wwwroot)。这将使 IIS7 能够处理任何 非节点 文件,例如图像、静态 html 文件和 xml 文件,并让 node.js 仅处理它的拥有 服务器端脚本

    Having IIS handling static files and running server-side scripts on node.js side-by-side is a highly recommended practice and really boosts the performance of your web sites.

    在这个例子中,我使用重写规则处理 .asp 文件和 iisnode。将 *.asp* 替换为您的 node.js 服务器脚本扩展(例如 *.njs*)。请注意,通配符模式中没有根斜杠;这和模式末尾的最后一个 * 一样重要。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="node.js" verb="*" modules="iisnode" />
    </handlers> 
    <iisnode nodeProcessCommandLine="&quot;C:\...\start_node.cmd&quot;" />
    <defaultDocument>
      <files>
        <remove value="index.php" />
        <remove value="default.aspx" />
        <remove value="iisstart.htm" />
        <remove value="index.html" />
        <remove value="index.htm" />
        <remove value="Default.htm" />
      </files>
    </defaultDocument>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
    <rewrite>
      <rules>
        <rule name="CavoBoutique" patternSyntax="Wildcard">
          <match url="*.asp*" />
          <action type="Rewrite" url="node.js" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>