Ubuntu 22.04
Sponsored Link

Nginx : Use Stream module2022/05/12

 
Configure Nginx to use Stream module.
It's possible to proxy TCP, UDP (Nginx 1.9.13 and later for UDP), UNIX-domain sockets requests.
This example is based on the environment like follows to proxy MariaDB requests to backend servers.
-----------+---------------------------+-----
           |                           |
           |10.0.0.31                  |
+----------+-----------+               |
|   [ www.srv.world ]  |               |
|        Nginx         |               |
+----------------------+               |
                                       |
------------+--------------------------+-----------
            |                          |           
            |10.0.0.51                 |10.0.0.52  
+-----------+----------+   +-----------+----------+
| [ node01.srv.world ] |   | [ node02.srv.world ] |
|       Mariadb#1      |   |       Mariadb#2      |
+----------------------+   +----------------------+

[1] Configure Nginx.
root@www:~#
apt -y install libnginx-mod-stream
root@www:~#
vi /etc/nginx/nginx.conf
# add to the end
# [weight=*] means balancing weight

stream {
    upstream mariadb-backend {
        server 10.0.0.51:3306 weight=2;
        server 10.0.0.52:3306;
    }
    server {
        listen 3306;
        proxy_pass mariadb-backend;
    }
}

root@www:~#
systemctl reload nginx

[2] Verify it works fine to access to frontend Nginx server from any client computer.
ubuntu@client:~$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node01.srv.world |
+---------------+------------------+
ubuntu@client:~$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node01.srv.world |
+---------------+------------------+
ubuntu@client:~$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node02.srv.world |
+---------------+------------------+
ubuntu@client:~$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node01.srv.world |
+---------------+------------------+
Matched Content