Set up the Nginx Stream module to proxy TCP, UDP, and UNIX-domain sockets requests. This example demonstrates proxying MariaDB requests to backend servers.
www.emc.world (IP: 10.0.0.31)node01.emc.world (IP: 10.0.0.51) - MariaDB#1node02.emc.world (IP: 10.0.0.52) - MariaDB#2[root@www ~]# dnf -y install nginx-mod-stream
Add stream configuration to the Nginx configuration file:
[root@www ~]# vi /etc/nginx/nginx.conf
# Add the following to the end of the file:
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
Modify SELinux settings if SELinux is enabled:
[root@www ~]# setsebool -P httpd_can_network_connect on
[root@www ~]# setsebool -P httpd_can_network_connect_db on
# Create and apply an SELinux policy module:
[root@www ~]# vi nginx-stream.te
# Add the following policy configuration:
module nginx-stream 1.0;
require {
type mysqld_port_t;
type httpd_t;
class tcp_socket name_bind;
}
allow httpd_t mysqld_port_t:tcp_socket name_bind;
[root@www ~]# checkmodule -m -M -o nginx-stream.mod nginx-stream.te
[root@www ~]# semodule_package --outfile nginx-stream.pp --module nginx-stream.mod
[root@www ~]# semodule -i nginx-stream.pp
Test the setup by accessing the frontend Nginx server from a client computer.
Execute the following commands on a client machine:
[cent@client ~]$ mysql -u serverworld -ppassword -h www.emc.world -e "show variables like 'hostname';"
# Output should show the hostname of one of the MariaDB servers
Repeat the command to verify that requests are balanced between node01.emc.world and node02.emc.world.