LoginSignup
2
2

More than 1 year has passed since last update.

Nginxをリバースプロキシとして使ってみた

Posted at

はじめに

以下の記事を参考に試してみた結果の備忘録です。
https://qiita.com/OPySPGcLYpJE0Tc/items/1f4219a51980a75696b5

リバースプロキシサーバーの構築

centos7のdockerコンテナを用意

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

コンテナにnginxをインストール

コンテナにログイン

$ docker exec -it nginx_reverse_proxy /bin/bash

nginxをインストール

# 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

nginxを起動する

# systemctl start nginx

# systemctl status nginx
    ● nginx.service - nginx - high performance web server
    Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
    Active: active (running) since Wed 2021-05-12 14:33:11 UTC; 7s ago
        Docs: http://nginx.org/en/docs/
    Process: 168 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
    Main PID: 169 (nginx)
    CGroup: /docker/ef5c700de13cd3382f585be627278ab4d2bf5263b9867a2d9a3d5e431247512b/system.slice/nginx.service
            ├─169 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx...
            ├─170 nginx: worker process
            └─171 nginx: worker process
            ‣ 169 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx...

    May 12 14:33:11 ef5c700de13c systemd[1]: Starting nginx - high performanc....
    May 12 14:33:11 ef5c700de13c systemd[1]: Started nginx - high performance....
    Hint: Some lines were ellipsized, use -l to show in full.

# curl localhost:80
// nginxのデフォルトのhtmlテンプレートが表示される。省略。

nginxをリバースプロキシとして設定

リバースプロキシとしての設定ファイルを以下のように追加。
※なお最初の3行でキャッシュ機能を有効化させている点も注意すること。

# hostname -i
    172.17.0.2

# vi /etc/nginx/conf.d/backend.conf
    proxy_cache_path /var/cache/nginx/rproxy keys_zone=zone1:1m max_size=1g inactive=24h;
    proxy_temp_path /var/cache/nginx_tmp;
    proxy_ignore_headers Cache-Control;

    server {
        listen 80;
        server_name 172.17.0.2;
        location / {
            proxy_pass http://172.17.0.3:8080;
            proxy_cache zone1;
            proxy_cache_valid 200 302 600s;
            add_header X-Nginx-Cache $upstream_cache_status;
        }
    }

nginxを再起動

# systemctl restart nginx

webサーバーの構築

centos7のdockerコンテナを用意

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

コンテナにhttpdをインストール

dockerコンテナログイン

$ docker exec -it httpd_server /bin/bash

httpdをインストール

# yum -y install httpd

httpdをwebサーバとして設定

8080番ポートに設定。(リバースプロキシnginxのコンテナからアクセスされるようにするため)

# vi /etc/httpd/conf/httpd.conf
    Listen 8080

httpd起動

# systemctl start httpd

# systemctl status httpd

lsofコマンドで確認

# yum -y install lsof

# lsof -i:8080
    COMMAND PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    httpd   171   root    3u  IPv4 697190      0t0  TCP *:webcache (LISTEN)
    httpd   172 apache    3u  IPv4 697190      0t0  TCP *:webcache (LISTEN)
    httpd   173 apache    3u  IPv4 697190      0t0  TCP *:webcache (LISTEN)
    httpd   174 apache    3u  IPv4 697190      0t0  TCP *:webcache (LISTEN)
    httpd   175 apache    3u  IPv4 697190      0t0  TCP *:webcache (LISTEN)
    httpd   176 apache    3u  IPv4 697190      0t0  TCP *:webcache (LISTEN)

webページを配置

# echo "hoge" > /var/www/html/index.html

# curl localhost:8080
    hoge

動作検証

キャッシュ確認

リバースプロキシへアクセスする前に、nginxの設定ファイルで記述したキャッシュ先ファイルを確認しておく。
空である。

# ls /var/cache/nginx/rproxy/

リバースプロキシへアクセス

リバースプロキシへアクセスしてみる。

# curl 172.17.0.2:80
    hoge

キャッシュ確認

# ls /var/cache/nginx/rproxy/
    bea87927112d3594daed0e660e1256e4
# cat /var/cache/nginx/rproxy/bea87927112d3594daed0e660e1256e4
    m�`~�`�`pA)np"5-5c223261fc200"
    KEY: http://172.17.0.3:8080/
    HTTP/1.1 200 OK
    Date: Wed, 12 May 2021 14:54:13 GMT
    Server: Apache/2.4.6 (CentOS)
    Last-Modified: Wed, 12 May 2021 14:51:42 GMT
    ETag: "5-5c223261fc200"
    Accept-Ranges: bytes
    Content-Length: 5
    Connection: close
    Content-Type: text/html; charset=UTF-8

    hoge

アクセスログも確認

webサーバー側(httpd入れた側)のログを確認する。
最初にlocalhostでアクセスしたログと、リバースプロキシを介してアクセスしたログの2つがある。

# tail -f /var/log/httpd/access_log
    127.0.0.1 - - [12/May/2021:14:52:17 +0000] "GET / HTTP/1.1" 200 5 "-" "curl/7.29.0"
    172.17.0.2 - - [12/May/2021:14:54:13 +0000] "GET / HTTP/1.0" 200 5 "-" "curl/7.29.0"

キャッシュが使われていることを確認する

上記のtailコマンド実行した状態で、以下のように2度、3度とリバースプロキシにアクセスしてみる。

# curl 172.17.0.2:80
    hoge

# curl 172.17.0.2:80
    hoge

上記のようにアクセスしても、tailコマンドの反応はない。
つまり、webサーバーにアクセスされていない。
要するにキャッシュが使われていることが考えられる。

確認のためリバースプロキシ側(nginx)のログをみる。
以下のように4つログがあるので、リバースプロキシへはアクセスは行っている。

# cat /var/log/nginx/access.log 
127.0.0.1 - - [12/May/2021:14:35:54 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.2 - - [12/May/2021:14:54:13 +0000] "GET / HTTP/1.1" 200 5 "-" "curl/7.29.0" "-"
172.17.0.2 - - [12/May/2021:15:00:57 +0000] "GET / HTTP/1.1" 200 5 "-" "curl/7.29.0" "-"
172.17.0.2 - - [12/May/2021:15:00:58 +0000] "GET / HTTP/1.1" 200 5 "-" "curl/7.29.0" "-"

以上から、2回目、3回目のリバースプロキシへのアクセスは、リバースプロキシへは届いているが、webサーバーへはアクセスされていないため、リバースプロキシ内のキャッシュが使われているはずであることがわかる。

2
2
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
2
2