【問題】
Node.jsやRailsでサービスつくってドメインとっても、そのドメインのアクセスをApacheで捌いてNodeやRailsが動いてるポートへ流さないと意味ない。
その設定方法いつも忘れるから備忘録。
【ゴール】
あるIPのサーバにホスト名 anicatch.net でHTTPリクエストがあったときに、そのIPのサーバの3001番で動いてるNode.jsにリクエストを振る。
【解決】
1. お名前どっとこむで取得したホスト名とIPアドレスをDNSサーバに登録して名前解決できるようにする。
さくらVPSにお名前.comの独自ドメインを割り当てる
2. Apache2のmod_proxyを有効にする
3. Apache2のconfでproxyの設定をする
Apacheのプロキシを使ってMongrelのポート(3000番)を80番に転送
↑の記録
% sudo a2enmod proxy perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "ja_JP.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). Enabling module proxy. Run '/etc/init.d/apache2 restart' to activate new configuration! % sudo a2enmod proxy_http perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "ja_JP.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). Considering dependency proxy for proxy_http: Module proxy already enabled Module proxy_http already enabled % sudo a2enmod proxy_connect perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "ja_JP.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). Considering dependency proxy for proxy_connect: Module proxy already enabled Enabling module proxy_connect. Run '/etc/init.d/apache2 restart' to activate new configuration!
ここで、a2enmod proxy_httpをしたときだけ「Module proxy_http already enabled」がすでに出てるだけなのは、すでに下記コマンドを試したあとだったから
% cd /etc/apache2/mods-enabled % sudo ln -s ../mods-available/proxy_http proxy_http
ということはつまりa2enmodって、ln -sしてるだけ?かもね
で、/etc/apache2/mods-available/proxy.confの下記の部分のコメントアウトを外す
<Proxy *> AddDefaultCharset off Order deny,allow Deny from all Allow from all #Allow from .example.com </Proxy>
つづいて/etc/apache2/httpd.confに以下のような修正を加える
<VirtualHost *:80> ServerName anicatch.net ServerAdmin webmaster@anicatch.net ProxyRequests off ProxyPass / http://otiai10.com:3001/ <Location /> ProxyPassReverse http://otiai10.com:3001/ ProxyPassReverseCookieDomain localhost anicatch.net ProxyPassReverseCookiePath / / Order allow,deny Allow from all </Location> </VirtualHost>
最後にapacheをrestartする
% sudo /etc/init.d/apache2 restart Restarting web server: apache2 ... waiting .