LoginSignup
8
5

More than 5 years have passed since last update.

Nginxのビルドバックを使ってBluemix上にリバース・プロキシを構築する

Last updated at Posted at 2016-09-29

はじめに

別々のアプリケーションに対して、クロスオリジン対応などで同じURLでアクセスしたいということがあるかと思います。
これを実現する方法として、Nginxビルドパックを利用してBluemix上にリバース・プロキシを構築する方法を紹介します。

想定する構成は以下の図のとおりです。

overview.png

前提

CloudFoundryのCLIを利用します。CLI未導入の場合は、環境にあったインストーラーをダウンロードページより取得し、導入してください。

設定方法

使用するビルドパック

Nginxのビルドバックとして、Staticfile Buildpackというビルドバックを利用します。Nginx Buildpackもありますが、こちらは非推奨となったため使用しません。

Staticfile Buildpackの使用方法は、Cloud Foundryのドキュメントにも説明が記載されています。
このドキュメントの「Custom Nginx Configuration」に、独自のNginxの設定ファイルを使用するための方法として、以下の内容が記載されています。

You can customize the Nginx configuration further, by adding nginx.conf and mime.types to your root folder. If you specified an alternate root folder, the files must be placed there.

cf pushを実行するディレクトリの直下に、設定ファイル(nginx.conf)をおいておくことで、独自の設定ファイルを使うことができます。

設定ファイルの準備

設定ファイルのサンプルです。

nginx.conf
worker_processes 1;
daemon off;

error_log /home/vcap/app/nginx/logs/error.log;
events { worker_connections 1024; }

http {
  charset utf-8;
  log_format cloudfoundry '$http_x_forwarded_for - $http_referer - [$time_local] "$request" $status $body_bytes_sent';
  access_log /home/vcap/app/nginx/logs/access.log cloudfoundry;
  default_type application/octet-stream;
  include mime.types;
  sendfile on;

  gzip on;
  gzip_disable "msie6";
  gzip_comp_level 6;
  gzip_min_length 1100;
  gzip_buffers 16 8k;
  gzip_proxied any;
  gunzip on;
  gzip_static always;
  gzip_types text/plain text/css text/js text/xml text/javascript application/javascript application/x-javascript application/json application/xml application/xml+rss;

  tcp_nopush on;
  keepalive_timeout 30;
  port_in_redirect off;
  server_tokens off;

  server {
    listen <%= ENV["PORT"] %>;
    server_name localhost;

    location /a {
      proxy_set_header X-Forwarded-Proto https;

      proxy_pass http://nginx-backend-a.mybluemix.net/a;
      proxy_redirect http:// https://;
    }
    location /b {
      proxy_set_header X-Forwarded-Proto https;

      proxy_pass http://nginx-backend-b.mybluemix.net/b;
      proxy_redirect http:// https://;
    }
  }
}

設定ファイルの後半にある「location」の部分が転送設定です。この部分を環境に合わせて修正してください。
その他の設定は、Staticfile Buildpackのデフォルト設定を記載しています。

location /a は、http://nginx-proxy.mybluemix.net/aへのアクセスに対する設定です。
proxy_passとして記載されたhttp://nginx-proxy-sample-backend-a.mybluemix.net/aが転送先のURLとなります。
proxy_set_headerproxy_redirectは、HTTPSアクセスの転送に関する設定となります。

Bluemixへのデプロイ

/work/nginxなど作業用のディレクトリを作成し、そのディレクトリの直下に設定ファイルnginx.confを配置してください。
コマンドを実行するためのターミナルを立ち上げ、作業用ディレクトリに移動後、以下のコマンドを実行してください。アプリケーション名やホスト名、メモリ量は環境に応じた値をセットしてください。

$ cf push アプリケーション名 -n ホスト名 -b https://github.com/cloudfoundry/staticfile-buildpack -m メモリ量

リバース・プロキシの構築は以上です。実際にURLにアクセスして稼働確認を実施してください。

8
5
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
8
5