What's the proper way to host Nuxt.js web app on IIS? - iis

I use IIS in Windows to run several web page servers.
And this time, I studied Nuxt.js.
Build a project created with Nuxt.js using the "npm run build" command.
I know that if you go into that folder and "npm run dev", the server opens on port 3000.
At this time, instead of "http://example.com:3000" on the web browser, I would like to launch the service through "http://example.com".
How shall I do it?
Is there a way to set it up in IIS Manager?
If not, should we consider a new web server instead of IIS?
If not, is there a way to set it up in Nuxt.js?
I tried the HTTP redirection feature in IIS Manager, but I could not get the desired result.

If you want to access the website on port 3000 by entering "http://example.com" in the browser address bar, you can do it through IIS reverse proxy.
First of all, you need to install URL Rewrite module and ARR module on IIS.
Then you need to double-click the Application Request Routing Cache on the server level, and select "Server Proxy Settings" on the right tree node, check "Enable Proxy" and apply.
According to your description, you need to have two websites on your IIS, one is the default website (port 80), and the other is the application website you deployed to IIS (port 3000). Next you need to create a rewrite rule on the default website, as follows:
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://example.com:3000/{R:1}" />
</rule>
</rules>
</rewrite>
By the above method, you can access your application through the URL: "http://example.com".

With HttpPlatformHandler you can easily host Nuxt.js web apps on IIS, but changes to your project are required as below,
Nuxt 2.x
The official guide to host Nuxt 2.x for Azure App Service (Windows) shows the general hints,
Create server\index.js.
Modify nuxt.config.js.
but it misses important steps,
You must add express and nuxt-start as dependencies (check your package.json).
Change const nuxt = await loadNuxt(isDev ? 'dev' : 'start') to simply const nuxt = await loadNuxt('start'), as isDev isn’t defined anywhere.
Then your web.config should look similar to,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Users\<user name>\AppData\Roaming\nvm\v16.13.2\node.exe" arguments=".\server\index.js">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
Note that iisnode mentioned in that Nuxt.js guide is no longer maintained, and only HttpPlatformHandler is recommended.
Note that rewrite rules in official guide were not added as the minimal sample project does not require them, but you can add them for your project if needed.
Nuxt 3.0
The steps are simplified,
npx nuxi init test-nuxt
cd test-nuxt
npm install
npm run build
with web.config,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Users\<user name>\AppData\Roaming\nvm\v16.13.2\node.exe" arguments=".output\server\index.mjs">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
Reference
My blog post on Nuxt 2.x
My blog post on Nuxt 3.0
Nuxt.js for Azure guide

Related

I created a project with NUXT 3 and used IIS's URL rewrite function, but there was an error [duplicate]

I use IIS in Windows to run several web page servers.
And this time, I studied Nuxt.js.
Build a project created with Nuxt.js using the "npm run build" command.
I know that if you go into that folder and "npm run dev", the server opens on port 3000.
At this time, instead of "http://example.com:3000" on the web browser, I would like to launch the service through "http://example.com".
How shall I do it?
Is there a way to set it up in IIS Manager?
If not, should we consider a new web server instead of IIS?
If not, is there a way to set it up in Nuxt.js?
I tried the HTTP redirection feature in IIS Manager, but I could not get the desired result.
If you want to access the website on port 3000 by entering "http://example.com" in the browser address bar, you can do it through IIS reverse proxy.
First of all, you need to install URL Rewrite module and ARR module on IIS.
Then you need to double-click the Application Request Routing Cache on the server level, and select "Server Proxy Settings" on the right tree node, check "Enable Proxy" and apply.
According to your description, you need to have two websites on your IIS, one is the default website (port 80), and the other is the application website you deployed to IIS (port 3000). Next you need to create a rewrite rule on the default website, as follows:
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://example.com:3000/{R:1}" />
</rule>
</rules>
</rewrite>
By the above method, you can access your application through the URL: "http://example.com".
With HttpPlatformHandler you can easily host Nuxt.js web apps on IIS, but changes to your project are required as below,
Nuxt 2.x
The official guide to host Nuxt 2.x for Azure App Service (Windows) shows the general hints,
Create server\index.js.
Modify nuxt.config.js.
but it misses important steps,
You must add express and nuxt-start as dependencies (check your package.json).
Change const nuxt = await loadNuxt(isDev ? 'dev' : 'start') to simply const nuxt = await loadNuxt('start'), as isDev isn’t defined anywhere.
Then your web.config should look similar to,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Users\<user name>\AppData\Roaming\nvm\v16.13.2\node.exe" arguments=".\server\index.js">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
Note that iisnode mentioned in that Nuxt.js guide is no longer maintained, and only HttpPlatformHandler is recommended.
Note that rewrite rules in official guide were not added as the minimal sample project does not require them, but you can add them for your project if needed.
Nuxt 3.0
The steps are simplified,
npx nuxi init test-nuxt
cd test-nuxt
npm install
npm run build
with web.config,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Users\<user name>\AppData\Roaming\nvm\v16.13.2\node.exe" arguments=".output\server\index.mjs">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
Reference
My blog post on Nuxt 2.x
My blog post on Nuxt 3.0
Nuxt.js for Azure guide

web.config settings not respected in .net core 3.1 Azure multi-instance scaled app service?

I'm trying to add/remove certain http headers from responses coming back from a 'pure' web api application (i.e. no MVC) published to Azure.
I added the following web.config to the project in VS2019:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff"/>
<add name="X-Frame-Options" value="SAMEORIGIN"/>
<remove name="X-Powered-By"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
This works fine in my dev environment with IISExpress. It also works fine when the app is published to Azure for app services that are not configured for multi-instance scaling. However, when the app service is configured for multi-instance scaling (three instances in my case) then responses from the app contain 'X-Powered-By' and no 'X-Content-Type-Options' or 'X-Frame-Options'.
Publishing creates the following web.config in Out folder on my dev machine:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff" />
<add name="X-Frame-Options" value="SAMEORIGIN" />
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\<apname>.exe" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
</system.webServer>
</configuration>
I also verified that the above web.config is present in the root of the app service in Azure.
Is there anything else that needs to be done in app service configuration in Azure for this to work with multi-instance scaling?
After testing, you do not need to make any changes. In the code or web.config, your solution is currently possible, provided that it is deployed in a windows-based webapp. If it is deployed under linux, then the web.config file is not effective. The web.config file is only applicable to iis. Under linux, the configuration file that needs to be used should be .htaccess.
Put web.config under the wwwroot path, which is the root directory of the project.
Based on windows azure webapp, the post-deployment effect should be consistent with the local iis effect. After testing, after I deploy, you can see the screenshots, and the effect in your web.config has been achieved.
Note:
Some headers cannot be deleted, but they can be overwritten. They need to be coded in the program. You are not involved in this question yet.
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff" />
<add name="X-Frame-Options" value="SAMEORIGIN" />
<-- replace server vaule -->
<add name="Server" value="N/A" />
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
Test Steps:
Method 1 with web.config
Create a sample project like below.
Deploy to azure, please see my files on scm site.
Method 2 without web.config (workaround, also works in linux)
Add below code in Startup.cs, it also works for me .
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Content-Type-Options", "nosniff");
context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
context.Response.Headers.Remove("X-Powered-By");
await next.Invoke();
});

How to serve NodeJS application from IIS/Windows Server Edition OS without using iisnode?

So, I've written a NodeJS application on Windows having Server Edition OS, My application basically communicates with other Softwares installed in the same system by executing some commands using NodeJS child process.
Everything is working fine on localhost, as my server has a static IP, I want to serve the application to public. How can I do this without using iisnode?
I tried using iisnode, but I am falling into issues since then, I am able to server my site, but due to some permission issues on C drive, the cmd command gives Access Denied error.
Option 1:
Run your Node.js app at a local binding such as http://localhost:8080 Reference
Set up IIS as reverse proxy Reference
iisnode is a dead project you definitely shouldn't use it any more.
Option 2:
Use HttpPlatformHandler to launch your Node.js app,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe" arguments=".\app.js">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
Reference
Note that due to your Node.js installation, C:\Program Files\nodejs\node.exe might be a mapped path. You have to use the actual path instead, such as C:\Users\<user name>\AppData\Roaming\nvm\v16.13.2\node.exe.
Note that you also need to give IIS_IUSRS enough permissions to access node.exe.

HTTP Error 502.5 running ASP.NET Core 2.0 app from IIS

I'm getting an HTTP 502.5 error when trying to run my ASP.NET core 2.0 web app from IIS.
I've installed the .NET Core Windows Server Hosting Bundle and checked all my IIS settings as per this document https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/index?tabs=aspnetcore2x
The web.config looks like this.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\OscarWeb.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
The Windows event log produces the following errors.
It is IIS v6.2 running on Windows Server 2012 R2. The web application was built using ASP.NET Core 2.0.
When I run dotnet from the commandline as follows there are no errors:
dotnet oscarweb.dll
The first thing you have to do in this scenario is to create the logs folder if it is missing and check the stdout logs generated.
It can be several different things but from personal experience the most common issue i had was IIS not having enough permissions to run it. In IIS you can configure the Identity used in Advanced Settings. If it is using ApplicationPoolIdentity then change it to LocalSystem and see if it works. The logs in the stdtout file however will give you the answer.
The solution to this problem (in my case at least) was to reboot the web server. After installing the .NET Core Windows Server Hosting Bundle you need to reboot the server for them to be registered correctly.
Thanks to Chris Pratt (first comment underneath my question) for suggesting the answer :)
Not sure if your webconfig is complete. I believe aspnetcore tag needs to have the ASPNETCORE_ENVIRONMENT environment variable set as well.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\OscarWeb.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="YourRuntimeEnv" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>

Sails.js iisnode configuration

I'm wondering if anyone could help with getting a sails JavaScript app running in IIS 7 on Windows.
https://github.com/tjanczuk/iisnode/issues/298 did not prove to be helpful for me.
I have gone through the iisnode setup and created this as my web.config file:
<configuration>
<system.webServer>
<!-- Tell IIS to use the iisnode module to run your
application -->
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<!-- Add iisnode with the #nodeProcessCommand line if
you see the error: Make sure the node.exe executable
is available at the location specified in the
system.webServer/iisnode/#nodeProcessCommandLine element
of web.config. -->
<iisnode
nodeProcessCommandLine="%ProgramFiles%\nodejs\node.exe"
/>
<!-- Since behind the covers, Sails.js is just an express app
rewrite all urls to processed by iisnode via app.js. This
will sort out things like the routing to your public
resources (images, js, styles) and all configured rest
endpoints. -->
<rewrite>
<rules>
<rule name="root">
<match url=".*" />
<action type="Rewrite" url="app.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sails is installed and when I run the server from the terminal sails lift I can access the app at http://localhost:1337/, but when I attempt to access it through the IIS port8090 I receive a HTTP 500 with the message:
To run an app using node app.js, you usually need to have a version of sails installed in the same directory as your app.
To do that, run npm install sails
Alternatively, if you have sails installed globally (i.e. you did npm install -g sails), you can use sails lift.
When you run sails lift, your app will still use a local ./node_modules/sails dependency if it exists,
but if it doesn't, the app will run with the global sails instead!

Resources