利用iisnode模块,让你的Node.js应用跑在Windows系统IIS中

最近比较喜欢用Node.js做一些简单的应用,一直想要部署到生产环境中,但是手上只有一台windows server 2008服务器,并且已经开启了IIS服务,运行了很多.Net开发的网站,80端口已经被占用了。  

起初是想用nginx来作为web服务器监听80端口,将所有web访问转发到对应的IIS和node,但由于已运行的老站点众多,如此配置实在需要大量的精力,于是突发奇想,能不能直接利用IIS来托管node服务呢?进过一番搜索之后发现了iisnode模块,可以很轻松的解决这个问题。下面就把实操步骤分享出来,方便有同样需求的朋友参考。

首先iisnode是一个IIS Module加载到IIS以后,就可以在任意站点中通过Web.config指定某些路径转交给node程序执行,通过参数配置,可以设置启动的node进程个数,以及最大连接数等。并且可以监听站点文件变化,自动重启node服务等功能。

iisnode代码托管在github上,如果不想自己编译,可以直接通过以下链接下载适合自己的版本。

https://github.com/tjanczuk/iisnode/wiki/iisnode-releases

比如我的服务器是windows server 2008 64位系统,选择下载“iisnode for iis 7/8 (x64)”安装程序

只要版本正确,安装过程并没有需要特别注意的,自己根据提示一步一步完成即可。

之后还需要安装一下IIS的URL Rewrite模块(需要利用rewrite功能转发相关的请求交给node服务来执行)

下载地址:http://www.iis.net/downloads/microsoft/url-rewrite

软件全部安装完成之后,在IIS中新建网站,将目录指定到我们的nodejs应用目录即可,最后关键的一步,在目下新建web.config配置文件并写入如下的内容:

<configuration>
    <system.webServer>
        <handlers>
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
 
        <rewrite>
            <rules>
                <rule name="all">
                    <match url="/*" />
                    <action type="Rewrite" url="app.js" />
                </rule>
            </rules>
        </rewrite>
 
        <iisnode promoteServerVars="REMOTE_ADDR" />
    </system.webServer>
</configuration>

作用是将当前目录的所有请求都利用iisnode模块转发到node服务,并指定了node的执行目录。其中的app.js就是node应用的入口文件(可以按照自己的目录结构进行修改)。

一切就绪,现在打开浏览器访问网站,就可以看到效果了。

如果运行的时候出现如下错误:

500.19
配置错误 不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 allowOverride="false" 的位置标记明确设置的。

通过cmd运行如下代码即可解决:

%windir%system32inetsrvappcmd unlock config -section:system.webServer/handlers

其中的handlers是报错的节点名字。

另外如果是Express的项目,建议把web.config文件改为如下内容

<configuration>
    <system.webServer>
        <handlers>
            <add name="iisnode" path="launch.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" />
        </handlers>

        <rewrite>
            <rules>
                <rule name="all">
                    <match url="/*" />
                    <action type="Rewrite" url="launch.js" />
                </rule>
            </rules>
        </rewrite>

        <iisnode promoteServerVars="REMOTE_ADDR" />
    </system.webServer>
</configuration>

并新建程序入口文件launch.js 代码如下:

#!/usr/bin/env node

require('./bin/www');

如此操作的原因请参考博文:http://heeroluo.net/article/detail/118/suffering-from-iisnode

如果出现如下错误:

The iisnode module is unable to start the node.exe process. Make sure the node.exe executable is available at the location specified in the system.webServer/iisnode/@nodeProcessCommandLine element of web.config. By default node.exe is expected in one of the directories listed in the PATH environment variable

解决方法:在web.config 中的 system.webServer 节点加上以下内容:

<iisnode watchedFiles="*.js;node_modules*;routes*.js;views*.jade"  nodeProcessCommandLine="C:Program Files
ode.exe"/>
nodeProcessCommandLine的值为您node的路径

完整示例:
<configuration>
    <system.webServer>
        <handlers>
            <add name="iisnode" path="launch.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" />
        </handlers>

        <rewrite>
            <rules>
                <rule name="all">
                    <match url="/*" />
                    <action type="Rewrite" url="launch.js" />
                </rule>
            </rules>
        </rewrite>

        <iisnode promoteServerVars="REMOTE_ADDR" watchedFiles="*.js;node_modules*;routes*.js;views*.jade"  nodeProcessCommandLine="C:Program Files
ode.exe"/> 
  </system.webServer>
</configuration>
原文地址:https://www.cnblogs.com/aieceo/p/7906640.html