リバース プロキシーの学習メモ
この記事について
リバース プロキシーの知識が必要になったので、感覚をつかむために自宅環境で NGINX を使用して実際に構築したときのメモです。
※ この記事には続きがあります※
構成
行いたいこと
-
http://192.168.100.101/
で node2 のページを表示する -
http://192.168.100.101/node3/
で node3 のページを表示する -
http://192.168.100.101/node4/
で node4 のページを表示する
node1 ~ node 4 の作成
- Oracle VirtualBox + Vagrant で作成
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
(1..4).each do |i|
config.vm.define "node#{i}" do |node|
node.vm.network "public_network", ip: "192.168.100.10#{i}"
node.vm.box = "almalinux/9"
node.vm.hostname = "node#{i}.example.jp"
node.vm.provider "virtualbox" do |vb|
vb.name = "node#{i}"
vb.memory = "2048"
vb.cpus = 2
vb.gui = true
vb.customize [
"modifyvm", :id,
"--vram", "16",
"--graphicscontroller", "vmsvga",
"--nicpromisc2", "allow-all",
"--description", "AlmaLinux9"
]
end
node.vm.provision "shell", inline: $alma_commands_list
end
end
end
$alma_commands_list = <<-'SCRIPT'
timedatectl set-timezone Asia/Tokyo
UNAME='y_mrok'
UPASS='pass+789'
useradd ${UNAME}
echo "${UPASS}" | passwd --stdin ${UNAME}
cat <<EOF > /etc/sudoers.d/${UNAME}
${UNAME} ALL=(ALL) NOPASSWD:ALL
EOF
chmod 0440 /etc/sudoers.d/${UNAME}
SCRIPT
- 作成後は各ノードに ssh ログインして、全体をアップデート
sudo dnf -y update
sudo systemctl reboot
node2 ~ node4 の準備
- 各ノードに SSH でログイン
- Apache のインストールとサービスの起動
sudo dnf -y install httpd
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
- node2 ~ node4 の
/var/www/html/index.html
ファイルに以下の内容を記述
■ node2
node2
■ node3
node3
■ node4
node4
node1 の準備
以下のページを参照しながら NGINX をインストールします。
-
node1 に ssh でログイン
-
/etc/yum.repos.d/nginx.repo
ファイルを作成
sudo vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
- Apache のインストールとサービスの起動
sudo dnf -y install nginx
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
NGINX には stable 版と mainline 版があり、上記リポジトリでは stable 版がインストールされます(しました)。
プロキシ サーバー用の設定
対象ファイル
/etc/nginx/conf.d/default.conf
/ で表示されるページの無効化
http://192.168.100.101/
で表示されるページの設定をコメント化します。
- 以下の行をすべてコメント化
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
- コメント化後の内容
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
各ページ用の設定
- 上記でコメント化した行の後に、以下の内容を追加
location / {
proxy_pass http://192.168.100.102:80/;
}
location /node3/ {
proxy_pass http://192.168.100.103:80/;
}
location /node4/ {
proxy_pass http://192.168.100.104:80/;
}
-
/etc/nginx/conf.d/default.conf
ファイルの全体
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
location / {
proxy_pass http://192.168.100.102:80/;
}
location /node3/ {
proxy_pass http://192.168.100.103:80/;
}
location /node4/ {
proxy_pass http://192.168.100.104:80/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
SELinux の設定変更
nginx.service をリスタートしてページが表示できれば良いのですが、正しく表示できないときは /var/log/nginx/error.log
ファイルに以下のようなログが出力されていないか確認します。
2025/01/11 16:17:50 [crit] 4264#4264: *1 connect() to 192.168.100.102:80 failed (13: Permission denied) while connecting to upstream, client: 192.168.100.16, server: localhost, request: "GET /node2/ HTTP/1.1", upstream: "http://192.168.100.102:80/", host: "192.168.100.101"
Goolgle で検索したところ SELinux が設定が原因のようです。SELinux 自体を無効化するか、以下のコマンドを実行します。
sudo setsebool httpd_can_network_connect on -P
httpd_can_network_connect
以外にどのような値があるか確認したい場合は以下のコマンドを実行してください。
sudo getsebool -a
nginx.service のリスタート
すべての設定の終了後に nginx.service をリスタートします。
sudo systemctl restart nginx.service
クライアント PC からの表示テスト
■ http://192.168.100.101/
■ http://192.168.100.101/node3/
■ http://192.168.100.101/node4/
感想
SELinux のところでつまづきましたが、それ以外はすんなりと進みました。最初に思っていた以上に簡単に作成できたなと思います。設定できる項目は多々あるので、もう少し触って見ようと思います。
Discussion