Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 208. IIS + Node.js 환경 구성 [링크 복사], [링크+제목 복사]
조회: 36110
글쓴 사람
정성태 (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분
정성태

[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13605정성태4/23/2024203닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/2024231오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/2024311닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/2024661닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/2024751닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/2024761닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024809닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/2024835닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/2024803닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20241039닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/20241047닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241065닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241074닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241215C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241187닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241078Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241150닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241247닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신 [2]파일 다운로드1
13587정성태3/27/20241168오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241320Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
13585정성태3/26/20241105Windows: 262. PerformanceCounter의 InstanceName에 pid를 추가한 "Process V2"
13584정성태3/26/20241058개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법파일 다운로드1
13583정성태3/25/20241175Windows: 261. CPU Utilization이 100% 넘는 경우를 성능 카운터로 확인하는 방법
13582정성태3/19/20241433Windows: 260. CPU 사용률을 나타내는 2가지 수치 - 사용량(Usage)과 활용률(Utilization)파일 다운로드1
13581정성태3/18/20241606개발 환경 구성: 707. 빌드한 Unity3D 프로그램을 C++ Windows Application에 통합하는 방법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...