Nginx 负载均衡架构搭建


Nginx 负载均衡架构搭建

一、介绍

负载均衡跨越多个应用程序实例,是一种常用的技术,其用于优化资源利用率、最大化吞吐量、减少延迟和确保容错配置。

可以使用 nginx 作为高效的 HTTP 负载均衡器,将流量分布到多个应用服务器,并通过 nginx 提高 web 应用程序的性能、可扩展性和可靠性。

image.png

二、静态站点负载均衡

服务器信息

Hostname IP
proxy 192.168.2.88
web01 192.168.2.81
web02 192.168.2.82

配置 hosts

在所有机器的 /etc/hosts 文件添加:

192.168.2.88 proxy
192.168.2.81 web01
192.168.2.82 web02

安装 nginx

在所有机器上安装 nginx,参考官方安装文档:

《Installing NGINX Open Source》

nginx 主配置(所有机器)

主配置文件:/etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

proxy 配置

子配置文件:/etc/nginx/conf.d/proxy.conf

upstream webapp {
    server web01;
    server web02;
}

server {
    listen 80;
    charset utf-8;
    access_log /var/log/nginx/app.access.log main;

    location / {
        proxy_pass http://webapp;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_redirect off;
        proxy_connect_timeout 30;
        proxy_send_timeout 15;
        proxy_read_timeout 15;
    }
}

web01 和 web02 配置

子配置文件:/etc/nginx/conf.d/webapp.conf

server {
    listen      80;
    root        /www/wwwroot/webapp;

    location / {
        index   index.html;
    }
}

重启服务

在所有机器上,检查 nginx 配置

nginx -t

在所有机器上,重启 nginx

systemctl restart nginx

三、动静分离+负载均衡(PHP)

服务器信息

Hostname IP
proxy 192.168.2.88
web01 192.168.2.81
web02 192.168.2.82
phpserver01 192.168.2.83
phpserver02 192.168.2.84

注:web01 和 web02 存放静态资源文件,phpserver01 和 phpserver02 运行 php 程序,proxy 作为反向代理服务器。

配置 hosts

在 proxy 的 /etc/hosts 文件添加:

192.168.2.88 proxy
192.168.2.81 web01
192.168.2.82 web02
192.168.2.83 phpserver01
192.168.2.84 phpserver02

安装 nginx

在所有机器上安装 nginx,参考官方安装文档:

《Installing NGINX Open Source》

安装 php

在 phpserver01 和 phpserver02 上安装 PHP 环境,参考:

《CentOS 安装指定版本 PHP》

nginx 主配置(所有机器)

主配置文件:/etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

proxy 配置

子配置文件:/etc/nginx/conf.d/proxy.conf

upstream webapp {
    server web01;
    server web02;
}

upstream phpserver {
    server phpserver01;
    server phpserver02;
}

server {
    listen 80;
    charset utf-8;
    access_log /var/log/nginx/app.access.log main;

    location / {
        index index.php;
        proxy_pass http://phpserver;
    }

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html|htm|txt)$ {
        proxy_pass http://webapp;
    }

    location ~ \.php$ {
        proxy_pass http://phpserver;
    }
}

web01 和 web02 配置

子配置文件:/etc/nginx/conf.d/webapp.conf

server {
    listen      80;
    root        /www/wwwroot/webapp;
}

phpserver01 和 phpserver02 配置

子配置文件:/etc/nginx/conf.d/php.conf

server {
    listen 80;
    server_name phpserver01;

    # === Fastcgi Config ===
    fastcgi_connect_timeout 30;
    fastcgi_send_timeout 30;
    fastcgi_read_timeout 30;
    fastcgi_buffer_size  64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    location / {
        root /www/wwwroot/webapp;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

重启服务

在所有机器上,检查 nginx 配置

nginx -t

在所有机器上,重启 nginx

systemctl restart nginx

在 phpserver01 和 phpserver02 上启动 php-fpm

systemctl start php-fpm

文章作者: Truda
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Truda !
评论
  目录