Deploying Next.js to IISNode: A Guide Without a Custom Server
Problem: Many developers face challenges deploying Next.js applications on Windows servers using IISNode. While using a custom server is an option, it can be complex and unnecessary. This article provides a simplified approach, eliminating the need for custom server setup.
Scenario: You've built a fantastic Next.js application and want to deploy it on a Windows server using IISNode, but you prefer a streamlined setup without a custom server.
Original Code (serverless.js):
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
});
The Solution:
The key to deploying Next.js without a custom server lies in understanding that Next.js itself can handle requests. It already includes a built-in server that can be accessed directly through IISNode.
Steps:
- Install IISNode: Download and install IISNode from https://www.iis.net/downloads/microsoft/iisnode.
- Create an IIS Application Pool: Go to "IIS Manager" > "Application Pools" and create a new application pool. Ensure you select "No Managed Code" as the .NET CLR version.
- Configure the Application Pool: In the advanced settings of the application pool, set the "Enable 32-Bit Applications" option to "True" (if your application requires it).
- Deploy your Next.js Application: Deploy your Next.js application to the desired location on your server. Ensure that the
public
directory of your Next.js application is located in the root directory of your IIS website. - Create an IIS Website: Create a new website in IIS Manager, pointing to the directory containing your Next.js application's
public
folder. - Configure IISNode: Go to your website's "Handler Mappings" and add a new mapping. Set the following:
- Request Path:
*
- Type:
IISNode
- Executable: The path to your
iisnode.exe
installation (e.g.,C:\Program Files (x86)\IISNode\iisnode.exe
) - Arguments:
-d {PATH_TO_YOUR_NEXTJS_APP_FOLDER}
- Request Path:
- Restart IIS: Restart IIS to apply the changes.
Analysis:
This approach utilizes IISNode's capabilities to serve your Next.js application directly, leveraging the built-in server of Next.js without the complexity of a custom server.
Benefits:
- Simplified Deployment: Eliminates the need for custom server code, streamlining the deployment process.
- Improved Performance: Leverages the inherent optimizations of Next.js's server-side rendering and static file serving.
- Increased Security: IISNode provides a secure environment for running your Next.js application.
Example:
Let's say your Next.js application is located at C:\MyNextJsApp
. You would configure the iisnode.exe
argument as follows:
-d C:\MyNextJsApp
Conclusion:
This method allows you to deploy your Next.js application on Windows servers using IISNode without the need for a custom server, offering a simple and efficient solution for development and production environments. By understanding this approach, you can significantly reduce deployment complexity and focus on building your applications.
Further Resources:
Remember to tailor your configuration based on your specific requirements and application setup.