Nginx セキュリティ · 3 min read · Jan 24, 2026
Nginxウェブサーバーを保護するためのヒントとコツ

Nginxはオープンソースの軽量で高性能な、世界中で最も成長しているウェブサーバーです。NginxはLinux、Windows、Mac OS、Solarisオペレーティングシステムで動作します。NGINXは人気が高まっているため、ますます多くのNGINXデプロイメントを保護する必要があります。
このチュートリアルでは、一般的なNginxサーバーのセキュリティに関するヒントとコツを説明します。
要件
- Ubuntu 18.04またはDebian 9を実行しているサーバー。
- サーバーにルートパスワードが設定されています。
Nginxのインストール
まず、システムにNginxをインストールする必要があります。次のコマンドを実行してインストールできます。
apt-get install nginx -yNginxがインストールされたら、次のコマンドでNginxのステータスを確認できます。
systemctl status nginx次の出力が表示されるはずです。
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-03-10 02:43:14 UTC; 4min 40s ago
Docs: man:nginx(8)
Process: 2271 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 2281 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 2274 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 2285 (nginx)
Tasks: 2 (limit: 1111)
CGroup: /system.slice/nginx.service
??2285 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??2290 nginx: worker process
Mar 10 02:43:14 ubuntu1804 systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 10 02:43:14 ubuntu1804 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Mar 10 02:43:14 ubuntu1804 systemd[1]: Started A high performance web server and a reverse proxy server.
Nginxの更新
Nginxウェブサーバーを更新する必要があります。パフォーマンスの向上、新機能、セキュリティ修正が追加されています。ほとんどの最新のLinuxディストリビューションは、デフォルトのパッケージリストに最新のNginxを含んでいません。そのため、パッケージマネージャーを介して最新バージョンのNginxをアップグレードする必要があります。次のコマンドでNginxウェブサーバーを更新できます。
apt-get update -y
apt-get install nginx --reinstall -y情報漏洩の防止
まず、Nginxがバージョン情報を開示しないようにする必要があります。
デフォルトでは、NginxはHTTPヘッダーにその名前とバージョンを表示します。
次のコマンドで確認できます。
curl -I http://localhost次の出力が表示されるはずです。
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Sat, 09 Mar 2019 15:28:01 GMT
Content-Type: text/html
Content-Length: 10918
Last-Modified: Fri, 01 Feb 2019 16:05:17 GMT
Connection: keep-alive
ETag: "5c546e3d-2aa6"
Accept-Ranges: bytes
上記の出力には、Nginxとオペレーティングシステムのバージョンが表示されます。
この情報を隠すには、/etc/nginx/nginx.confファイルを編集します。
nano /etc/nginx/nginx.confhttp設定部分内にserver_tokens off行を追加します。
http {
##
# Basic Settings
##
server_tokens off;
作業が完了したら、ファイルを保存して閉じます。次に、変更を適用するためにNginxウェブサーバーを再起動します。
systemctl restart nginx再度curlコマンドを実行します。
curl -I http://localhost次の出力が表示されるはずです。
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 09 Mar 2019 15:33:31 GMT
Content-Type: text/html
Content-Length: 10918
Last-Modified: Fri, 01 Feb 2019 16:05:17 GMT
Connection: keep-alive
ETag: "5c546e3d-2aa6"
Accept-Ranges: bytes
アクセスを制限するIPの設定
Nginxには、特定のIPアドレスを許可または拒否するためのngx_http_access_moduleというシンプルなモジュールが付属しています。
172.16.0.0/16からのNginxを許可し、他のサブネットからは拒否したい場合は、/etc/nginx/sites-enabled/defaultファイルを開きます。
nano /etc/nginx/sites-enabled/defaultサーバーブロック内に次の変更を加えます。
server {
listen 80 default_server;
listen [::]:80 default_server;
allow 172.16.0.0/16;
deny all;
作業が完了したら、ファイルを保存して閉じます。次に、これらの変更を適用するためにNginxを再起動します。
systemctl restart nginx次に、192.168.0.102のような他のIPアドレス範囲からNginxサーバーにアクセスしてみてください。
次に、次のコマンドでNginxログを確認します。
tail -f /var/log/nginx/error.log次の出力にアクセス禁止が表示されるはずです。
2019/03/09 16:13:01 [error] 11589#11589: *1 access forbidden by rule, client: 192.168.0.102, server: _, request: "GET /test/ HTTP/1.1", host: "172.16.0.122"
TLSでNginxを保護する
TLS(Transport Layer Security)はSSL(Secure Socket Layer)の後継です。より強力で効率的なHTTPSを提供し、Forward Secrecy、最新のOpenSSL暗号スイートとの互換性、HSTSなどの多くの強化が含まれています。このチュートリアルでは、Nginxで自己署名のSSL証明書を有効にする方法を示します。代わりにLet’s Encrypt証明書を使用したい場合は、こちらを参照してください: https://www.howtoforge.com/tutorial/nginx-with-letsencrypt-ciphersuite/
まず、次のコマンドでSSL用のディレクトリを作成します。
mkdir /etc/nginx/ssl/次に、次のコマンドでキーと証明書を生成します。
cd /etc/nginx/ssl/最初に、次のコマンドでキーを生成します。
openssl genrsa -aes256 -out nginx.key 1024次の出力が表示されるはずです。
Generating RSA private key, 1024 bit long modulus
...++++++
.............................++++++
e is 65537 (0x010001)
Enter pass phrase for nginx.key:
Verifying - Enter pass phrase for nginx.key:
次に、次のコマンドでcsrを生成します。
openssl req -new -key nginx.key -out nginx.csr以下の情報をすべて提供します。
Generating RSA private key, 1024 bit long modulus
...++++++
.............................++++++
e is 65537 (0x010001)
Enter pass phrase for nginx.key:
Verifying - Enter pass phrase for nginx.key:
root@ubuntu1804:~# openssl req -new -key nginx.key -out nginx.csr
Enter pass phrase for nginx.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:Gujarat
Locality Name (eg, city) []:Junagadh
Organization Name (eg, company) [Internet Widgits Pty Ltd]:IT
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:HITESH
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:admin
An optional company name []:IT
次に、次のコマンドで証明書に署名します。
openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt次の出力が表示されるはずです。
Signature ok
subject=C = IN, ST = Gujarat, L = Junagadh, O = IT, OU = IT, CN = HITESH, emailAddress = [email protected]
Getting Private key
Enter pass phrase for nginx.key:
次に、Nginxのデフォルトの仮想ホストファイルを開き、証明書を定義します。
nano /etc/nginx/sites-enabled/default次の変更を加えます。
server {
listen 192.168.0.100:443 ssl;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
作業が完了したら、ファイルを保存して閉じます。次に、これらの変更を適用するためにNginxサーバーを再起動します。
systemctl restart nginxディレクトリのパスワード保護
Nginxウェブサーバーを設定する際に、特定のディレクトリをパスワードで保護することもできます。これを行うには、.htpasswdファイルを使用します。
そのために、passwdファイルを作成し、次のコマンドでユーザーを追加します。
mkdir /etc/nginx/.htpasswd
htpasswd -c /etc/nginx/.htpasswd/passwd admin次の出力が表示されるはずです。
New password:
Re-type new password:
Adding password for user admin
次に、次のコマンドでNginxウェブルート内にテストディレクトリを作成します。
mkdir /var/www/html/test次に、次のコマンドでwww-dataユーザーに所有権を与えます。
chown -R www-data:www-data /var/www/html/test次に、次のコマンドでNginxのデフォルトの仮想ホストファイルを開きます。
nano /etc/nginx/sites-enabled/default次に、以下のようにテストディレクトリを保護します。
location /test {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd/passwd;
作業が完了したら、ファイルを保存して閉じます。次に、これらの変更を適用するためにNginxサービスを再起動します。
systemctl restart nginx次に、ウェブブラウザを開き、URL http://your-server-ip/test を入力します。テストディレクトリにアクセスするためにユーザー名とパスワードを入力するように求められます。

おめでとうございます! Ubuntu 18.04サーバーでNginxサーバーを正常に保護しました。これがNginxウェブサーバー上にホストされているアプリケーションを保護するのに役立つことを願っています。質問があればお気軽にお尋ねください。詳細については、Nginxセキュリティドキュメントを参照してください。
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。