最近比较喜欢用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%\system32\inetsrv\appcmd 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\node.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\node.exe"/>
  </system.webServer>
</configuration>

最新文章

  1. phpcms v9最常用的22个调用代码
  2. iOS学习24之UIControl及其子类
  3. Python学习笔记(0)
  4. myeclipse/eclipse没有Project Facets的解决方法
  5. c# 服务端
  6. iOS篇之有沙盒缓存
  7. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (六) 自动化测试结构小节
  8. 第 12 章 命令模式【Command Pattern】
  9. 每天一个Linux命令(20)--find命令之exec
  10. Android™ 1.5 android.R.drawable Icon Resources
  11. POJ 3279 枚举(思维)
  12. java学习笔记之System类
  13. 你学会UI设计了吗?
  14. filter 过滤器 禁止浏览器缓存
  15. python高级-生成器(17)
  16. Javascript 4.4
  17. Spring 配置文件中 元素 属性 说明
  18. qduoj LC的课后辅导
  19. 解决IDEA 配置搞坏的问题
  20. linux 的常用命令---------第八阶段

热门文章

  1. WPF读取和显示word
  2. Linux下C语言RPC(远程过程调用)编程实例
  3. android Choose library dependency 搜索不到目标库
  4. 编解码TIFF图像
  5. Linux 下配置 phpredis 的过程和遇到的问题
  6. 高性能高并发网站架构,教你搭建Redis5缓存集群
  7. spring cloud 系列第3篇 —— ribbon 客户端负载均衡 (F版本)
  8. angular安装
  9. java里字节与字符的区别
  10. 02_javaSE面试题:单例设计模式