はじめに
WordPress を動かしているリバースプロキシとWebサーバの Nginx で、Let’s Encrypt と Certbot で SSL/TLS の発行、導入をしました記録♪ – oki2a24 にて SSL/TLS を導入しました。
その際、Certbot によって Nginx の設定ファイルが書き換えられました。具体的には certbot --nginx コマンドを実行することで、/etc/nginx/conf.d/default.conf ファイルが書き換えられました。
今回は、その内容を見てみます。
Certbot によって書き換えられる前の /etc/nginx/conf.d/default.conf
- 最初の server ディレクティブがリバースプロキシ設定
- 次の server ディレクティブがWebサーバ設定
root /srv/wordpress;
index index.php index.html index.htm;
proxy_cache_path /var/cache/nginx keys_zone=czone:32m levels=1:2 inactive=3d max_size=256m;
proxy_cache czone;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_valid 200 404 1d;
server {
listen 80 default_server;
location ~ /\. {deny all; access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
log_not_found off;
proxy_pass http://unix:/var/run/nginx.sock;
}
set $do_not_cache 0;
if ($request_method != "GET") {
set $do_not_cache 1;
}
if ($uri ~* "\.php$") {
set $do_not_cache 1;
}
if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
set $do_not_cache 1;
}
location / {
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_cache_key "$scheme://$host$request_uri";
proxy_pass http://unix:/var/run/nginx.sock;
}
}
server {
listen unix:/var/run/nginx.sock;
try_files $uri $uri/ /index.php?q=$uri&$args;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass_header "X-Accel-Redirect";
fastcgi_pass_header "X-Accel-Buffering";
fastcgi_pass_header "X-Accel-Charset";
fastcgi_pass_header "X-Accel-Expires";
fastcgi_pass_header "X-Accel-Limit-Rate";
}
}
Certbot によって書き換えられた後の /etc/nginx/conf.d/default.conf
- server ディレクティブが、書き換え前は 2 つだったのが、4 つになった。
- server ディレクティブは、最後の 2 つが Certbot によって追加された。
root /srv/wordpress;
index index.php index.html index.htm;
proxy_cache_path /var/cache/nginx keys_zone=czone:32m levels=1:2 inactive=3d max_size=256m;
proxy_cache czone;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_valid 200 404 1d;
server {
listen 80 default_server;
location ~ /\. {deny all; access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
log_not_found off;
proxy_pass http://unix:/var/run/nginx.sock;
}
set $do_not_cache 0;
if ($request_method != "GET") {
set $do_not_cache 1;
}
if ($uri ~* "\.php$") {
set $do_not_cache 1;
}
if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
set $do_not_cache 1;
}
location / {
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_cache_key "$scheme://$host$request_uri";
proxy_pass http://unix:/var/run/nginx.sock;
}
}
server {
listen unix:/var/run/nginx.sock;
try_files $uri $uri/ /index.php?q=$uri&$args;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass_header "X-Accel-Redirect";
fastcgi_pass_header "X-Accel-Buffering";
fastcgi_pass_header "X-Accel-Charset";
fastcgi_pass_header "X-Accel-Expires";
fastcgi_pass_header "X-Accel-Limit-Rate";
}
}
server {
location ~ /\. {deny all; access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
log_not_found off;
proxy_pass http://unix:/var/run/nginx.sock;
}
set $do_not_cache 0;
if ($request_method != "GET") {
set $do_not_cache 1;
}
if ($uri ~* "\.php$") {
set $do_not_cache 1;
}
if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
set $do_not_cache 1;
}
location / {
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_cache_key "$scheme://$host$request_uri";
proxy_pass http://unix:/var/run/nginx.sock;
}
server_name oki2a24.com; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/oki2a24.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/oki2a24.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = oki2a24.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
server_name oki2a24.com;
return 404; # managed by Certbot
}
Certbot によって 書き換えられた前後の /etc/nginx/conf.d/default.conf の差分
- 追加された 1 つめの server は SSL/TLS のWebサーバ設定
- HTTP のWebサーバ設定をコピーしたものに、SSL/TLS 設定を追記したもの
- さらに、Certbot によって追加された設定ファイルを読み込んでいる
- 追加された 2 つめの server は HTTP のWebサーバ設定
- 既存の HTTP 設定を上書きしている。???
- HTTP へのアクセスを HTTPS へリダイレクトしている。
# diff -up default.conf.before default.conf
--- default.conf.before 2018-06-30 13:26:40.985797113 +0900
+++ default.conf 2018-06-22 13:35:28.717365000 +0900
@@ -60,3 +60,59 @@ server {
fastcgi_pass_header "X-Accel-Limit-Rate";
}
}
+
+
+server {
+
+ location ~ /\. {deny all; access_log off; log_not_found off; }
+ location = /robots.txt { access_log off; log_not_found off; }
+ location = /favicon.ico { access_log off; log_not_found off; }
+ location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
+ log_not_found off;
+ proxy_pass http://unix:/var/run/nginx.sock;
+ }
+
+ set $do_not_cache 0;
+
+ if ($request_method != "GET") {
+ set $do_not_cache 1;
+ }
+
+ if ($uri ~* "\.php$") {
+ set $do_not_cache 1;
+ }
+
+ if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
+ set $do_not_cache 1;
+ }
+
+ location / {
+ proxy_no_cache $do_not_cache;
+ proxy_cache_bypass $do_not_cache;
+ proxy_cache_key "$scheme://$host$request_uri";
+ proxy_pass http://unix:/var/run/nginx.sock;
+ }
+
+
+ server_name oki2a24.com; # managed by Certbot
+
+ listen 443 ssl; # managed by Certbot
+ ssl_certificate /etc/letsencrypt/live/oki2a24.com/fullchain.pem; # managed by Certbot
+ ssl_certificate_key /etc/letsencrypt/live/oki2a24.com/privkey.pem; # managed by Certbot
+ include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
+ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
+
+}
+
+server {
+ if ($host = oki2a24.com) {
+ return 301 https://$host$request_uri;
+ } # managed by Certbot
+
+
+ listen 80 ;
+ server_name oki2a24.com;
+ return 404; # managed by Certbot
+
+
+}
\ No newline at end of file
#
Certbot によって追加された設定ファイル /etc/letsencrypt/options-ssl-nginx.conf
性能アップのための設定
- ssl_session_cache shared:le_nginx_SSL:1m;
- shared: OS の共 有メモリの機能を使ってプロセス間で共有するセッションを使用
- le_nginx_SSL:1m: セッションを le_nginx_SSL という名前で 1 MB の容量を使用して設定
- ssl_session_timeout 1440m;
セッションタイムアウを 24 時間 (1440 分) に設定 - nginx実践ガイド impress top gearシリーズ | 渡辺高志 | コンピュータ・IT | Kindleストア | Amazon
P141, 142
暗号化アルゴリズムと前方秘匿性(Perfect Forward Secrecy)
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
- SL/TLS のプロトコルバージョンを指定
- 現在は SSL3.0 が非推奨となっており、TLS1.0 についてもそろそろ廃止が進みつつある。
- ssl_prefer_server_ciphers on;
暗号化アルゴリズムの選択は、サーバ側の優先度を使用 - ssl_ciphers
暗号化アルゴリズムを指定 - nginx実践ガイド impress top gearシリーズ | 渡辺高志 | コンピュータ・IT | Kindleストア | Amazon
P131
# This file contains important security parameters. If you modify this file # manually, Certbot will be unable to automatically provide future security # updates. Instead, Certbot will print and log an error message with a path to # the up-to-date file that you will need to refer to when manually updating # this file. ssl_session_cache shared:le_nginx_SSL:1m; ssl_session_timeout 1440m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";
SSL/TLS の評価
SSL の評価とは?については、次のページが参考になります。
そこで、
でテストしたところ、A でした。問題なさそうです♪
おわりに
Nginx の SSL/TLS 設定については、次の本を見ながら勉強しました。
他に、次のページがとても勉強になりました。
Certbot の書き換え前から存在した、HTTP 設定は必要でしょうか?削除してみてどうなるか、など確かめてみたいなと考えています。
以上です。
