【问题标题】:Is it possible to run an Nest.js application on windows iis?是否可以在 windows iis 上运行 Nest.js 应用程序?
【发布时间】:2020-07-31 01:57:20
【问题描述】:

我有一个想要在 Windows IIS 服务器上运行的 Nest.js 应用程序。 我已经安装了 iisnode 模块 github.com/tjanczuk/iisnode 并在 IIS 上创建了一个新网站和应用程序

我能够调用我的 Api 端点但收到以下消息:

警告:由于安全性和可用性问题,不推荐使用 Buffer()。请改用 Buffer.alloc()、Buffer.allocUnsafe() 或 Buffer.from() 方法。

问题是我不使用 Buffer() 我的代码,它只用于我项目中使用的一些节点模块。

在我花更多时间解决已弃用的警告之前,我只是想确定一下,通常是否可以使用 iisnode 在 IIS 上运行 nest.js 应用程序?

非常感谢您的帮助

这是我的 web.config 文件

<configuration>
  <system.webServer>

    <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

    <handlers>
      <add name="iisnode" path="main.js" verb="*" modules="iisnode" />
    </handlers>
<rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="sendToNode">
          <match url="/*" />
      <action type="Rewrite" url="main.js" />
        </rule>
      </rules>
    </rewrite>
  ...

  </system.webServer>
</configuration>
'''

【问题讨论】:

  • IISNode 是为在 Azure App Service 上提供 Node.js Web 应用程序而开发的,当时那里只有 Windows 操作系统。但是,Azure 应用服务现在完全支持 Linux/Docker,因此此类项目(IIS 上的 Node.js/Python)已停止维护(github.com/Azure/iisnode 已闲置两年)。实际上,这是一个提示,您不应该尝试在 IIS 上运行 Node.js。你能切换到 Linux 吗?
  • 您好 Lex Li,感谢您的快速回答。不,很遗憾没有:-(
  • 您的错误与 iis 无关。基于警告 new Buffer() 应替换为以下之一:Buffer.alloc()、Buffer.allocUnsafe() 或 Buffer.from()。 link1,link2

标签: windows iis nestjs iisnode


【解决方案1】:

从零开始

Install urlrewrite fist if not

在这里,我们将创建一个 hello world 项目并在 iis 中托管 服务器

所以在你的偏好中安装 Nest globaly 我正在使用这种方法

npm install -g @nestjs/cli

所以创建新的嵌套项目

nest new myhost

然后通过 this=> 移动到该文件夹​​=>


cd myhost
npm run start

这里我将3000端口改为4444你可以随意改变

只需构建您的项目,现在 dist 文件夹已创建

 npm run build

现在安装pm2节点包这个包作为后台服务pm2link

npm install pm2 -g

现在你可以查看链接http://localhost:4444/

Now Open iis(互联网信息服务)

==> Add Website
        ==>Add site name
        ==>Add Physical path as you build folder 
        ==> Add Port not 4444 because our node project running on that port so use different port

现在点击 url rewrite 将端口 4445(iis 托管端口)重定向到 4444(项目运行端口)

选择反向代理

添加反向代理规则

现在检查您的浏览器在两个端口上的工作情况

web.config 可能喜欢上面这个配置就够了

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:4444/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration> 

【讨论】:

    【解决方案2】:

    如果有人仍在寻找如何在 IIS 上托管 NestJs 应用程序的答案,您必须使用 URLRewrite 和 PM2(不需要 IISNode):

    在您的 IIS 中安装 URLRewrite 模块。

    安装PM2

    这个想法是通过 PM2 运行您的 NestJs 应用程序,然后在 IIS 中设置一个反向代理,将所有入站请求重定向到正在运行的 NestJS 应用程序。

    首先确保您的应用程序通过 PM2 运行。导航到您的main.js 文件所在的文件夹,然后运行pm2 start main.js

    然后,将 NestJS 应用程序配置为在 IIS 中选择的端口上运行。

    然后,在web.config 中设置一个反向代理设置,重定向到您的 NestJs 应用程序正在运行的实际端口。我的web.config 看起来像这样,我将入站请求重定向到localhost:3333

    <?xml version="1.0" encoding="UTF-8"?>
     <configuration>
      <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:3333/{R:1}" />
                </rule>
            </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

    就是这样。您的 NestJs 应用程序现在将在 IIS 上成功运行。

    请参考这个精彩的link 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 2010-10-27
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      相关资源
      最近更新 更多