Nginx : Stream module を利用する2022/11/24 |
|
Nginx での Stream module の設定です。
任意の TCP, UDP (1.9.13), UNIX-domain sockets をプロキシ可能です。
当例では、以下のような環境を例に MaridaDB へのリクエストをバックエンドサーバーへ転送します。
-----------+---------------------------+-----
| |
|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] | Nginx の設定です。 |
|
[root@www ~]#
dnf -y install nginx-mod-stream
[root@www ~]#
vi /etc/nginx/nginx.conf # 最終行に追記 # [weight=*] で重み付け (weight=2 の場合 指定無サーバーと 2:1 の割合でバランシング)
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] | SELinux を有効にしている場合は、ポリシーの変更が必要です。 |
|
[root@www ~]# setsebool -P httpd_can_network_connect on [root@www ~]# setsebool -P httpd_can_network_connect_db on
[root@www ~]#
vi nginx-stream.te # 以下の内容で新規作成
module nginx-stream 1.0;
require {
type mysqld_port_t;
type httpd_t;
class tcp_socket name_bind;
}
#============= httpd_t ==============
allow httpd_t mysqld_port_t:tcp_socket name_bind;
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 |
| [3] | 任意のクライアントコンピューターからフロントエンドの Nginx サーバーへ Mariadb アクセスして動作確認します。 |
[cent@client ~]$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';" +---------------+------------------+ | Variable_name | Value | +---------------+------------------+ | hostname | node01.srv.world | +---------------+------------------+ [cent@client ~]$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';" +---------------+------------------+ | Variable_name | Value | +---------------+------------------+ | hostname | node01.srv.world | +---------------+------------------+ [cent@client ~]$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';" +---------------+------------------+ | Variable_name | Value | +---------------+------------------+ | hostname | node02.srv.world | +---------------+------------------+ [cent@client ~]$ mysql -u serverworld -ppassword -h www.srv.world -e "show variables like 'hostname';" +---------------+------------------+ | Variable_name | Value | +---------------+------------------+ | hostname | node01.srv.world | +---------------+------------------+ |
| Sponsored Link |
|
|