プロキシ設定方法
サービスのポートが外部ネットワークに直接公開されないよう、デプロイ後に Nginx プロキシを設定することを強く推奨します。システムの安全性を高められ、証明書が必要な場合も関連ドキュメントを参照して設定できます。また、Nginx の負荷分散とリバースプロキシ機能によって、可用性と安定性を向上できます。
Nginx のデプロイ
-
nginx のインストールパッケージをダウンロードします。
- Linux amd64
- Linux arm64
wget https://pdpublic.nocoly.com/offline/common/nginx-1.30.4-glibc2.17-amd64.tar.gzwget https://pdpublic.nocoly.com/offline/common/arm64/nginx---glibc2.17-arm64.tar.gz -
Nginxのインストールディレクトリへの展開
- Linux amd64
- Linux arm64
tar -zxvf nginx-1.30.4-glibc2.17-amd64.tar.gz -C /usr/local/tar -zxvf nginx---glibc2.17-arm64.tar.gz -C /usr/local/ -
Nginx用のシステムユーザーおよびディレクトリ作成
useradd -r -s /usr/sbin/nologin nginxmkdir -p /usr/local/nginx/conf/conf.d /data/logs/weblogs/chown -R nginx:nginx /data/logs/weblogs -
Nginxメイン設定ファイルの作成
cat > /usr/local/nginx/conf/nginx.conf <<\EOFuser nginx;worker_processes auto;worker_cpu_affinity auto;worker_rlimit_nofile 204800;pid nginx.pid;events {use epoll;worker_connections 20480;}http {include mime.types;default_type application/octet-stream;server_tokens off;log_format main "$http_x_forwarded_for | $time_local | $request | $status | $body_bytes_sent | ""$request_body | $content_length | $http_referer | $http_user_agent | ""$http_cookie | $remote_addr | $hostname | $upstream_addr | $upstream_response_time | $request_time";server_names_hash_bucket_size 128;client_header_buffer_size 8k;client_max_body_size 10M;large_client_header_buffers 4 32k;sendfile on;tcp_nopush on;tcp_nodelay on;proxy_buffer_size 64k;proxy_buffers 4 128k;keepalive_timeout 10;open_file_cache max=102400 inactive=60s;open_file_cache_valid 30s;open_file_cache_min_uses 1;resolver_timeout 10s;underscores_in_headers on;gzip on;gzip_proxied any;gzip_disable "msie6";gzip_vary on;gzip_min_length 1024;gzip_comp_level 8;gzip_buffers 16 8k;gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png;proxy_http_version 1.1;include conf.d/*.conf;}EOF -
業務用プロキシルールの設定
/usr/local/nginx/conf/conf.d/にプロキシ設定ファイル(例:hdp.conf)を作成します。vi /usr/local/nginx/conf/conf.d/hdp.conf利用するHTTPまたはHTTPSの設定例を参照してください。
-
Nginx設定ファイルのフォーマットチェック
/usr/local/nginx/sbin/nginx -t -
Nginxのsystemdサービスファイル作成
cat > /etc/systemd/system/nginx.service <<'EOF'[Unit]Description=NGINX HTTP and reverse proxy serverAfter=network.targetWants=network-online.target[Service]Type=forkingPIDFile=/usr/local/nginx/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -t -qExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s quitRestart=on-failureLimitNOFILE=65535[Install]WantedBy=multi-user.targetEOF -
Nginxの起動と自動起動の有効化
systemctl daemon-reloadsystemctl enable nginxsystemctl start nginx
Nginx ログの定期ローテーション
Nginx のリクエストログが蓄積してディスク容量を圧迫しないよう、ログを自動的にローテーション・削除する仕組みを設定することを推奨します。
-
必要な設定用ディレクトリとログ保存先を作成します。
mkdir -p /usr/local/logrotate-configmkdir -p /data/logs/weblogs/oldlogschown -R nginx:nginx /data/logs/weblogs -
logrotate のローテーションルールを記述します。
cat > /usr/local/logrotate-config/nginx <<\EOF/data/logs/weblogs/*.log {create 0640 nginx nginxdailydateextdateformat -%Y-%m-%ddateyesterdayrotate 180missingokifemptycompressdelaycompressolddir /data/logs/weblogs/oldlogssharedscriptspostrotate/bin/kill -USR1 `cat /usr/local/nginx/nginx.pid 2>/dev/null` 2>/dev/null || trueendscript}EOF -
設定が有効であることを手動で確認します。
logrotate -d -f /usr/local/logrotate-config/nginx- 必ずデバッグ出力を確認してください。エラーが出た場合は、別途対応が必要です。
-
crontab に定期実行タスクを設定します。
( crontab -l 2>/dev/null; echo '0 0 * * * /usr/sbin/logrotate -f /usr/local/logrotate-config/nginx >/dev/null 2>&1' ) | crontab -