5544992 发表于 2017-12-27 15:44:21

在IIS上运行node



前言
  之前自己搞了个域名,然后发现域名默认映射到80端口,腾讯云又没有修改映射端口的功能。然后服务器又是和几个sx一起租的,于是只能想办法把发到80端口的请求分配到相应的项目里。当时有两个想法:自己写个中间件和用IIS,最后还是选了IIS,折腾好之后就没再管。结果最近升级了服务器,重新部署项目的时候发现基本上都忘光了。。于是就简单记录一下步骤。

iisnode
  IIS什么的就不多说了,直接说主要用的东西,iisnode。下载地址。
  另外还需要安装URL Rewrite。
  
安装iisnode之后,可以用%programfiles%\iisnode\setupsamples.bat来安装一个例子,然后访问http://localhost/node。
  
安装完之后,新建一个站点,监听80端口,配置好自己的域名。

web.config
  web.config配置可以参考github中samples中的configuration.
<configuration>  <system.webServer>
  <handlers>
  <add name=&quot;iisnode&quot; path=&quot;launch.js&quot; verb=&quot;*&quot; modules=&quot;iisnode&quot; resourceType=&quot;Unspecified&quot; requireAccess=&quot;Script&quot; />
  </handlers>
  <rewrite>
  <rules>
  <rule name=&quot;all&quot;>
  <match url=&quot;/*&quot; />
  <action type=&quot;Rewrite&quot; url=&quot;launch.js&quot; />
  </rule>
  </rules>
  </rewrite>
  <iisnode
  
      nodeProcessCommandLine=&quot;&quot;C:\Program Files\nodejs\node.exe&quot;&quot;
  
      interceptor=&quot;&quot;C:\Program Files\iisnode\interceptor.js&quot;&quot;
  
      promoteServerVars=&quot;REMOTE_ADDR&quot;/>
  </system.webServer>
  
</configuration>

launch.js
  因为bin会在url重写时被IIS屏蔽,因此不能直接把url重写到bin/www上,因此需要增加一个中间文件,或者修改bin目录的名字(当然是不推荐的)。
  
launch.js很简单,只需要require一下bin/www.
require('./bin/www');  接下来重启站点就可以了。

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

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

  这时候只要运行%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers 其中的handlers是报错的节点名字。
  嘛,我是到这里就没有问题可以正常运行了。欢迎访问,虽然没有什么东西。
页: [1]
查看完整版本: 在IIS上运行node