LoginSignup
2
0

More than 5 years have passed since last update.

docker-composeを使ってリバースプロキシサーバからドメインアクセスしたい

Posted at

docker-compose version 1.23.2

docker-composeを使って、webサーバを立ち上げた時にvertualhostでアクセスしたいと思い、調べていたらnginx-proxyを見つけ、複数サーバからドメインアクセスできそうだったのでやってみた。

docker image:https://hub.docker.com/r/jwilder/nginx-proxy/

大まかなイメージ図
docker-composeを使ってリバースプロキシサーバから複数webサーバを呼び出したい .jpg

ディレクトリ構成

nginx-proxy
├── docker-compose.yml
├── web1.localhost
│   ├── config
│   │   └── web1
│   │       └── default.conf
│   ├── docker-compose.yml
│   └── web1
│       └── index.html
└── web2.localhost
    ├── config
    │   └── web2
    │       └── default.conf
    ├── docker-compose.yml
    └── web2
        └── index2.html

ディレクトリは各々作成お願いします。

リバースプロキシサーバの設定

nginx-proxy/docker-compose.yml
version: '3'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
networks:
  default:
    external:
      name: shared

WEBサーバの設定

nginx-proxy/web2.localhost/docker-compose.ymlも同様に作成する

nginx-proxy/web1.localhost/docker-compose.yml
version: '3'

services:
  web1:
    image: nginx
    environment:
      - VIRTUAL_HOST=web1.localhost
    volumes:
      - ./config/web1/default.conf:/etc/nginx/conf.d/default.conf
      - ./web1:/var/www/html
networks:
  default:
    external:
      name: shared

nginxの設定

nginx-proxy/web2.localhost/config/web2/default.confも同様に作成する

nginx-proxy/web1.localhost/config/web1/default.conf
server {
    listen 80;

    root  /var/www/html/;
    index index.php index.html index.htm;
}

docker ネットワークの作成

docker network create --driver bridge shared

適当に入力
nginx-proxy/web1.localhost/web1/index.html
nginx-proxy/web2.localhost/web2/index2.html

docker-compose 起動

nginx-proxy
docker-compose up -d
nginx-proxy/web1.localhost
docker-compose up -d
nginx-proxy/web2.localhost
docker-compose up -d

http://web1.localhost/
http://web2.localhost/

アクセスして、設定したhtmlが表示されていればOK

お疲れ様でした。。。

余裕があればhttps接続の方法も調べたい^ ^

参考文献:
https://suin.io/561
https://gtrt7.com/blog/nginx/reverseproxy
https://qiita.com/fukafuka_dev/items/05d8dc4cc4671d7a5d4f

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