LoginSignup
28
39

More than 5 years have passed since last update.

DockerでNginxイメージを利用し、リバースプロキシを構築してみる

Posted at

Dockerイメージ「jwilder/nginx-proxy」が何がなんだかよくわからずで、下記の記事にたどり着きました。

【作って覚えるリバースプロキシ | 東北ギーク】
http://tech.respect-pal.jp/reverse_proxy_cooking/

リバースプロキシをとりあえず試してみたいという方にはおすすめの記事です!こちらの記事を参考に実際に私も実験してみました。
(※私なりにところどころ内容を変えて試していますのはご了承ください)

ディレクトリ構造

nginx1、nginx2のディレクトリにブラウザに表示するNginxコンテナ用のdocker-compose.ymlを配置、proxyディレクトリにリバースプロキシ用のdocker-compose.ymlを配置します。

Githubにアップロードしておきましたので、こちらからファイル一式ダウンロードできます。
https://github.com/Vc2R6SJeCYR7/nginx-reverse-proxy

reverse-proxy
├─nginx1
│  └─etc
│      └─nginx
│          └─conf.d
├─nginx2
│  ├─etc
│  │  └─nginx
│  │      └─conf.d
│  └─html
└─proxy
    ├─etc
    │  └─nginx
    │      └─conf.d
    └─html

ネットワーク作成

コンテナ間で通信できるようにするため、ネットワークを作成します。

$ docker network create --driver bridge shared

Nginxのコンテナ

ブラウザに表示する「nginx1」と「nginx2」は下記のようにします。

nginx1のコンテナ


version: '3'
services:

  nginx1:
    image: nginx:latest
    container_name: 'nginx1'
    volumes:
      - ./html:/usr/share/nginx/html
      - ./etc/nginx/conf.d/:/etc/nginx/conf.d
    environment:
      VIRTUAL_HOST: 'blog1.nginx.com'

networks:
  default:
    external:
      name: shared



nginx2のコンテナ(nginx1と同じ)



version: '3'
services:

  nginx2:
    image: nginx:latest
    container_name: 'nginx2'
    volumes:
      - ./html:/usr/share/nginx/html
      - ./etc/nginx/conf.d/:/etc/nginx/conf.d
    environment:
      VIRTUAL_HOST: 'blog2.nginx.com'

networks:
  default:
    external:
      name: shared

リバースプロキシコンテナ

docker-compose.yml

リバースプロキシ用のnginxコンテナは下記のファイルを使用して立ち上げます。


version: '3'
services:

  proxy:
    image: nginx:latest
    container_name: 'proxy'
    privileged: true
    volumes:
      - ./html:/usr/share/nginx/html
      - ./etc/nginx/conf.d/:/etc/nginx/conf.d
    ports:
      - "80:80"
      - "443:443"
    restart: always

networks:
  default:
    external:
      name: shared

default.conf

「nginx1」と「nginx2」をドメインによって表示を振り分ける設定が必要になりますので、下記のファイルのように作成します。


/reverse-proxy/proxy/etc/nginx/con.d/default.conf

server {
    listen       80;
    server_name  blog1.nginx.com;


    location / {
        proxy_pass   http://nginx1:8080;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}



server {
    listen       80;
    server_name  blog2.nginx.com;

    location / {
        proxy_pass   http://nginx2:8080;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}


コンテナ立ち上げ

各ディレクトリでコンテナを立ち上げます。


/reverse-proxy/nginx1 -> docker-compose up -d
/reverse-proxy/nginx2 -> docker-compose up -d
/reverse-proxy/proxy -> docker-compose up -d

ブラウザ表示確認

これで、マルチドメインで「nginx1」と「nginx2」のコンテナにアクセスできるようになりました。

MacやWindowsをhostsgファイルなどで、ipアドレスとドメインの設定をし、ブラウザで「blog1.nginx.com」と「blog2.nginx.com」にアクセスすると、nginxの表示ディレクトリが見えるかと思います。

リバースプロキシすごい。

参考

【jwilder/nginx-proxy - Docker Hub】
https://hub.docker.com/r/jwilder/nginx-proxy

【作って覚えるリバースプロキシ | 東北ギーク】
http://tech.respect-pal.jp/reverse_proxy_cooking/

28
39
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
28
39