# Nginx web サーバーのセキュリティ
[error] 2988348#2988348: *489243 open() "/usr/share/nginx/html/upl.php" failed (2: No such file or directory), client: ***.***.***.***, server: , request: "GET /upl.php HTTP/1.1", host: "***.***.***.***"
環境
- Ubuntu 22
- nginx/1.18.0
# Nginx 設定ファイル
- ファイル
/etc/nginx/nginx.conf - http ブロック修正
http {
# 大量リクエストを制限し、DDoS や悪意のあるスクレイピングを防ぎ
# 1秒間に5リクエストまで許可
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
# 不要な情報の非表示
server_tokens off;
# ファイルやディレクトリのリスト表示を無効化
autoindex off;
# 悪意のあるリクエストをブロック
if ($query_string ~* "wget|curl|rm|chmod|sh") {
return 403;
}
}
# server ブロック修正
server {
listen 80;
server_name hapicode.com;
root /var/www/laravel/public;
index index.php;
location / {
# クロスサイトスクリプティング(XSS)やクリックジャッキングを防ぎ
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# 悪質な User-Agent をブロックï
if ($http_user_agent ~* (python|curl|wget|scrapy|bot)) {
return 403;
}
# リクエストのパラメータに SELECT や DROP などの SQL キーワードが含まれる場合、ブロック
if ($query_string ~* "union.*select.*\(") {
return 403;
}
if ($query_string ~* "drop.*table") {
return 403;
}
}
# 攻撃者が .php や .sh を実行禁止
location ~* \.(php|sh|exe|cgi|pl|py)$ {
deny all;
}
# .htaccess .env .git などの機密ファイルへのアクセスを禁止
location ~ /\.(?!well-known).* {
deny all;
access_log off;
log_not_found off;
}
}
# 監視ツールを導入
access.log を監視して自動でアラート表示、通知、遮断したりするツールを導入検討
| ツール | 機能 | メリット | 向いている用途 |
|---|---|---|---|
| GoAccess リアルタイム監視 & 可視化 Web | UI | で確認可能 | 高速なログ解析 |
| Logwatch | 定期レポート生成 | メールでレポート送信 | 1 日 1 回の確認 |
| Fail2Ban | 不正 IP 自動ブロック | 攻撃者の IP を遮断 | セキュリティ強化 |
| Swatch | リアルタイムアラート | すぐに通知が来る | 怪しいアクセス即対応 |