是否可以在Windows iis上运行Nest.js应用程序?

1szpjjfi  于 2023-04-06  发布在  Windows
关注(0)|答案(3)|浏览(432)

我有一个Nest.js应用程序,我想在Windows IIS服务器上运行。我已经安装了iisnode模块github.com/tjanczuk/iisnode,并在IIS上创建了一个新的网站和应用程序
我能够调用我的API端点,但收到以下消息:
警告:由于安全性和可用性问题,Buffer()已弃用。请改用Buffer.alloc()、Buffer.allocUnsafe()或Buffer.from()方法。
问题是我没有在我的代码中使用Buffer(),它只在我的项目中使用的一些节点模块中使用。
在我花更多的时间解决这个过时的警告之前,我只是想确定一下,在IIS上运行nest.js应用程序是否可以使用iisnode?
非常感谢你的帮助
这是我的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>
'''
ccrfmcuu

ccrfmcuu1#

从零开始

Install urlrewrite fist if not
在这里,我们将在iis* 服务器中创建一个 hello world 项目和 *host
所以安装nest globaly你的喜好我用这种方法

npm install -g @nestjs/cli

创建新的Nest项目

nest new myhost

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

cd myhost
npm run start

在这里我把 3000 端口改为 4444 你可以根据自己的意愿进行更改

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

npm run build

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

npm install pm2 -g


现在您可以检查链接http://localhost:4444/

现在打开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重写将端口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>
kqlmhetl

kqlmhetl2#

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

思路是通过PM2运行NestJs应用,然后在IIS中设置反向代理,将所有入站请求重定向到正在运行的NestJS应用。

首先确保您的应用程序正在通过PM2运行。
然后,将NestJS应用程序配置为在IIS中选择的端口上运行。
然后,在web.config中设置一个反向代理设置,它重定向到NestJs应用程序运行的实际端口。

<?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以获取更多信息。

fkaflof6

fkaflof63#

1.为了纠正警告:由于安全性和可用性问题,Buffer()已弃用。请改用Buffer.alloc()、Buffer.allocUnsafe()或Buffer.from()方法。
在“C:\Program Files\iisnode”下,修改第169行的“interceptor.js”文件。
这将解决这个问题所造成的iisnode本身。
1.仅通过iisnode启动Nestjs应用(即不使用反向代理)。
首先,确保安装了“URLRewrite”,并且您的“web.config”文件如下:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="main.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>
        <rule name="HTTP to Prod HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
        <!-- Don't interfere with requests for logs -->
        <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
        </rule>
        <!-- Don't interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^main.js\/debug[\/]?" />
        </rule>
        <!-- First we consider whether the incoming URL matches a physical file in the     /public folder -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}" />
        </rule>
        <!-- All other URLs are mapped to the Node.js application entry point -->
        <rule name="DynamicContent">
          <conditions>
             <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
          </conditions>
          <action type="Rewrite" url="main.js" />
        </rule>
      </rules>
    </rewrite>

  </system.webServer>
</configuration>

然后,修改你的“main.js”文件如下:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@nestjs/core");
const app_module_1 = require("./app.module");

const normalizePort = val => {
    const port = parseInt(val, 10);

    if (isNaN(port)) {
        return val;
    }
    if (port >= 0) {
        return port;
    }
    return false;
};
const port = normalizePort(process.env.PORT || '3000');

async function bootstrap() {
    
    const app = await core_1.NestFactory.create(app_module_1.AppModule);
    await app.listen(port);
}
bootstrap();
//# sourceMappingURL=main.js.map

注意process.env.PORT的使用。

相关问题