nginxでSSL(オレオレ自己証明)
自己証明書を使ったSSL対応サイトを作りました。
環境
・さくらVPS
・Debian 8.6
・nginx 1.6.2
まずはVirtualhostの設定をします。
# vi /etc/nginx/conf.d/default.conf
中身はこんな感じで。server_nameは自分で設定したドメイン名を入れてください。
server {
listen 80;
server_name www.hoge.com;
location / {
root /var/www/virtualhost;
index index.html index.htm;
}
}
server {
listen 80;
server_name hoge.com;
location / {
root /var/www/virtualhost;
index index.html index.htm;
}
}
設定ファイルが適切に記述できているかを確認します。下記がでればOK。どこかのをコピペして持ってくると半角スペースが全角になっていてエラーが出る時があるので注意。
# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
上のrootで指定した場所(ここでは/var/www/virtualhost)にindex.htmlを置けば完了です。設定を反映させるためにnginxを再起動します。
# /etc/init.d/nginx stop
# /etc/init.d/nginx start
さて、本題のオレオレ証明書の作成です。といっても難しくはありません。
まずは、秘密鍵 (Private Key):server.keyを作成
$ openssl genrsa 2048 > server.key
次に、証明書署名要求 (CSR):server.csrを作成
$ openssl req -new -key server.key > server.csr
いろいろと聞かれるので答えます。Common Nameに自分のドメイン名を入れるのを注意しましょう。
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
そして、サーバ証明書(CRT):server.crtを作成
$ openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt
自分がいるディレクトリにserver.key、server.csr、server.crtが作られているのを確認しましょう。
後はSSLの設定を追記すれば完了です。
# vi /etc/nginx/conf.d/ssl.conf
中身はこんな感じ。server.crtとserver.keyの場所をちゃんと指定してあげて下さい。
server {
listen 443 ssl;
server_name www.hoge.com;
ssl_certificate /etc/nginx/conf.d/server.crt;
ssl_certificate_key /etc/nginx/conf.d/server.key;
}
server {
listen 443 ssl;
server_name hoge.com;
ssl_certificate /etc/nginx/conf.d/server.crt;
ssl_certificate_key /etc/nginx/conf.d/server.key;
}
最後に、設定ファイルのチェックとnginxの再起動をします。
# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# /etc/init.d/nginx stop
# /etc/init.d/nginx start
以上で完了です。お疲れ様でした。
参考になったサイト