目的
リバースプロキシとしてnginxを利用して、HTTPリクエストを2台のWEBサーバに送る
環境
GCEで3台サーバを用意。※OSはCentOS7.4
1.nginxのWEBサーバ
2.apacheのWEBサーバ(複製用)
3.nginxのリバプロサーバ
手順
nginxのWEBサーバ
nginxをyumでインストール。
[root@nginx-Web ~]# yum install nginx
セキュリティは完全無視でとりあえず起動する状態までconfファイルを削って起動する。
[root@nginx-Web ~]# vi /etc/nginx/nginx.conf
[root@nginx-Web ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx-Web ~]# systemctl start nginx
nginx.confはこんな感じ
events {
worker_connections 1024;
}
http{
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server{
listen 80 default;
server_name localhost;
access_log /var/log/nginx/nginx-Web-access.log;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
ドキュメントルートにindex.htmlをおいて『nginxのWEBサーバのIP』にアクセスしてみる
ApacheのWEBサーバ
yumでインストールしてnginxと同じようにセキュリティ完全無視でとりあえず起動できる状態までconfファイルを削って起動する。
[root@apache-Web ~]# yum install httpd
[root@apache-Web ~]# vi /etc/httpd/conf/httpd.conf
[root@apache-Web ~]# systemctl start httpd
httpd.confはこんな感じ
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
DirectoryIndex index.html
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog "/var/log/httpd/apache-web-access.log" combined
TypesConfig /etc/mime.types
index.htmlをおいて『ApacheのWEBサーバのIP』にアクセスしてみる
nginxのリバプロサーバ
yumでインストールして、WEBサーバ用に作成したconfファイルをベースにリバプロの設定を追加して起動する。
[root@nginx-proxy ~]# yum -y install
[root@nginx-proxy ~]# vi /etc/nginx/nginx.conf
[root@nginx-proxy ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx.confはこんな感じ
events {
worker_connections 1024;
}
http{
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server{
listen 80 default;
server_name localhost;
access_log /var/log/nginx/nginx-proxy-access.log;
location / {
proxy_pass http://nginxのWEBサーバのIP; #これでリバプロの設定
}
}
}
リバースプロキシだけの設定はこれで完了。
『nginxのリバプロサーバのIP』にアクセスしてさっきのnginxのWEBサーバの画面が表示されるか確認する。
リクエストを複製する設定を追加する。
[root@nginx-proxy ~]# vi /etc/nginx/nginx.conf
[root@nginx-proxy ~]# systemctl restart nginx
nginx.confはこんな感じ
events {
worker_connections 1024;
}
http{
server{
listen 80 default;
server_name localhost;
access_log /var/log/nginx/admin_access.log;
location / {
proxy_pass http://nginxのWEBサーバのIP;
post_action @testmirror; #複製先の設定を読み込む
}
#ここが複製の設定
location @testmirror{
internal;
proxy_pass http://apacheのWEBサーバのIP;
}
}
}
実際にアクセスして、アクセスログを見てみる。
アクセスした端末IP - - [24/Feb/2018:08:37:41 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"
nginxのリバプロサーバのIP - - [24/Feb/2018:08:37:41 +0000] "GET / HTTP/1.0" 200 27 "-" "Mozilla/5.0(Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0" "
-"
nginxのリバプロサーバのIP - - [24/Feb/2018:08:37:41 +0000] "GET / HTTP/1.0" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"
-"
以上で終わり!!
次はセキュリティを強化するかAPサーバを導入して簡単なアプリが動くように設定してみようかなと。
初めてのnginxよりも初めてのMarkdownに一番悪戦苦闘した。