Time to Read
30分
ゴール
Nginxを、PassengerとSSLを有効にした状態でコンパイルし、配備する。いっしょに、自分CAで署名したSSL証明書も作って準備してみる。
Nginxを、SSL、passenger込みでコンパイルする
Nginx+Passengerであれば、通常 passenger-install-nginx-module を利用すると思うが、何も考えていないとSSLが有効化されないでコンパイルされる。今回、SSLを有功にしてコンパイルする手順をメモした。
最初に、ソース一式を持ってくる必要がある。
1 2 3 | wget http://nginx.org/download/nginx-0.7.67.tar.gz tar xzf nginx-0.7.67.tar.gz sudo passenger-install-nginx-module |
Do you want this installer to download, compile and install Nginx for you?
1. Yes: download, compile and install Nginx for me. (recommended)
The easiest way to get started. A stock Nginx 0.7.67 with Passenger
support, but with no other additional third party modules, will be
installed for you to a directory of your choice.
2. No: I want to customize my Nginx installation. (for advanced users)
Choose this if you want to compile Nginx with more third party modules
besides Passenger, or if you need to pass additional options to Nginx's
'configure' script. This installer will 1) ask you for the location of
the Nginx source code, 2) run the 'configure' script according to your
instructions, and 3) run 'make install'.
Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.
Enter your choice (1 or 2) or press Ctrl-C to abort:
普通うっかり「1」を選んでしまうけど、ここでは「2」。
Where is your Nginx source code located?
さっき落としたコード一式のディレクトリを指定。 /home/udzura/nginx-0.7.67
とかそんな感じでしょう。
Where do you want to install Nginx to?
デフォルトでいいと思う。そうすると、 /opt/nginx
。
Extra Nginx configure options
ここで、SSLを有効にするために --with-http_ssl_module
を指定する(参考: NginxHttpSslModule)。
./configure --prefix='/opt/nginx' --add-module='/usr/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/nginx' --with-http_ssl_module
こうなればOK。Is this what you want? (yes/no)
に「yes」と答えよう。
あとは、コンピューターさんが勝手にコンパイルしてくれる。デーモン実行ファイルは、 /opt/nginx/sbin/nginx
になるはず。
SSL証明書を、自分で署名して作成
こちらで解説されている手順に、追加することはない。素晴らしく詳細な解説なのでご参照あれ。
一点補足すると、 CA.sh
は、Ubuntuでは /usr/lib/ssl/misc/
以下にある。 find / -name CA.sh
して探してしまったよ。。。
NginxのSSL設定
上記サイト、最後の設定がapacheのものなので、たとえば /opt/nginx/cert
とかそういうディレクトリに cert.pem と servernopasskey.pam を置いといて、設定を以下のように。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | http { server { listen 443; server_name secure.local.udzura.jp; ssl on; # ※ nginx.confのある場所からの相対パス ssl_certificate ../cert/cert.pem; ssl_certificate_key ../cert/serverkey.pem; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; passenger_enabled on; rails_env development; root /var/rails/secure_test/public; } } |
と言うか、まあ、コメントアウトされてるやつをほぼそのまま使えばよろしい。
initスクリプトは、公式wikiに説明のあるファイルを適切に設定して使えばいいと思う。
早速 sudo service nginx start
して、
1 2 3 4 | <h1>Hello SSL.</h1> <p> <%= request.ssl? rescue "error" %> </p> |
こういうやる気のない index.html.erb を作って、アクセスしてみよう。
当然こうなるので、例外を承認して。。。
SSLでアクセスできた~。
(分かっていらっしゃると思うけれど、 secure.local.udzura.jp は、自分のマシンの/etc/hostsをいじってローカルに向けています。)
総括
「RailsではSSLのサーバ設定難しい」と思ってたんですけど、Nginxだと簡単だった。ssl_requrement とかも検証し易くなりますね。