FreeBSD 14
Sponsored Link

Nginx : Use Stream module2024/02/13

 
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:~ #
vi /usr/local/etc/nginx/nginx.conf
# line 15 : add
load_module /usr/local/libexec/nginx/ngx_stream_module.so;

# add to last line
# [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:~ #
service nginx reload
[2] Verify it works fine to access to frontend Nginx server from any client computer.
freebsd@client:~ $ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node01.srv.world |
+---------------+------------------+
freebsd@client:~ $ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node01.srv.world |
+---------------+------------------+
freebsd@client:~ $ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node02.srv.world |
+---------------+------------------+
freebsd@client:~ $ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';"
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| hostname      | node01.srv.world |
+---------------+------------------+
Matched Content