Deploy Nextjs App on IIS

Date:16 Feb 2025 at 9:58am

Last updated:16 Feb 2025 at 9:58am


Deploying nextjs app on IIS can seem challenging at first but once you get the hang of it, it’s a breeze.

To deploy a nextjs app on windows, you use a reverse proxy. In simple terms, a reverse proxy is a gateway that forwards a clients requests from on server to another. In this case we will set it up to accept traffic trhough an ip address and forward the traffic to our local instance of nextjs app.

Installations

Configure reverse proxy

Ensure you’ve installed URL Rewrite module and Application Request Routing(ARR) module.

After installation, you’ll need to enable proxy settings.

On IIS navigation pane, select the host and the select Application Request Routing option as shown below.

Screenshot of arr configuration

Under actions on the right side, select Server proxy settings

Screenshot of arr proxy configuration

Check Enable proxy option and uncheck Reverse rewrite host in response headers option

Add IIS website

Under IIS right click on sites and add a website. Chose an appropriate physical path

Screenshot of adding a website on iis

Select the website created and select URL Rewrite option on the center pane.

Go to Add Rule(s) > Reverse Proxy

Screenshot of reverse proxy configuration

Under inbound rules specify the local url to route traffic to.

Now trafic to mynextjsapp on IIS will be routed to http://localhost:3000

Let’s now setup http://localhost:3000

Get the source code and build it

Since nextjs sucks when it comes to distribution, unless you’re on vercel, you’ll have to get build the source code on your server. Upload the source on the server. I usually use github so, git clone is as trivial as it gets.

Build the source code

npm run build

Set up a windows service

Install Non Sucking Service Manager(NSSM) and add the nssm.exe path to the env path

Start a cmd prompt start nssm to install your windows service

nssm install MyAppWindowsService

This will launch nssm configuration window as shown below.

Screenshot of nssm configuration window

Specify application path as path to npm npm.cmd. To get this path, open a cmd prompt and run the command where npm For my case the output was H:\programs\nodejs\npm.cmd

Specify startup directory as the directory of the source code.

For arguments option specify,

npm run start -- --port=3000

Under details tab, fill in a display name and description. This is what whil appear under services.msc

Once done click on install.

Start the service

Open service manager, Start > Services.msc start your service.

You should now have your IIS website routing to your localhost app.

References


If you have any feedback on this article, shoot me an email, me at jnjenga.com