To upload a Next app to IIS, you can follow these steps:
1- Install the following modules
IIS NODE
https://github.com/Azure/iisnode/releases/tag/v0.2.26
URL REWRITE
https://www.iis.net/downloads/microsoft/url-rewrite
Application Request Routing
https://www.iis.net/downloads/microsoft/application-request-routing
2- Create a folder on your C disk and pass the following:
The .next folder you get when you do npm run build.
The public folder
The node_modules
3- Create a server.js file in your folder with the following information.
const { createServer } = require("http");
const { parse } = require("url");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const port = process.env.PORT ;
const hostname = "localhost";
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
if (pathname === "/a") {
await app.render(req, res, "/a", query);
} else if (pathname === "/b") {
await app.render(req, res, "/b", query);
} else {
await handle(req, res, parsedUrl);
}
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("internal server error");
}
})
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, async () => {
console.log(`> Ready on http://localhost:${port}`);
});
});
4- Configuration in the IIS
We check if we have our modules installed; we do this by clicking on our IIS server.
Then we click on modules to see the IIS NODE.
After that, we select feature delegation.
and verify that the controller mappings are read and write.
then we create our website in the IIS and reference the folder we created, click on the website and enter controller assignments
Once inside we click on add module assignments, in Request Path we put the name of the js file in this case "server.js", in module we select iisnode and in name we place iisnode.
We give him accept; This will create a configuration file in our folder called "Web". Open it and place this:
<!-- 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 site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>
<!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<add segment="node_modules"/>
</hiddenSegments>
</requestFiltering>
</security>
<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />
<iisnode node_env="production"/>
<!--
You can control how Node is hosted within IIS using the following options:
* watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
* node_env: will be propagated to node as NODE_ENV environment variable
* debuggingEnabled - controls whether the built-in debugger is enabled
See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
-->
<!--<iisnode watchedFiles="web.config;*.js"/>-->
</system.webServer>
We stop our site in the IIS, update and upload the site.