Nginx Web サーバーは、世界中の Web サイトの 30% 以上で使用されており、さらに増加しています。
オンライン Web の脅威の増加を考慮すると、Web エンジニアにとっての課題の 1 つは、Nginx の強化と安全性を十分に認識することです。
Nginx は、そのパフォーマンスと軽量な Web サーバー/プロキシでよく知られており、多くの最もビジーなサイトで使用されています。
- Pinterest.com
- Reddit.com
- ワードプレス.com
- スタックオーバーフロー.com
- Mail.ru
Nginx で Web アプリケーションをホストしていてセキュリティを懸念している場合、最初に実装したいのは Web アプリケーション ファイアウォール (WAF) です。
Mod Security は Trustwave SpiderLabs によるオープンソース WAF で、2012 年に Nginx で利用できるようになりました。
このガイドでは、Nginx で Mod Security をダウンロード 、 インストール 、 設定する 方法を説明します。
次のデモは、 DigitalOcean でホストされる CentOS 上で行われます。
Nginx を初めて使用する場合は、この 基礎コース を受講することをお勧めします。

Nginx と ModSecurity をダウンロードする
nginx をサーバーに直接ダウンロードするか、ローカル PC にダウンロードして転送することができます。
- 以下のリンクから最新バージョンをダウンロードしてください
http://nginx.org/en/download.html
- サーバーに直接ダウンロードしている場合は、以下のように wget を使用できます
wget http://nginx.org/download/nginx-1.9.15.tar.gz
- Gunzipコマンドを使用して解凍します。
gunzip -c nginx-1.9.15.tar.gz | tar xvf –
- 新しいフォルダーが作成されたことがわかります
drwxr-xr-x 8 1001 1001 4096 Apr 19 12:02 nginx-1.9.15
- 以下のリンクから最新バージョンの Mod Security をダウンロードします。
https://github.com/SpiderLabs/ModSecurity
- 以下のコマンドをサーバーから直接使用できます
wget https://www.modsecurity.org/tarball/2.9.1/modsecurity-2.9.1.tar.gz
gunzip -c modsecurity-2.9.1.tar.gz | tar xvf –
インストールしましょう

Mod Security を使用して Nginx をインストールする
Nginx と MOD セキュリティのソース コードをコンパイルすることが重要です。
- サーバーにログインし、root 権限があることを確認します。
注: 新しいサーバーで実行している場合は、次のライブラリをインストールする必要がある場合があります。
yum install gcc make automake autoconf libtool pcre pcre-devel libxml2 libxml2-devel curl curl-devel httpd-devel
まず 、MOD セキュリティをコンパイルしましょう。 modsecurity-2.9.1 フォルダーに移動し、以下のコマンドを使用します。
./configure --enable-standalone-module
make
次に 、Modセキュリティを備えたNginxをインストールします
./configure --add-module=../modsecurity-2.9.1/nginx/modsecurity
make
make install
これで、Nginx に Mod Security がインストールされ、それを構成する段階になりました。

Nginx を使用して Mod セキュリティを構成する
上記でダウンロードした ModSecurity ソース コードを解凍したフォルダーから modsecurity.conf-recommended & unicode.mapping ファイルを nginx conf フォルダーにコピーします。 find コマンドを使用することもできます。
find / -name modsecurity.conf-recommended
find / -name unicode.mapping
[root@GeekFlare-Lab conf]# cp /opt/nginx/binary/modsecurity-2.9.1/modsecurity.conf-recommended /usr/local/nginx/conf/
[root@GeekFlare-Lab conf]# cp /opt/nginx/binary/modsecurity-2.9.1/ unicode.mapping /usr/local/nginx/conf/
[root@GeekFlare-Lab conf]#
modsecurity.conf-recommended の名前を modsecurity.conf に変更しましょう。
mv modsecurity.conf-recommended modsecurity.conf
- nginx.conf ファイルのバックアップを作成します。
- nginx.conf ファイルを開き、「location /」ディレクティブの下に以下を追加します
ModSecurityEnabled on;
ModSecurityConfig modsecurity.conf;
したがって、次のように表示されるはずです
location / {
ModSecurityEnabled on;
ModSecurityConfig modsecurity.conf;
}
現在、Mod Security は Nginx と統合されています。 Nginx を再起動して、エラーなしで起動していることを確認します。
確認しましょう…
Nginx が Mod Security でコンパイルされていることを確認するには 2 つの方法があります。
初め…
nginx 実行可能ファイルで –V を使用して、コンパイルされたモジュールを一覧表示します。
[root@GeekFlare-Lab sbin]# . /nginx -V
nginx version: nginx/1.9.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
configure arguments: --add-module=../modsecurity-2.9.1/nginx/modsecurity
[root@GeekFlare-Lab sbin]#
2番…
ログ フォルダーに移動してエラー ファイルを表示すると、次のように表示されます。
2016/05/21 21:54:51 [notice] 25352#0: ModSecurity for nginx (STABLE)/2.9.1 (http://www.modsecurity.org/) configured.
2016/05/21 21:54:51 [notice] 25352#0: ModSecurity: APR compiled version="1.3.9"; loaded version="1.3.9"
2016/05/21 21:54:51 [notice] 25352#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2016/05/21 21:54:51 [notice] 25352#0: ModSecurity: LIBXML compiled version="2.7.6"
これで、Nginx を使用した ModSecurity の構成が正常に完了しました。
デフォルト設定では検出モードのみになっており、アクションは実行されず、Web アプリケーションは保護されません。
次回の記事では、OWASP ルール セットを構成し、Mod Security を有効にして Web セキュリティの脆弱性から保護する方法を説明しました。
