10
11

More than 5 years have passed since last update.

OSX Yosemite で Rails ローカル開発環境に開発用アドレスでアクセスする

Last updated at Posted at 2015-02-28

Rails をローカル環境で開発する場合、ブラウザから http://localhost:3000/ にアクセスしますが、サブドメインを使用している場合などには多少工夫する必要があります。

dnsmasq や nginx を使用して、開発用ドメインをローカル環境で簡単に運用できる環境を作成します。

Dnsmasq

まず、専用のアドレスでローカルホストにアクセスできるように、dndmasq でローカル DNS を立てます。

$ brew install dnsmasq

設定ファイルを編集します。

/usr/local/etc/dnsmasq.conf
# 以下を追記
address=/.dev/127.0.0.1

OS 起動時に起動するように設定します。

$ sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
$ sudo chown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

.dev アドレスには dnsmasq に問い合わせるように、/etc/resolver/dev ファイルを作成します。

/etc/resolver/dev
nameserver 127.0.0.1

Nginx

バーチャルホストを使用し、*.rails.dev アドレスで rails server につながるように設定します。

$ brew install nginx

Nginx を一般ユーザー権限で動かす場合、 80 番ポートを使用することができません。
nginx の設定で 8080 ポートを指定します。

/usr/local/etc/nginx/nginx.conf
worker_processes  2;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;

    keepalive_timeout  300;

    proxy_set_header Host $http_host;
    proxy_set_header X-Remote-Addr $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 10s;

    gzip  on;

    server {
        listen       8080 default_server;
        server_name  _;

        location / {
            root   html;
            index  index.html;
        }
    }

    include conf.d/*.conf;
}

さらに、バーチャルホスト *.rails.dev を設定します。

/usr/local/etc/nginx/conf.d/rails.conf
upstream rails {
  server 127.0.0.1:3000;
}

server {
  listen 8080;
  server_name rails.dev *.rails.dev;

  location / {
    try_files $uri @app;
  }

  location @app {
    proxy_set_header Host       $host;
    proxy_set_header Upgrade    $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass http://rails;
  }

  error_page 403 404         /404.html;
  error_page 422             /422.html;
  error_page 500 502 503 504 /500.html;
}

OS 起動時に起動するように設定します。

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

80 番アクセスを行う(ブラウザのアドレスバーにポートを指定しない)には、ポートフォワードを行います。

portwoward

pfctl を使用してポート 80 から 8080 へフォワードします。

設定ファイルを作成します。

/etc/pf.anchors/nginx.forwarding
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080
/etc/pf-nginx.conf
rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/nginx.forwarding"

OS 起動時に自動的にポートフォワードを開始するには、LaunchDaemon へ登録します。

plist ファイルをルート権限で作成

/Library/LaunchDaemons/nginx-pfctl.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>nginx-pfctl</string>
  <key>WorkingDirectory</key>
  <string>/var/run</string>
  <key>Program</key>
  <string>/sbin/pfctl</string>
  <key>ProgramArguments</key>
  <array>
    <string>pfctl</string>
    <string>-ef</string>
    <string>/etc/pf-nginx.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

plist をロード

$ launchctl load /Library/LaunchDaemons/nginx-pfctl.plist

直接 80 から 3000 へのポートフォワードをしてもいいのですが、nginx 経由でバーチャルホストを使用し他の開発環境へ対応することができるので、このような設定にしてみました。

確認

適当な rails プロジェクトを rails server で立ち上げ、http://rails.dev/ にブラウザでアクセスしてみましょう。

ところで

Repro株式会社 では一緒に切磋琢磨できる仲間を募集しています! ぜひこちらをごらんください!

10
11
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
10
11