Nginx四层负载均衡1
阅读原文时间:2023年07月09日阅读:3
1、Nginx负载均衡Redis

服务器

IP地址

作用

系统版本

Nginx代理服务器

10.0.0.38

负载均衡服务器

Rocky8.6

Redis服务器1

10.0.0.18

Redis服务器1

Rocky8.6

Redis服务器2

10.0.0.28

Redis服务器2

Rocky8.6

client

10.0.0.101

测试访问网站

Ubuntu2004

# 安装redis服务器
# Rides1(10.0.0.18):
[root@redis1 ~]# yum install -y redis
[root@redis1 ~]# vim /etc/redis.conf        #修改监听端口
bind 0.0.0.0
[root@redis1 ~]# systemctl restart redis
[root@redis1 ~]# ss -ntl                    # 查看6379端口是否开启
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process
LISTEN    0         128                  0.0.0.0:6379              0.0.0.0:*
==========================================
# Rides2(10.0.0.28):
[root@redis2 ~]# yum -y install redis
[root@redis2 ~]# vim /etc/redis.conf
bind 0.0.0.0
[root@redis2 ~]# systemctl restart redis



# Nginx服务器(10.0.0.38):
# 提前安装好Nginx,这里是编译安装到/apps/nginx里
[root@Nginx nginx]# vim /apps/nginx/conf/nginx.conf
include  /apps/nginx/conf/tcp/*.conf;     #不建议直接更改主配置,所以增加一个包含目录(注意:此处和http是并列级别)
[root@Nginx nginx]# vim /apps/nginx/conf/tcp/redis.conf
stream  {
    upstream redis_server{
    server 10.0.0.18:6379 max_fails=3 fail_timeout=30s;
    server 10.0.0.28:6379 max_fails=3 fail_timeout=30;
   }
server {
    listen 80;
    proxy_pass redis_server;
    proxy_connect_timeout 3s;
    proxy_timeout 3s;
   }
}


#客户端安装redis测试:
[root@ubuntu2004 ~]#redis-cli -h 10.0.0.38 set name b   #增加一个name的变量,值是b
OK

[root@ubuntu2004 ~]#redis-cli -h 10.0.0.38 get name     #查询name变量
"b"
[root@ubuntu2004 ~]#redis-cli -h 10.0.0.38 get name
(nil)
[root@ubuntu2004 ~]#redis-cli -h 10.0.0.38 get name
"b"
[root@ubuntu2004 ~]#redis-cli -h 10.0.0.38 get name
(nil)

## 可以看到负载到两台rides上了。
2、Nginx负载均衡MySQL

服务器

IP地址

作用

系统版本

Nginx代理服务器

10.0.0.38

负载均衡服务器

Rocky8.6

Mysql服务器1

10.0.0.18

Mysql服务器1

Rocky8.6

Mysql服务器2

10.0.0.28

Mysql服务器2

Rocky8.6

client

10.0.0.101

测试访问网站

Ubuntu2004

#mysql两台服务器做主主复制:(参见本人博客“Rocky之Mysql主从复制”第6节)
mysql> create user wang@'10.0.0.%' identified by '123456';    #增加一个测试用户
mysql> flush privileges;
========================================================
# Nginx(10.0.0.38):
[root@rocky8 nginx-1.22.0]#vim /apps/nginx/conf/tcp/redis.conf
stream  {
    upstream redis_server{
    server 10.0.0.18:6379 max_fails=3 fail_timeout=30s;
    server 10.0.0.28:6379 max_fails=3 fail_timeout=30;
   }
    upstream mysql_server {
    server 10.0.0.18:3306 max_fails=3 fail_timeout=30s;
    server 10.0.0.28:3306 max_fails=3 fail_timeout=30;

    }
server {
    listen 6379;
    proxy_pass redis_server;
    proxy_connect_timeout 3s;
    proxy_timeout 3s;
   }
server {
    listen 3306;
    proxy_pass mysql_server;
   }
}

[root@rocky8 nginx-1.22.0]#nginx -s reload

====================================================
#客户端测试:
[root@rocky8 ~]# mysql -uwang -p123456 -h10.0.0.38 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          28 |
+-------------+
[root@rocky8 ~]# mysql -uwang -p123456 -h10.0.0.38 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          18 |
+-------------+
[root@rocky8 ~]# mysql -uwang -p123456 -h10.0.0.38 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          28 |
+-------------+
[root@rocky8 ~]# mysql -uwang -p123456 -h10.0.0.38 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          18 |
+-------------+
3、Rockey搭建LNMP(Nginx与php-fpm在同一服务器 )

服务器

IP地址

作用

系统版本

Nginx和PHP服务器

10.0.0.38

Nginx+php-fpm服务器

Rocky8.6

Mysql服务器

10.0.0.18

Mysql服务器

Rocky8.6

client

10.0.0.101

测试访问网站

Ubuntu2004

# nginx服务器(10.0.0.38):
[root@nginx ~]#yum install php-fpm php-mysqlnd php-json   #安装php相关包已经依赖
[root@nginx ~]#vim /etc/php-fpm.d/www.conf   #修改配置文件,最后结果如下:
[root@nginx ~]#egrep -v "^;|^$" /etc/php-fpm.d/www.conf
[www]
user = nginx       # 最好改为nginx运行的用户
group = nginx      # 最好改为nginx运行的组
listen = 127.0.0.1:9000          #监听地址及IP  (跨网络需要写网卡的IP)
pm.status_path = /pm_status      #取消注释
ping.path = /ping                #取消注释
ping.response = pong             #取消注释
slowlog = /var/log/php-fpm/www-slow.log    #慢日志路径,不用修改
php_admin_value[error_log] = /var/log/php-fpm/www-error.log    #错误日志路径,不用修改
php_value[session.save_handler] = redis     #修改此行
php_value[session.save_path]    = "tcp://redis-server:6379"   #修改此行
[root@nginx ~]#systemctl restart php-fpm.service
[root@nginx ~]#ss -ntl      #查看端口是否开启
LISTEN    0         128                127.0.0.1:9000              0.0.0.0:*  

[root@nginx ~]#vim /apps/nginx/conf/nginx.conf
 include /apps/nginx/conf/conf.d/*.conf;            #在http内增加
[root@nginx ~]#mkdir /apps/nginx/conf/conf.d
[root@nginx ~]#vim /apps/nginx/conf/conf.d/php.conf
server {
    listen 80;
    server_name www.wang.org;
    root /data/html;                 #静态页面访问/data/html

    location ~ \.php$ {
            root           /data/php;        #以php结尾的文件访问此目录
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;   #$document_root也可以写成上边root的目录:/data/php
            include        fastcgi_params;
    }
}
[root@nginx ~]#mkdir -p /data/php
[root@nginx ~]#mkdir /data/html
[root@nginx ~]#vim /data/php/index.php
<?php
phpinfo();
?>
[root@nginx ~]#vim /data/html/index.html
www.wang.org

[root@nginx ~]#nginx -s reload
=========================================
# 客户端测试连接:

# Mysql(10.0.0.18):安装mysql并创建用户
mysql> create user wang@'10.0.0.%' identified by '123456';
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
mysql> flush privileges;
===================================================

# nginx服务器(10.0.0.38):
[root@nginx ~]#vim /data/php/mysql.php      #测试连接数据库文件
<?php
    $servername = "10.0.0.18";
    $username = "wang";
    $password = "123456";
    // 创建连接
    $conn = mysqli_connect($servername,$username, $password);
    // 检测连接
    if (!$conn) {
        die("php连接MySQL数据库失败: " . mysqli_connect_error());
    }
    echo "php连接MySQL数据库成功!";
?>

# 修改配置页面测试ping和pm_status页面:(10.0.0.38):
[root@nginx ~]#vim /apps/nginx/conf/conf.d/php.conf
server {
    listen 80;
    server_name www.wang.org;
    root /data/html;

    location ~ \.php$|ping|pm_status {         #增加ping和pm_status
            root           /data/php;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }
}
[root@nginx ~]#nginx -s reload