LoginSignup
9
13

More than 5 years have passed since last update.

Dockerでリバースプロキシを構築

Last updated at Posted at 2018-05-18

内容されているTomcatで動くSpring BootをNginxで動かしたいと思ったのをきっかけにリバプロについて学んだ時の備忘録。

リバプロって

リバースプロキシの略。
ユーザにWebアプリを見せるとき代理で応答する機能。主にApacheやNginxで設定できる。

イメージ

New Wireframe 1 (2).png

リバプロを詳しく解説しているサイト

「分かりそう」で「分からない」でも「分かった」気になれるIT用語辞典:
http://wa3.i-3-i.info/word1755.html

リバプロって何がいいの

  • Webサーバーの存在を隠ぺいすることができる
  • アクセスの負荷分散DoSやDDoSなどの一般的なWebベースの攻撃から保護することができる
  • 応答をキャッシュや圧縮することでパフォーマンス向上
  • Webアプリが落ちてもsorryページを表示できる

リバプロのメリットについて詳しく記載しているサイト

90秒の動画で学ぶITキーワード:リバースプロキシ(Reverse Proxy)
http://www.atmarkit.co.jp/ait/articles/1608/25/news034.html

DockerでWebアプリとNginx使ってリバプロ

  • 前提条件

Docker Composeをインストールしていること

  • Nginxでリバプロを実行するには設定ファイル(.conf)が必要
/nginx/conf.d/app.conf
server {
    listen 80;
    # 以下に別にエラーページを設定しておく
    root /usr/share/nginx/html;
    error_page 500 502 503 504 /error.html;
    location /error.html {
        internal;
    }

    location / {
        proxy_pass http://app:8080;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static {
        access_log   off;
        expires      30d;
        alias /app/static;
    }
}
  • Docker Conmponentを作成
docker-compose.yml
version: '3.3'

services:

# リバプロのnginx起動
  nginx:
   container_name: some-nginx
   image: nginx:1.13
   restart: always
   ports:
     - 80:80
     - 443:443
   volumes:
     - ./nginx/conf.d/:/etc/nginx/conf.d
     - ./nginx/html/:/usr/share/nginx/html

# アプリ起動
  app:
    restart: always
    build: . #アプリを起動するDockerFile用意
    ports:
       - "8080"
#      - "8080:8080" Webアプリの8080ポートを隠す
    depends_on:
      - nginx
  • Docker起動
docker-compose up
  • 起動確認
http://localhost

※Spring BootとNginxでリバプロを構築するときに参考にしたサイト

9
13
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
9
13