nginxでhttp2通信の実現ーApacheからnginxへ移行したい方必見
- 2016/9/1
- システム
- この記事は約3分で読めます。
nginx(エンジンエックス)といえば、
大量のリクエストに高速でしかも消費メモリを抑えて
対応できるというイメージをお持ちだと思います。
そう!
実際にそうなんです!
なので、Apacheからnginxへ移行したい方のために、
nginxのインストールからlet’s encryptを使って
http2通信を実現するところまでをご紹介します。
nginxでhttp2通信の実現
nginxのインストール
※OSは、CentOS 7になります。
yumでちゃちゃっとできちゃいます。
まず、
1 |
vim /etc/yum.repos.d/nginx.repo |
で以下を記述。
1 2 3 4 5 |
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 |
それから、
1 |
yum install nginx |
※2016/8/29時点では、最新安定版は1.10.1になります。
nginxの起動設定及び起動
1 2 |
systemctl enable nginx systemctl start nginx |
nginxのインストールは、以上で終了です!
nginxの設定
example.comで
/var/www/publicにアクセスするものとして
設定を進めます。
①ディレクトリ、テストファイルの作成
1 2 3 |
mkdir -p /var/www/public/ chmod -R 777 /var/www echo "<h1>This is a test.</h1>" > /var/www/public/index.html |
②nginx.confの編集
※高速化のためにやっているだけので、
絶対に必要なものではありません。
1 |
vim /etc/nginx/nginx.conf |
以下を記述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; worker_rlimit_nofile 150000; events { worker_connections 65535; multi_accept on; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server_tokens off; charset UTF-8; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$request_time"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 5; keepalive_requests 20; gzip off; open_file_cache max=100 inactive=10s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; server_names_hash_bucket_size 64; server_name_in_redirect off; include /etc/nginx/conf.d/*.conf; } |
③example.com用のconfファイルの作成
1 |
vim /etc/nginx/conf.d/examplecom.conf |
以下を記載
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
server { listen 80; server_name example.com; # Path to the root of your installation root /var/www/public/; error_log /var/log/example.com.error.log; add_header Cache-Control public; # set max upload size client_max_body_size 10G; # Disable gzip to avoid the removal of the ETag header gzip off; index index.html; location = /robots.txt { allow all; log_not_found off; access_log off; } location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){ deny all; } location / { rewrite ^(/core/doc/[^\/]+/)$ $1/index.html; access_log /var/log/example.com.access.log main; try_files $uri $uri/ $uri.html $uri/index.html; } # Optional: set long EXPIRES header on static assets location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ { expires 30d; # Optional: Don't log access to assets access_log off; } } |
nginxの再起動
1 |
systemctl restart nginx |
以上で、example.comでアクセスできるようになったはずです!
http2通信の導入
http2通信を導入するには、ブラウザの仕様上SSLをはる必要があるので、
無料のSSLであるlet’s encryptを使って実現します。
①let’s encrypt クライアントのインストール
まずは、gitをインストールします。
1 |
yum -y install git |
let’s encrypt クライアントの本体をインストールします。
1 2 |
cd /usr/local/ git clone https://github.com/letsencrypt/letsencrypt |
②let’s encrypt クライアントが依存するパッケージのインストール
1 2 |
cd letsencrypt/ ./letsencrypt-auto --help |
③証明書の取得
1 2 3 |
./letsencrypt-auto certonly --webroot \ -w /var/www/public/ -d example.com \ --agree-tos |
④証明書の自動更新の設定
let’s encryptの証明書は、有効期限が3ヶ月なので、
自動で更新できるようにcronを設定します。
1 |
crontab -u root -e |
以下を記述
1 |
00 05 01 * * /usr/local/letsencrypt/letsencrypt-auto renew --force-renew && /bin/systemctl restart nginx |
これで毎月1日の5:00に自動で更新されます。
⑤example.com用のconfファイルの編集
1 |
vim /etc/nginx/conf.d/examplecom.conf |
以下に変更
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
server { listen 80; server_name example.com; # enforce https return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !DH !aNULL !eNULL !LOW !kECDH !DSS !MD5 !EXP !PSK !SRP !CAMELLIA !SEED'; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # Path to the root of your installation root /var/www/public/; error_log /var/log/example.com.error.log; add_header Cache-Control public; add_header Strict-Transport-Security 'max-age=31536000;'; # set max upload size client_max_body_size 10G; # Disable gzip to avoid the removal of the ETag header gzip off; index index.html; location = /robots.txt { allow all; log_not_found off; access_log off; } location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){ deny all; } location / { rewrite ^(/core/doc/[^\/]+/)$ $1/index.html; access_log /var/log/example.com.access.log main; try_files $uri $uri/ $uri.html $uri/index.html; } # Optional: set long EXPIRES header on static assets location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ { expires 30d; # Optional: Don't log access to assets access_log off; } } |
nginxを再起動します。
1 |
systemctl restart nginx |
以上で、全ての設定は完了です!
Firefoxでアクセスしてみると
http2通信になっていることがわかると思います。
ちなみに、SSLの質をテストしてくれるサイトで
A+を獲得できました!!
https://www.ssllabs.com/ssltest/
最後に
nginxをyumでインストールして、
ちょっとconfファイルを編集するだけで、設定完了です。
Apacheでhttp2通信を実現する場合と比べて、格段に楽です。
しかも、nginxの方が大量リクエストにも対応できていいし、
正直もっと早くから、nginxにしとけば良かったorz
デミ
最新記事 by デミ (全て見る)
- 【20分で完了】MacにDocker for Macのインストール - 2017/02/02
- 【2017年版】Web接客ツール9社を比較してみた - 2017/01/26
- 【昼休み中に完了!】Macで最新Ruby、Railsのインストールから画面表示まで - 2017/01/19