LoginSignup
0
0

More than 3 years have passed since last update.

Docker-Composeで実行中のアプリケーションをホスト側OSのwebサーバーで待受けリバースプロキシでフォワードする

Last updated at Posted at 2019-10-01

アプリケーションサーバをDocker(docker-compose)で構築して
ホスト側のWebサーバーからリバースプロキシでDockerアプリケーションにフォワードする方法

1. Dockerで構築した10080ポートで実行しているWebアプリケーション

ホストOSの中で http://localhost:10080 で動作するWebアプリケーションが
実行中だとします。

それをホストOS側のnginxでリバースプロキシでフォワードしたい場合のnginx.confの設定

nginx.conf

http  {

    ...

    server {
        # 待受ポート
        listen 80;
        listen [::]:80;
        # 独自ドメイン
        server_name your-domain.com

        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-Host  $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header "special-header";
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        # 当該設定のドキュメントルートを設定
        root /var/www/html/public_html_application;
        # 以下の記述でプロキシ設定を実行する
        location / {
            proxy_pass http://localhost:10080 ;
            # 非公開アプリケーションのため nginxでbasic認証を実装する
            # .htpasswdは任意の方法で生成する
            auth_basic "basic  authentication";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }
        # リバースプロキシは行うものの、特定のパターンのURLのみ特定ページへリダイレクトさせたい場合
        # apacheのmod_rewrite機能と同等機能
        rewrite /special_pattern/(.*)  /replace/ last;
    }

    ...

}


以上が、 80ポートで待ち受けるアプリケーションの設定
次に 443ポート(SSL)で待ち受けるアプリケーションの設定

2. Dockerで構築した3000ポートかつhttp2で実行しているWebアプリケーション

nginx.conf

http {

    ...

    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  your-domain.com;
        root         /var/www/html/your-domain.com;

        # フォワードさせるアプリケーションが利用している ssl接続の証明書のパスを
        # 以下のディレクトリで指定する
        ssl_certificate "/var/www/your-domain.com/server.crt";
        ssl_certificate_key "/var/www/your-domain.com/server.key";
        # 以下のディテクティブはなくても動作するが、未調査のため各値がどう作用するか不明
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass https://localhost:3000;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            # 非公開アプリケーションのため nginxでbasic認証を実装する
            # .htpasswdは任意の方法で生成する
            auth_basic "basic  authentication";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }
    }

    ...

}
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