LinuC 202 出題範囲
https://linuc.org/linuc2/range/202.html
nginx の設定と管理
nginxコマンド
- 設定の構文チェック
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- 設定反映 (reload)
# nginx -s reload
nginx のSSL設定
ssl, ssl_certificate, ssl_certificate_key, ssl_ciphers, ssl_protocols
# サーバ証明書
ssl_certificate "/etc/letsencrypt/live/example.com/fullchain.pem";
# サーバ証明書の秘密鍵
ssl_certificate_key "/etc/letsencrypt/live/example.com/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
# 暗号化スイートのリスト
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
# プロトコル
ssl_protocols TLSv1.2;
*ssl_ciphers
のパラメータはMozillaWikiを参照する
Security/Server Side TLS
https://wiki.mozilla.org/Security/Server_Side_TLS
リバースプロキシサーバーとしての設定
proxy_pass, proxy_http_version, proxy_set_header
# HTTPヘッダ転送
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# アップストリームサーバのグループを定義
upstream backend {
server 127.0.0.1:8080;
}
upstream
の使い所がミソ。
# リバースプロキシ
server {
listen 80;
server_name example.com;
# プロキシ先をアップストリームサーバに指定
location / {
proxy_pass http://backend;
}
}
# アップストリーム
server {
listen 8080;
server_name example.com;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# PHP-FPM
location ~* \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}
nginx でリダイレクトを行う
return
return
は特定のステータスコードを応答する。
location / {
return 301 https://example.com$request_uri;
}
rewrite
rewrite
はマッチしたリクエストURIを別の文字列に置換する。
server {
listen 80;
server_name example.com;
rewrite ^ https://www.example.com$request_uri? permanent;
}
- rewriteのフラグ
- 指定なし -> URIを書き換えて後続の処理をする。
- last -> URIを書き換えた後、コンテキスト内の処理を終了する。再度マッチングする。
- break -> URIを書き換えた後、コンテキスト内の処理を終了する。
- redirect -> 302(Found)リダイレクトする。
- permanent -> 301(Moved Permanently)リダイレクトする。