Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 208. IIS + Node.js 환경 구성 [링크 복사], [링크+제목 복사],
조회: 43092
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

IIS + Node.js 환경 구성

node.js의 node 프로세스가 현실적인 서비스를 하기 위해서는 80 포트를 사용해야 합니다. 그런데, 단순히 node.js 하나 운영하려고 웹 서버의 상징인 80 포트를 점유하는 것은 그리 바람직한 것은 아닙니다. 그래서 Apache 웹 서버의 경우 이를 위해 mod_proxy 등의 모듈을 이용해 proxy를 경유하는 형태로 처리하게 됩니다.

How to use Node.js with Apache on port 80
; http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80/

그렇다면 IIS라면 어떻게 해야 할까요? ^^




사실 처음에 제가 IIS + node.js 방법을 생각했을 때 누군가 80 포트를 공유하는 방식으로 node.exe를 확장한 프로그램을 공개하지 않았을까 기대했었습니다.

IIS의 80 포트를 공유하는 응용 프로그램 만드는 방법
; https://www.sysnet.pe.kr/2/0/1555

그런데, github에 공개되어 있는 확장 도구는 IIS의 module을 C/C++로 만드는 것으로 해결을 했습니다. (보시면 알겠지만, 이게 더 매끄럽게 통합이 됩니다.)

Hosting node.js applications in IIS on Windows
; https://github.com/tjanczuk/iisnode

자... 그럼 80 포트를 node.js와 ASP.NET이 사이좋게 사용하는 시나리오를 한번 실습해 볼까요? ^^

우선 "Hosting node.js applications in IIS on Windows" 글에 따라 다음과 같은 환경을 구성해 줍니다.

  1. 운영체제: Windows Vista 이후, 또는 Windows Server 2008 이후
  2. IIS 및 관리도구를 설치하고 ASP.NET을 활성화
  3. WebSocket은 Windows 8/2012 이상의 운영체제에서만 가능
  4. URL rewrite 모듈
  5. 윈도우 용 node.js

1, 2번은 ASP.NET 개발자라면 당연히 기본 셋팅되어 있을테니 생략하고, 3번은 WebSocket 기능을 사용하려면 IIS 8 이상이어야 한다고 명시한 것입니다. 왜냐하면 WebSocket이 IIS 8에 기본 내장되어 있기 때문에 그것을 쓰려는 것이고 2중으로 확장할 필요가 없어 그런 것 같습니다. URL Rewrite은 다음의 사이트에서 다운로드 받아 해결할 수 있습니다.

URL Rewrite
; http://www.iis.net/downloads/microsoft/url-rewrite

마지막으로 node.js는 MSI로 묶어 배포하는 버전을 설치해 주면 됩니다.

Latest node.js build for Windows (x86)
; http://go.microsoft.com/?linkid=9784334

설치는 msi만 실행해 주면 완료됩니다. 이후 cmd.exe 창을 열고 node를 실행하면 다음과 같이 실습할 수 있습니다. ^^

C:\Users\tester>node
> var test = 'test'
undefined
> console.log(test)
test
undefined
>

그나저나 윈도우에서 node.js 사용하는 방법이 많이 쉬워졌군요. ^^ 단순하게 msi만 실행해 주면 끝이라니.




이제 본격적으로 node.js와 IIS를 iisnode를 이용해 통합해 볼까요? ^^

iisnode for IIS 7.x/8.x: x86
; http://go.microsoft.com/?linkid=9784330

iisnode for IIS 7.x/8.x: x64
; http://go.microsoft.com/?linkid=9784331

위의 경로에서 플랫폼에 맞는 MSI 파일을 내려받아 설치해 줍니다. 역시 설치 과정은 단순하게 끝납니다.

자, 이제 ^^ node를 IIS의 웹 애플리케이션(또는 웹 사이트)에 통합할 수 있습니다. 적당하게 폴더를 하나 정해서 웹 애플리케이션을 하나 만들어 주고,

iis8_node_js_1.png

여기에서 node.js를 처리할 수 있도록 handler를 web.config을 이용해 연결해 주면 됩니다.

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="*.js" verb="*" modules="iisnode" />

      <!-- 
        또는 파일명까지 지정하는 것도 가능하고
      <add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
      -->
    </handlers>    
  </system.webServer>
</configuration>

이것으로 통합이 완료되었습니다. ^^ 이제 c:\nodetest 폴더에 다음과 같은 내용의 hello.js 파일을 만들어 실습할 수 있습니다.

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, world! [helloworld sample]');
}).listen(process.env.PORT);  

테스트 삼아서 "http://localhost/NodeTest/hello.js"로 방문하면 hello.js의 실행 결과를 볼 수 있습니다. 물론 해당 웹 애플리케이션은 IIS의 여느 가상 애플리케이션과 다를 바가 없기 때문에 다음과 같이 test.aspx 파일도 c:\nodetest 폴더에 포함시킬 수 있고,

<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>

<%
string text = Request.QueryString["inputValue"];
double result = 0;

if (string.IsNullOrEmpty(text) == false)
{
    int number = Int32.Parse(text);
    result = Math.Sqrt(number);
}
%>

<head>
    <title>제곱근 구하는 예제</title>
</head>

<body>

<form method="get" action="sqrt.aspx">
숫자: <input type="text" name="inputValue" /><br /><br />
<input type="submit" value="전송" /><br />
</form><br />

<%
if (result != 0)
{
%>
제곱근: <%=result%>
<%
}
%>

</body>
</html>

이렇게 동일한 /NodeTest URL로 호출하는 것이 가능합니다.

  • http://localhost/NodeTest/test.aspx
  • http://localhost/NodeTest/hello.js

iis8_node_js_2.png

오~~~ 멋지지 않나요? ^^ Apache와 비교해서 훨씬 자연스럽게 통합되었습니다. (혹시 이 글에서 소개된 Apache + node.js보다 더 쉬운 방법을 알고 계신 분은 공유 부탁드립니다.)




오류 정리입니다.

hello.js를 방문했을 때 웹 브라우저에서 다음과 같은 오류 메시지를 보게 된다면?

The iisnode module is unable to start the node.exe process. Make sure the node.exe executable is available at the location specified in the system.webServer/iisnode/@nodeProcessCommandLine element of web.config. By default node.exe is expected in one of the directories listed in the PATH environment variable.


이는 x64에서 실습한 경우에 발생하는데 다음의 Q&A에서 자세하게 설명해 주고 있습니다.

Error running node app in WebMatrix
; http://stackoverflow.com/questions/13079199/error-running-node-app-in-webmatrix

"Hosting node.js applications in IIS on Windows" 글에 공개된 "Latest node.js build for Windows" 버전은 x86으로 곧장 링크되어 있습니다. 따라서, IIS x64 웹 애플리케이션에서 x86 버전의 "C:\Program Files (x86)\nodejs\node.exe" 경로를 찾아들어갈 수 없어 발생하는 것입니다. 웹 브라우저에서 보여주는 오류 메시지의 내용이 그것이며 따라서 다음과 같은 설정을 web.config에 추가해 주면 해결됩니다.

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="*.js" verb="*" modules="iisnode" />
    </handlers>    

    <iisnode watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade" 
             nodeProcessCommandLine="C:\Program Files (x86)\nodejs\node.exe"/>
  </system.webServer>
</configuration>

또는 x64 버전의 node를 설치해 주어도 됩니다. 이 버전은 현재 다음의 사이트에서 배포되고 있습니다.

Node v0.8.22 (Stable)
; http://blog.nodejs.org/2013/03/06/node-v0-8-22-stable/

Windows x64 Installer
; http://nodejs.org/dist/v0.8.22/x64/node-v0.8.22-x64.msi

주의할 것은 현재 만들어진 node.js MSI 설치 파일은 x64를 설치한 경우 x86으로 설치된 node가 자동으로 삭제된다는 점입니다. (굳이 이렇게 설치 파일을 만든 이유를 모르겠군요.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 6/30/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2015-08-02 04시39분
[질문자] node.js는 혼자 언어와 서버 역할을 다 할 수 있는데
굳이 IIS 서버를 쓰는 이유가 무엇인가요?
현재 웹앱을 IIS서버에 DB와 같이 올리고 서버 언어는 node.js를 쓸려고 해서 공부중입니다.
[guest]
2015-08-02 12시17분
Java에서 WAS를 쓰는 것과 같은 이치입니다. 호스팅 프로세스에 대한 관리를 IIS에 맡겨 Recycle 기능을 덤으로 얻을 수 있다면, 당연히 윈도우 환경에서 마다할 이유가 없을 것입니다. ^^
정성태
2015-09-01 02시51분
[질문자] 핸들러 부분 저건 어디서 수정할 수 있나요?
web.config 파일을 못찾겠어요 ㅠㅠ
[guest]
2015-09-01 03시11분
web.config 파일은 그냥 만들어 주면 됩니다. 위의 글에서라면 "c:\nodetest" 폴더에 새 파일로 생성하면 됩니다.
정성태
2015-09-02 03시09분
[질문자] iisnode encountered an error when processing the request.

HRESULT: 0x2
HTTP status: 500
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 node.exe process has not written any information to stderr or iisnode was unable to capture this information. Frequent reason is that the iisnode module is unable to create a log file to capture stdout and stderr output from node.exe. Please check that the identity of the IIS application pool running the node.js application has read and write access permissions to the directory on the server where the node.js application is located. Alternatively you can disable logging by setting system.webServer/iisnode/@loggingEnabled element of web.config to 'false'.

이런 오류가 뜨는데 제가 완전 잘못한건가요?
xml configuration file을 web이라는 이름으로 만들어서 하니 저런 오류가 뜨네요...
기본서버파일인 wwwroot폴더에다 하는중인데 다 잘못됀건가요? 제가 iis는 초짜라서ㅠ
[guest]
2015-09-02 03시22분
이 글의 내용대로 실습했는데 그런 오류가 발생한다는 건가요?
정성태
2015-09-02 03시58분
[질문자] 네 그래서 찾아보니
IIS_IUSRS 그룹 쓰기 권한
이거랑 무슨 관련이 있는 것 같은데 한번 맨땅에 헤딩해보겠습니다!!
[guest]
2015-10-13 02시12분
정성태

... 136  137  138  139  140  141  142  143  144  145  146  147  148  149  [150]  ...
NoWriterDateCnt.TitleFile(s)
1302정성태6/25/201229301개발 환경 구성: 151. Azure 웹 사이트에 사용자 도메인 네임 연결하는 방법
1301정성태6/20/201225632오류 유형: 156. KB2667402 윈도우 업데이트 실패 및 마이크로소프트 Answers 웹 사이트 대응
1300정성태6/20/201231665.NET Framework: 329. C# - Rabin-Miller 소수 생성방법을 이용하여 RSACryptoServiceProvider의 개인키를 직접 채워보자 [1]파일 다운로드2
1299정성태6/18/201232764제니퍼 .NET: 21. 제니퍼 닷넷 - Ninject DI 프레임워크의 성능 분석 [2]파일 다운로드2
1298정성태6/14/201234276VS.NET IDE: 72. Visual Studio에서 pfx 파일로 서명한 경우, 암호는 어디에 저장될까? [2]
1297정성태6/12/201230909VC++: 63. 다른 프로세스에 환경 변수 설정하는 방법파일 다운로드1
1296정성태6/5/201227558.NET Framework: 328. 해당 DLL이 Managed인지 / Unmanaged인지 확인하는 방법 - 두 번째 이야기 [4]파일 다운로드1
1295정성태6/5/201224974.NET Framework: 327. RSAParameters와 System.Numerics.BigInteger 이야기파일 다운로드1
1294정성태5/27/201248438.NET Framework: 326. 유니코드와 한글 - 유니코드와 닷넷을 이용한 한글 처리 [7]파일 다운로드2
1293정성태5/24/201229661.NET Framework: 325. System.Drawing.Bitmap 데이터를 Parallel.For로 처리하는 방법 [2]파일 다운로드1
1292정성태5/24/201223681.NET Framework: 324. First-chance exception에 대해 조건에 따라 디버거가 멈추게 할 수는 없을까? [1]파일 다운로드1
1291정성태5/23/201230146VC++: 62. 배열 초기화를 위한 기계어 코드 확인 [2]
1290정성태5/18/201234984.NET Framework: 323. 관리자 권한이 필요한 작업을 COM+에 대행 [7]파일 다운로드1
1289정성태5/17/201239134.NET Framework: 322. regsvcs.exe로 어셈블리 등록 시 시스템 변경 사항 [5]파일 다운로드2
1288정성태5/17/201226322.NET Framework: 321. regasm.exe로 어셈블리 등록 시 시스템 변경 사항 (3) - Type Library파일 다운로드1
1287정성태5/17/201229172.NET Framework: 320. regasm.exe로 어셈블리 등록 시 시스템 변경 사항 (2) - .NET 4.0 + .NET 2.0 [2]
1286정성태5/17/201238100.NET Framework: 319. regasm.exe로 어셈블리 등록 시 시스템 변경 사항 (1) - .NET 2.0 + x86/x64/AnyCPU [5]
1285정성태5/16/201233153.NET Framework: 318. gacutil.exe로 어셈블리 등록 시 시스템 변경 사항파일 다운로드1
1284정성태5/15/201225593오류 유형: 155. Windows Phone 연결 상태에서 DRIVER POWER STATE FAILURE 블루 스크린 뜨는 현상
1283정성태5/12/201233187.NET Framework: 317. C# 관점에서의 Observer 패턴 구현 [1]파일 다운로드1
1282정성태5/12/201225983Phone: 6. Windows Phone 7 Silverlight에서 Google Map 사용하는 방법 [3]파일 다운로드1
1281정성태5/9/201233087.NET Framework: 316. WPF/Silverlight의 그래픽 단위와 Anti-aliasing 처리를 이해하자 [1]파일 다운로드1
1280정성태5/9/201226046오류 유형: 154. Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, ...'.
1279정성태5/9/201224803.NET Framework: 315. 해당 DLL이 Managed인지 / Unmanaged인지 확인하는 방법 [1]파일 다운로드1
1278정성태5/8/201226022오류 유형: 153. Visual Studio 디버깅 - Unable to break execution. This process is not currently executing the type of code that you selected to debug.
1277정성태5/8/201231220오류 유형: 152. cmd.exe - The system cannot write to the specified device. [2]
... 136  137  138  139  140  141  142  143  144  145  146  147  148  149  [150]  ...