>  기사  >  운영 및 유지보수  >  NodeJS 애플리케이션 로드 밸런싱을 위해 Nginx를 구성하는 방법

NodeJS 애플리케이션 로드 밸런싱을 위해 Nginx를 구성하는 방법

WBOY
WBOY앞으로
2023-05-25 23:19:041432검색

로드 밸런싱은 처리를 위해 사용자 요청을 여러 서버에 할당하여 많은 수의 사용자에 대한 액세스 지원을 달성할 수 있습니다. 로드 밸런싱 아키텍처는 그림에 나와 있습니다.

NodeJS 애플리케이션 로드 밸런싱을 위해 Nginx를 구성하는 방법

복잡한 웹 애플리케이션의 경우 프런트엔드 로드 밸런싱을 위해 nginx를 사용하는 것은 물론 중요합니다.
다음으로 nodejs 애플리케이션의 로드 밸런싱을 위해 nginx를 사용합니다.
1. nginx 구성
nginx.conf 수정:

upstream sample { 
     server 127.0.0.1:3000; 
     server 127.0.0.1:3001; 
     keepalive 64; 
    } 
     server { 
      listen 80; 
      .... 
      server_name 127.0.0.1; 
      .... 
      location / { 
        proxy_redirect off; 
        proxy_set_header x-real-ip $remote_addr; 
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
        proxy_set_header x-forwarded-proto $scheme; 
        proxy_set_header host $http_host; 
        proxy_set_header x-nginx-proxy true; 
        proxy_set_header connection ""; 
        proxy_http_version 1.1; 
        proxy_pass http://sample; 
      } 
    }

포트 3000과 포트 3001에 node.js 서버가 있습니다. 이 두 서버는 동일한 작업을 수행합니다. 업스트림 섹션에는 두 개의 node.js 서버가 구성됩니다. 또한, 우리는 또한 Proxy_pass를 http 요청 프록시로 설정했습니다.

2. nodejs 서버 구축

var http = require('http'); 
var morgan    = require('morgan'); 
 
var server1 = http.createserver(function (req, res) { 
 console.log("request for: " + req.url + "-- port 3000 "); 
 res.writehead(200, {'content-type': 'text/plain'}); 
 res.end('hello node.js\n'); 
}).listen(3000, "127.0.0.1"); 
 
var server2 = http.createserver(function (req, res) { 
 console.log("request for: " + req.url + "-- port 3001 "); 
 res.writehead(200, {'content-type': 'text/plain'}); 
 res.end('hello node.js\n'); 
}).listen(3001, "127.0.0.1"); 
 
server1.once('listening', function() { 
 console.log('server running at http://127.0.0.1:3000/'); 
}); 
 
server2.once('listening', function() { 
 console.log('server running at http://127.0.0.1:3001/'); 
});

3. nginx 서버에 액세스

이제 액세스할 수 있습니다.
다음 출력을 볼 수 있습니다.

위 내용은 NodeJS 애플리케이션 로드 밸런싱을 위해 Nginx를 구성하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제