Deploy NestJS on IIS
To deploy a NestJS application to Windows IIS (Internet Information Services), you’ll need to follow these general steps:
Prepare the NestJS Application:
Make sure you have built your NestJS application using the npm run build
command.
When you build a nodejs app, the build(dist) folder just contains your code, not node_modules
.
So, we can copy the package.json
file to the dist
folder and then run a npm install
command inside the build(dist) folder.
To do so, lets open the package.json
of your project and then add following scripts under the scripts
section.
"scripts": {
"build": "nest build && npm run copy && npm run node_modules:prod",
"copy": "copy \"package.json\" \"dist/\"",
"node_modules:prod": "cd dist && npm install --only=production",
}
Now, build your NestJS application using the npm run build
command.
Set Up IIS:
Open the Internet Information Services (IIS) Manager on your Windows server.
Create a new website or select an existing one where you want to deploy your NestJS app.
Ensure that the website is configured to use the appropriate .NET CLR version (typically v4.0) and the correct pipeline mode (Integrated).
Configure IIS for Node.js:
Install the IIS Node.js module by following the instructions provided in the official documentation: https://github.com/Azure/iisnode
Make sure you have Node.js installed on your server: https://nodejs.org/en/download
Create a Web.config File:
In your NestJS application’s root folder, create a web.config
file.
Open the web.config
file and add the following configuration:
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="main.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="NestJS" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="main.js" />
</rule>
</rules>
</rewrite>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
Copy the Files:
Copy the contents of the dist
folder generated during the NestJS build process into the directory of your website in IIS.
Ensure that the main.js
file and the web.config
file are in the same directory.
Restart IIS:
Restart the IIS server to apply the changes.
Now, your NestJS application should be deployed and accessible through IIS. You can access it by navigating to the appropriate URL in a web browser.