LoginSignup
0
0

More than 1 year has passed since last update.

NginxをProxyサーバとして動かす

Posted at

はじめに

※ 以下の記事をほぼほぼ参考に試してみた備忘録記事です。
https://qiita.com/hana_shin/items/31232789f4a6510e936b

準備

proxy兼webサーバーの役割を担うコンテナを作成

nginxがproxyの役割を、apache(httpd)がwebサーバの役割を担う想定。

$ docker run -it -d --privileged --name nginx_proxy_and_httpd_server centos:centos7 /sbin/init

nginxとhttpdをインストールする。

$ docker exec -it nginx_proxy_and_httpd_server /bin/bash

# vi /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1

# yum -y install nginx

# yum -y install httpd

webサーバーだけの役割を担うコンテナを作成

$ docker run -it -d --privileged --name httpd_server centos:centos7 /sbin/init

httpdをインストールする。

$ docker exec -it httpd_server /bin/bash

# yum -y install httpd

NginxのProxyとしての設定

各コンテナのIPだけ調べておく。

Nginxが入ったProxy兼務のコンテナ側

# hostname -i
172.17.0.2

httpdだけ入ったwebサーバとしての役割だけのコンテナ側

# hostname -i
172.17.0.3

nginx設定ファイル作成。

proxyの設定。

※ /etc/nginx/conf.d/ 配下に「*.conf」の名前でファイルを置いておくと、
元の/etc/nginx/nginx.confファイルから読み込んでもらえるので便利。

# vi /etc/nginx/conf.d/backend.conf
    upstream backend {
        server 172.17.0.2:80 weight=1;
        server 172.17.0.3:80 weight=1;
    }

    server {
        listen 8888;
        server_name 172.17.0.2;

        location / {
            proxy_pass http://backend;
        }
    }

その他の設定

nginx(Proxy)のポート番号の変更。

# vi /etc/nginx/conf.d/default.conf
    server {
        #listen       80;
        listen      8888;

各ソフトの起動

# systemctl start httpd

# systemctl start nginx

※webサーバーだけのコンテナではhttpdだけ起動する。

# systemctl start httpd

各ソフト起動状態確認

proxy兼webサーバー側コンテナのソフト起動状態確認。

# yum -y install lsof

# lsof -i:8888
    COMMAND PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx   358  root    6u  IPv4 339580      0t0  TCP *:ddi-tcp-1 (LISTEN)
    nginx   359 nginx    6u  IPv4 339580      0t0  TCP *:ddi-tcp-1 (LISTEN)
    nginx   360 nginx    6u  IPv4 339580      0t0  TCP *:ddi-tcp-1 (LISTEN)

# lsof -i:80
    COMMAND PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    httpd   261   root    3u  IPv4 656260      0t0  TCP *:http (LISTEN)
    httpd   262 apache    3u  IPv4 656260      0t0  TCP *:http (LISTEN)
    httpd   263 apache    3u  IPv4 656260      0t0  TCP *:http (LISTEN)
    httpd   264 apache    3u  IPv4 656260      0t0  TCP *:http (LISTEN)
    httpd   265 apache    3u  IPv4 656260      0t0  TCP *:http (LISTEN)
    httpd   266 apache    3u  IPv4 656260      0t0  TCP *:http (LISTEN)

webサーバーのみのコンテナのソフト起動状態確認。

# yum -y install lsof

# lsof -i:80
    COMMAND PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    httpd   176   root    3u  IPv4 657417      0t0  TCP *:http (LISTEN)
    httpd   177 apache    3u  IPv4 657417      0t0  TCP *:http (LISTEN)
    httpd   178 apache    3u  IPv4 657417      0t0  TCP *:http (LISTEN)
    httpd   179 apache    3u  IPv4 657417      0t0  TCP *:http (LISTEN)
    httpd   180 apache    3u  IPv4 657417      0t0  TCP *:http (LISTEN)
    httpd   181 apache    3u  IPv4 657417      0t0  TCP *:http (LISTEN)

動作確認

proxy兼webサーバー側コンテナのログ確認

# cat /var/log/nginx/access.log

# cat /var/log/httpd/access_log

webサーバーのみのコンテナのログ確認

# cat /var/log/httpd/access_log

proxy兼webサーバー側コンテナからproxyへアクセス

curl http://172.17.0.2:8888

proxy兼webサーバー側コンテナのログ確認

# cat /var/log/nginx/access.log
172.17.0.2 - - [11/May/2021:14:49:35 +0000] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0" "-"

# cat /var/log/httpd/access_log
172.17.0.2 - - [11/May/2021:14:49:35 +0000] "GET / HTTP/1.0" 403 4897 "-" "curl/7.29.0"

webサーバーのみのコンテナのログ確認

# cat /var/log/httpd/access_log

つまりは、今回のアクセスではproxy兼webサーバー側コンテナのwebサーバーへアクセスがいったということ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0