首页  >  文章  >  web前端  >  NGINX 与 Node.js:负载平衡、提供静态内容和 SSL

NGINX 与 Node.js:负载平衡、提供静态内容和 SSL

Barbara Streisand
Barbara Streisand原创
2024-09-26 07:23:22496浏览

NGINX with Node.js: Load Balancing, Serving Static Content, and SSL

NGINX 是一个功能强大且多功能的 Web 服务器,在扩展 Node.js 应用程序中发挥着关键作用。它通常用作反向代理来处理负载平衡、提供静态内容和管理 SSL 终止。在本文中,我们将探索如何将 NGINX 与 Node.js 结合使用,并通过实际示例解释每个功能的工作原理。

为什么将 NGINX 与 Node.js 结合使用?

虽然 Node.js 擅长构建可扩展的事件驱动应用程序,但它可能不是处理负载平衡、提供静态内容或 SSL 终止等任务的最有效方法。这就是 NGINX 的用武之地。NGINX 针对高效处理大量并发连接进行了优化,使其成为需要扩展的 Node.js 应用程序的完美伴侣。

将 NGINX 与 Node.js 结合使用的主要好处:

  1. 负载平衡:NGINX 可以在多个 Node.js 实例之间分配流量,确保没有单个服务器被淹没。
  2. 提供静态内容:它有效地提供静态文件(例如 CSS、JS、图像),使 Node.js 能够专注于动态内容。
  3. SSL 终止:NGINX 处理 SSL/TLS 加密和解密,减少 Node.js 的负载。

1. 使用 NGINX 进行负载均衡

水平扩展时,您需要运行 Node.js 应用程序的多个实例。 NGINX 可以在这些实例之间分配传入流量,确保负载均匀。

第 1 步:安装 NGINX

在 Ubuntu 系统上,您可以使用以下命令安装 NGINX:

sudo apt update
sudo apt install nginx

步骤 2:NGINX 负载均衡配置

nginx.conf 文件是您定义 NGINX 如何处理传入请求的位置。以下是如何设置 NGINX 在三个 Node.js 实例之间进行负载平衡。

http {
    upstream node_app {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://node_app;
            proxy_set_header Host $host;
            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;
        }
    }
}

说明

  • 上游块定义了 NGINX 将在其之间平衡流量的 Node.js 实例池。
  • proxy_pass 指令将请求转发到 Node.js 实例之一。

第 3 步:启动多个 Node.js 实例

node app.js --port 3000 &
node app.js --port 3001 &
node app.js --port 3002 &

第 4 步:启动 NGINX

配置 NGINX 后,使用以下命令启动它:

sudo systemctl start nginx

测试设置
现在,访问服务器的 IP 地址或域应该在三个 Node.js 实例之间分发请求。

2. 使用 NGINX 提供静态内容

Node.js 应用程序通常需要提供静态文件(例如图像、CSS 和 JavaScript)。 NGINX 在此任务上效率更高,因为它旨在处理大量静态文件请求。

第 1 步:静态文件的 NGINX 配置

修改 nginx.conf 文件以定义静态内容的位置。

server {
    listen 80;

    # Serve static content directly
    location /static/ {
        root /var/www/html;
    }

    # Proxy dynamic requests to Node.js
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        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;
    }
}

说明

  • 位置 /static/ 块告诉 NGINX 在向 /static/ 发出请求时从 /var/www/html/static/ 提供文件。
  • 其他请求被代理到 Node.js 应用程序。

第 2 步:移动静态文件

将静态文件(例如图像、CSS、JavaScript)移动到 /var/www/html/static/ 目录。

sudo mkdir -p /var/www/html/static
sudo cp -r path/to/static/files/* /var/www/html/static/

现在,对 /static 资源的请求将由 NGINX 直接处理,从而提高 Node.js 服务器的性能。

3. 使用 NGINX 终止 SSL

SSL(安全套接字层)对于保护用户和应用程序之间的通信至关重要。 NGINX 可以卸载 SSL 终止、加密和解密请求,因此您的 Node.js 应用程序不需要自行处理 SSL。

第 1 步:获取 SSL 证书

您可以使用 Let’s Encrypt 免费获取 SSL 证书:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

步骤 2:NGINX SSL 配置

颁发 SSL 证书后,您可以配置 NGINX 来处理 SSL 流量。

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

server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        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;
    }
}

说明

  • 第一个服务器块将所有 HTTP 流量重定向到 HTTPS。
  • 第二个块侦听端口 443 (HTTPS),使用 Let’s Encrypt 颁发的证书处理 SSL 加密。

第 3 步:测试 SSL 设置

访问您的域(例如,https://yourdomain.com),您的 Node.js 应用程序现在应该通过 HTTPS 提供服务。

用于优化 Node.js 的其他 NGINX 配置

1. 处理超时

为了防止长时间运行的请求被过早关闭,请配置 NGINX 的超时设置。

server {
    proxy_read_timeout 90s;
    proxy_send_timeout 90s;
    send_timeout 90s;
}

2. Rate Limiting

Rate limiting can help prevent abuse and manage high traffic by limiting the number of requests a user can make in a given time.

http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;

    server {
        location / {
            limit_req zone=mylimit burst=5;
            proxy_pass http://localhost:3000;
        }
    }
}

Explanation:

  • The limit_req_zone directive defines a rate limit of 1 request per second.
  • The burst parameter allows a small number of requests to exceed the limit before throttling kicks in.

Conclusion

NGINX is a powerful tool that can significantly enhance the performance, security, and scalability of your Node.js applications. By offloading tasks such as load balancing, serving static content, and handling SSL termination to NGINX, your Node.js server can focus on what it does best: processing dynamic content and handling real-time events.

以上是NGINX 与 Node.js:负载平衡、提供静态内容和 SSL的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn