検索
ホームページウェブフロントエンドjsチュートリアルNGINX と負荷分散による Node.js アプリケーションのスケーリング

Scaling Node.js Applications with NGINX and Load Balancing

As your Node.js applications grow and receive more traffic, scaling becomes crucial to maintaining performance and stability. In this article, we'll dive into key techniques and tools that help scale your Node.js applications, focusing on NGINX as a reverse proxy and load balancer, as well as other methods for handling high traffic and demand.

In this article, we will cover:

  1. What is NGINX and why is it important?
  2. Setting up NGINX as a reverse proxy for Node.js.
  3. Load balancing with NGINX.
  4. Caching static content with NGINX.
  5. Scaling strategies for Node.js applications.
  6. Real-world use cases for scaling.

What is NGINX and Why is it Important?

NGINX is a high-performance web server known for its capabilities as a reverse proxy, load balancer, and HTTP cache. It efficiently handles large volumes of simultaneous connections, making it an excellent tool for scaling Node.js applications.

Key Features of NGINX:

  • Reverse Proxy: It routes incoming client requests to backend servers, helping distribute the load across multiple servers.
  • Load Balancing: NGINX balances the incoming traffic across multiple server instances, ensuring that no single server is overwhelmed.
  • Caching: It caches static content like images, stylesheets, and scripts, reducing the need to generate the same responses repeatedly.

Setting Up NGINX as a Reverse Proxy for Node.js

A reverse proxy routes client requests to one or more backend servers. Using NGINX as a reverse proxy for your Node.js application can offload tasks such as SSL termination, caching, and load balancing from your application.

Step-by-Step Setup:

  1. Install NGINX:
    First, install NGINX on your server. On Ubuntu, this can be done using:

    sudo apt update
    sudo apt install nginx
    
  2. Configure NGINX:
    Create a new configuration file for your Node.js app in /etc/nginx/sites-available/ directory.

Here's an example configuration file:

   server {
       listen 80;
       server_name yourdomain.com;

       location / {
           proxy_pass http://localhost:3000; # Your Node.js app
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }
   }
  1. Enable the Configuration:
    Create a symbolic link from sites-available to sites-enabled to enable the configuration:

    sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
    
  2. Restart NGINX:
    Restart NGINX to apply the changes:

    sudo systemctl restart nginx
    

Now, NGINX will forward any incoming requests to your Node.js application running on port 3000. The reverse proxy setup ensures that your Node.js app is isolated from direct client access, adding a layer of security.

Load Balancing with NGINX

When traffic increases, a single Node.js instance might struggle to handle all incoming requests. Load balancing allows traffic to be distributed evenly across multiple instances of your application, improving reliability and performance.

NGINX Load Balancer Setup:

  1. Modify NGINX Configuration: In the NGINX configuration file, define the backend servers that NGINX will balance requests across: ```nginx upstream node_app { server localhost:3000; server localhost:3001; server localhost:3002; }

server {
listen 80;
server_name yourdomain.com;

   location / {
       proxy_pass http://node_app;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;
   }

}

2. **Explanation**:
   - The `upstream` block defines a pool of Node.js servers running on different ports.
   - NGINX will distribute incoming requests evenly among these servers.

3. **Load Balancing Algorithms**:
   By default, NGINX uses a round-robin algorithm to balance traffic. You can specify other load balancing methods such as:
   - **Least Connections**: Sends requests to the server with the fewest active connections.
     ```nginx
     upstream node_app {
         least_conn;
         server localhost:3000;
         server localhost:3001;
     }
     ```

4. **Test and Scale**:
   You can now test the setup by running multiple instances of your Node.js app on different ports (3000, 3001, 3002, etc.) and monitor how NGINX balances the traffic.

## Caching Static Content with NGINX
Caching static content such as images, CSS, and JavaScript files can significantly reduce the load on your Node.js application by serving cached versions of these assets directly from NGINX.

### Caching Setup in NGINX:
1. **Modify Configuration for Caching**:
   Add caching rules to your server block:
   ```nginx
   server {
       listen 80;
       server_name yourdomain.com;

       location / {
           proxy_pass http://localhost:3000;
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }

       # Caching static content
       location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
           expires 30d;
           add_header Cache-Control "public, no-transform";
       }
   }
  1. Explanation:
    • The expires 30d; directive tells NGINX to cache the static files for 30 days.
    • This significantly improves the response time for these assets as they are served directly from NGINX without touching the Node.js application.

Scaling Strategies for Node.js Applications

Scaling a Node.js application isn’t just about using NGINX. Below are a few more techniques to ensure that your application scales effectively:

Vertical Scaling

Vertical scaling means upgrading the server's hardware resources, such as increasing the number of CPUs or adding more memory. While this can improve performance in the short term, it's limited by the physical capabilities of the machine.

Horizontal Scaling

Horizontal scaling involves running multiple instances of your application across different servers and balancing the traffic among them using tools like NGINX or a cloud load balancer. This method allows virtually unlimited scaling by adding more instances.

Clustering in Node.js

Node.js can run on multiple cores by using the built-in cluster module. This allows you to utilize all the available CPU cores on a server, increasing throughput.

Example:

const cluster = require('cluster');
const http = require('http');
const os = require('os');

if (cluster.isMaster) {
    const numCPUs = os.cpus().length;

    // Fork workers.
    for (let i = 0; i  {
        console.log(`Worker ${worker.process.pid} died`);
    });
} else {
    // Workers can share any TCP connection
    http.createServer((req, res) => {
        res.writeHead(200);
        res.end('Hello, world!\n');
    }).listen(8000);
}

This example shows how to use all CPU cores available on a machine by forking worker processes.

Real-World Use Case: Scaling an E-commerce Website

Problem: An e-commerce website is experiencing high traffic during sales events, leading to slow response times and occasional server crashes.

Solution:

  • Use NGINX to distribute traffic across multiple Node.js servers running different instances of the application.
  • Cache static assets like product images and JavaScript files with NGINX to reduce load on the server.
  • Implement Node.js Clustering to fully utilize the server’s CPU resources.

Outcome: The e-commerce website can now handle thousands of concurrent users without slowdowns, ensuring a smooth user experience during peak traffic times.

Why SSL, Encryption, and Security Matter?

When scaling applications, security should not be overlooked. Implementing SSL (Secure Sockets Layer) ensures that data transmitted between the client and server is encrypted and protected from attacks.

Steps to Enable SSL:

  1. Obtain an SSL Certificate from a trusted Certificate Authority (CA) like Let's Encrypt.
  2. Configure NGINX to use SSL:

    server {
       listen 443 ssl;
       server_name yourdomain.com;
    
       ssl_certificate /etc/ssl/certs/yourdomain.crt;
       ssl_certificate_key /etc/ssl/private/yourdomain.key;
    
       location / {
           proxy_pass http://localhost:3000;
       }
    }
    
  3. Redirect HTTP to HTTPS to ensure all traffic is secure:

    server {
       listen 80;
       server_name yourdomain.com;
       return 301 https://$host$request_uri;
    }
    

Conclusion

Scaling a Node.js application is essential as traffic and demand grow. By utilizing NGINX as a reverse proxy and load balancer, you can distribute traffic effectively, cache static assets, and ensure high availability. Combining these techniques with horizontal scaling and Node.js clustering enables your applications to handle massive traffic loads while maintaining performance and stability.

Implement these strategies in your projects to achieve better scalability, improved user experience, and increased uptime.

以上がNGINX と負荷分散による Node.js アプリケーションのスケーリングの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python vs. JavaScript:開発者の比較分析Python vs. JavaScript:開発者の比較分析May 09, 2025 am 12:22 AM

PythonとJavaScriptの主な違いは、タイプシステムとアプリケーションシナリオです。 1。Pythonは、科学的コンピューティングとデータ分析に適した動的タイプを使用します。 2。JavaScriptは弱いタイプを採用し、フロントエンドとフルスタックの開発で広く使用されています。この2つは、非同期プログラミングとパフォーマンスの最適化に独自の利点があり、選択する際にプロジェクトの要件に従って決定する必要があります。

Python vs. JavaScript:ジョブに適したツールを選択するPython vs. JavaScript:ジョブに適したツールを選択するMay 08, 2025 am 12:10 AM

PythonまたはJavaScriptを選択するかどうかは、プロジェクトの種類によって異なります。1)データサイエンスおよび自動化タスクのPythonを選択します。 2)フロントエンドとフルスタック開発のためにJavaScriptを選択します。 Pythonは、データ処理と自動化における強力なライブラリに好まれていますが、JavaScriptはWebインタラクションとフルスタック開発の利点に不可欠です。

PythonとJavaScript:それぞれの強みを理解するPythonとJavaScript:それぞれの強みを理解するMay 06, 2025 am 12:15 AM

PythonとJavaScriptにはそれぞれ独自の利点があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1. Pythonは、データサイエンスやバックエンド開発に適した簡潔な構文を備えた学習が簡単ですが、実行速度が遅くなっています。 2。JavaScriptはフロントエンド開発のいたるところにあり、強力な非同期プログラミング機能を備えています。 node.jsはフルスタックの開発に適していますが、構文は複雑でエラーが発生しやすい場合があります。

JavaScriptのコア:CまたはCの上に構築されていますか?JavaScriptのコア:CまたはCの上に構築されていますか?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc;それは、解釈されていることを解釈しました。

JavaScriptアプリケーション:フロントエンドからバックエンドまでJavaScriptアプリケーション:フロントエンドからバックエンドまでMay 04, 2025 am 12:12 AM

JavaScriptは、フロントエンドおよびバックエンド開発に使用できます。フロントエンドは、DOM操作を介してユーザーエクスペリエンスを強化し、バックエンドはnode.jsを介してサーバータスクを処理することを処理します。 1.フロントエンドの例:Webページテキストのコンテンツを変更します。 2。バックエンドの例:node.jsサーバーを作成します。

Python vs. Javascript:どの言語を学ぶべきですか?Python vs. Javascript:どの言語を学ぶべきですか?May 03, 2025 am 12:10 AM

PythonまたはJavaScriptの選択は、キャリア開発、学習曲線、エコシステムに基づいている必要があります。1)キャリア開発:Pythonはデータサイエンスとバックエンド開発に適していますが、JavaScriptはフロントエンドおよびフルスタック開発に適しています。 2)学習曲線:Python構文は簡潔で初心者に適しています。 JavaScriptの構文は柔軟です。 3)エコシステム:Pythonには豊富な科学コンピューティングライブラリがあり、JavaScriptには強力なフロントエンドフレームワークがあります。

JavaScriptフレームワーク:最新のWeb開発のパワーJavaScriptフレームワーク:最新のWeb開発のパワーMay 02, 2025 am 12:04 AM

JavaScriptフレームワークのパワーは、開発を簡素化し、ユーザーエクスペリエンスとアプリケーションのパフォーマンスを向上させることにあります。フレームワークを選択するときは、次のことを検討してください。1。プロジェクトのサイズと複雑さ、2。チームエクスペリエンス、3。エコシステムとコミュニティサポート。

JavaScript、C、およびブラウザの関係JavaScript、C、およびブラウザの関係May 01, 2025 am 12:06 AM

はじめに私はあなたがそれを奇妙に思うかもしれないことを知っています、JavaScript、C、およびブラウザは正確に何をしなければなりませんか?彼らは無関係であるように見えますが、実際、彼らは現代のウェブ開発において非常に重要な役割を果たしています。今日は、これら3つの間の密接なつながりについて説明します。この記事を通して、JavaScriptがブラウザでどのように実行されるか、ブラウザエンジンでのCの役割、およびそれらが協力してWebページのレンダリングと相互作用を駆動する方法を学びます。私たちは皆、JavaScriptとブラウザの関係を知っています。 JavaScriptは、フロントエンド開発のコア言語です。ブラウザで直接実行され、Webページが鮮明で興味深いものになります。なぜJavascrを疑問に思ったことがありますか

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。