I was developing a frontend for ERP application in Next.js 13.4 App router concept. I was struggling to deploy the build file in IIS server. When i tried by setting up iis server locally by adding server.js as a entry point it works. But it doesn't work when i point the physical path to the build version.
Please help me to deploy the production build in iis server (Next.js 13.4)
I am getting a result like,
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
HTTP subStatus: 1001
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is 'true'.
In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.
The last 64k of the output generated by the node.exe process to stderr is shown below:
(node:20328) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
This is my web.config file,
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" scriptProcessor="C:\Program Files\iisnode\iisnode.dll" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64,winx86_64,winx86_64" />
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}" />
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
<iisnode watchedFiles="web.config;*.js" />
<defaultDocument>
<files>
<add value="server.js" />
</files>
</defaultDocument>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
This is my server.js file,
const next = require("next");
const http = require("http");
const app = next({ dev: process.env.NODE_ENV !== "production" });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const port = process.env.PORT || 3000;
http
.createServer((req, res) => {
handle(req, res).catch((err) => {
console.error("Error handling request:", err);
res.status(500).send("Internal Server Error");
});
})
.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
});
Just refer to this blog: Deploy Nextjs on IIS using iisnode.
I'm not sure where you are in your current deployment process. Please make sure you have installed IISnode and URL Rewrite module.
Add iisnode in your web.config file(I noticed that it seems to be missing in your configuration ):
<iisnode node_env="production" nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" />
The whole web.config file like this:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="nextjs-app">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
<iisnode node_env="production" nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" />
</system.webServer>
<location path="" overrideMode="Deny">
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
</system.webServer>
</location>
</configuration>