パパエンジニアのポエム

奥さんと娘ちゃんへの愛が止まらない

AWS EC2 に nginx をインストールしリバースプロシキさせる

EC2でPhoenixアプリをグローバルに公開するための設定。

port80を公開する

EC2に適用しているセキュリティグループのインバウンドルールに下記を追加。

タイプ プロトコル ポート範囲 送信元
HTTP TCP 80 0.0.0.0/0

これでポート80をグローバルに公開できる。

nginxのインストー

パッケージ情報確認。

sudo yum info nginx

読み込んだプラグイン:priorities, update-motd, upgrade-helper
利用可能なパッケージ
名前                : nginx
アーキテクチャー    : x86_64
エポック            : 1
バージョン          : 1.10.2
リリース            : 1.30.amzn1
容量                : 534 k
リポジトリー        : amzn-main/latest
要約                : A high performance web server and reverse proxy server
URL                 : http://nginx.org/
ライセンス          : BSD
説明                : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
                    : IMAP protocols, with a strong focus on high concurrency, performance and low
                    : memory usage.

↑のように表示されていれば利用可能なので、インストール。

sudo yum install nginx

インストールされたか確認。

nginx -V

built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) 
built with OpenSSL 1.0.1k-fips 8 Jan 2015

nginx を起動する

sudo nginx

プロセスを確認。

ps -ef | grep nginx

root      9069     1  0 17:43 ?        00:00:00 nginx: master process nginx
nginx     9070  9069  0 17:43 ?        00:00:00 nginx: worker process
ec2-user  9072  8991  0 17:43 pts/0    00:00:00 grep --color=auto nginx

起動している。
EC2のパブリックIPでアクセスできるか確認する。
nginxのデフォルトページが表示されたらおk。

nginx 自動起動設定

sudo service nginx start
sudo chkconfig nginx on
sudo chkconfig --list nginx

nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

これで自動起動設定完了。

nginx config 設定

Phoenixlocalhost:4000をリッスンしている。
それを[パブリックIP]:80でリバースプロシキ。
/etc/nginx/conf.d/local.confが以下。

upstream phoenix {
  server localhost:4000 max_fails=5 fail_timeout=5s;
}

server {
  listen 80;
  server_name [パブリックIP]

  location / {
    allow all;

    # Proxy Headers
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Cluster-Client-Ip $remote_addr;

    # The Important Websocket Bits!
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_pass http://phoenix;
  }
}

パブリックIP文字列が長すぎてハッシュ化出来ないと nginx に怒られたので、 /etc/nginx/nginx.confに下記追加。

server_names_hash_bucket_size 128;

これで準備完了。
あとはPhoenixをデプロイして、Dockerコンテナ内でlocalhost:4000でアプリをホストすればいけるはず。
次回やる。