前回作った検証環境Webサーバはとてもご機嫌である。しかしながら使ってみると不満も出てくる。nginxの売りの一つであるリバースプロキシを設定している場合、Webサイトに変更を加えて表示の具合を確認しようとすると、キャッシュを削除しないと加えた変更が反映されないというまどろっこしい事態になってしまうのだ。いちいちキャッシュディレクトリにあるキャッシュファイルを全部削除するのもめんどくさい。かといって検証環境だけリバースプロキシ設定を辞めてしまうと本番環境と設定に差分が出てしまうので色々ややこしい。リバースプロキシの後ろではとれる環境変数が違ったりするのだ。そこで登場するのが、サードパーティーモジュールのngx_cache_purgeである。このモジュールを使えば、ページ毎にキャッシュをパージ(削除)することができる。
とは言え、サードパーティーモジュールを追加するにはビルドの時点で入れてあげなきゃいけない。これがめんどくさいポイントなのだが、幸い本番環境も構築前なので検証環境のnginxをいったん削除してngx_cache_purgeが入ったパッケージをビルドしなおしてしまおう。さらに、nginxリポジトリではStable(安定)バージョンの1.2.7だったが、検証環境ということでDevelopment(開発)バージョンの1.3.14を使ってみようではないか。以下はその作業の備忘録である。
1.nginxのアンインストール
えいやっ!
# yum erase nginx
Loaded plugins: downloadonly, fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.2.7-1.el6.ngx will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================
Removing:
nginx x86_64 1.2.7-1.el6.ngx @nginx 842 k
Transaction Summary
=======================================================================================================================================================================
Remove 1 Package(s)
Installed size: 842 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : nginx-1.2.7-1.el6.ngx.x86_64 1/1
警告: /etc/nginx/nginx.conf は /etc/nginx/nginx.conf.rpmsave として保存されました。
Verifying : nginx-1.2.7-1.el6.ngx.x86_64 1/1
Removed:
nginx.x86_64 0:1.2.7-1.el6.ngx
Complete!
#
#
2.必要そうなパッケージのインストール
コンパイルやビルドに必要なパッケージを色々インストールする。
# yum install make gcc rpm-build perl-ExtUtils-Embed
~省略~
Complete!
3.NginxのSRPM をインストール
SRPMをダウンロードし、インストールする。
SRPMをダウンロード
# wget http://dl.fedoraproject.org/pub/epel/6/SRPMS/nginx-1.0.15-3.el6.src.rpm
--2013-03-13 23:12:08-- http://dl.fedoraproject.org/pub/epel/6/SRPMS/nginx-1.0.15-3.el6.src.rpm
dl.fedoraproject.org をDNSに問いあわせています... 209.132.181.23, 209.132.181.24, 209.132.181.25, ...
dl.fedoraproject.org|209.132.181.23|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 719402 (703K) [application/x-rpm]
`nginx-1.0.15-3.el6.src.rpm' に保存中
100%[=============================================================================================================================>] 719,402 51.4K/s 時間 10s
2013-03-13 23:12:19 (67.3 KB/s) - `nginx-1.0.15-3.el6.src.rpm' へ保存完了 [719402/719402]
#
#
# rpm -ivh ./nginx-1.0.15-3.el6.src.rpm
警告: ./nginx-1.0.15-3.el6.src.rpm: ヘッダ V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
1:nginx 警告: ユーザ mockbuild は存在しません - root を使用します
警告: グループ mockbuild は存在しません - root を使用します
~省略~
警告: グループ mockbuild は存在しません - root を使用します
#
#
ものすごくたくさん怒られたけど、代わりにroot使うよっていう警告だけなので気にしない。エラーやフェイタルならば気にしてやってもいいが、ワーニング程度では動じない図太さが、これからの経済が混乱し続ける世の中を生き抜くには必要なのだ。
4.Nginxのソースとngx_cache_purgeをダウンロード
さらにビルドする材料をダウンロードしてくる。
インストールしたソースディレクトリに移動
# cd ~/rpmbuild/SOURCES
Nginxのソースをダウンロード
# wget http://nginx.org/download/nginx-1.3.14.tar.gz
--2013-03-13 22:58:40-- http://nginx.org/download/nginx-1.3.14.tar.gz
nginx.org をDNSに問いあわせています... 206.251.255.63
nginx.org|206.251.255.63|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 749457 (732K) [application/octet-stream]
`nginx-1.3.14.tar.gz' に保存中
100%[=============================================================================================================================>] 749,457 80.1K/s 時間 12s
2013-03-13 22:58:52 (59.6 KB/s) - `nginx-1.3.14.tar.gz' へ保存完了 [749457/749457]
#
#
ngx_cache_purgeのダウンロード
# wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
--2013-03-13 23:33:57-- http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
labs.frickle.com をDNSに問いあわせています... 94.23.90.117
labs.frickle.com|94.23.90.117|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 10358 (10K) [application/octet-stream]
`ngx_cache_purge-2.0.tar.gz' に保存中
100%[=============================================================================================================================>] 10,358 15.5K/s 時間 0.7s
2013-03-13 23:33:59 (15.5 KB/s) - `ngx_cache_purge-2.0.tar.gz' へ保存完了 [10358/10358]
#
#
5.specファイルの編集
specファイルをダウンロードしたソースのバージョンに合うように編集する。dogmap.jpさんの「Nginx に cache purge モジュールを追加する」やSakuratandotbizさんの「さくらVPSとnginxリバースプロクシで最速WordPressブログを作る方法(ベンチマーク付き)」などを参考にさせてもらいました。
# cd ~/rpmbuild/SPECS
# vi nginx.spec
%global nginx_user nginx
%global nginx_group %{nginx_user}
%global nginx_home %{_localstatedir}/lib/nginx
%global nginx_home_tmp %{nginx_home}/tmp
%global nginx_logdir %{_localstatedir}/log/nginx
%global nginx_confdir %{_sysconfdir}/nginx
%global nginx_datadir %{_datadir}/nginx
%global nginx_webroot %{nginx_datadir}/html
Name: nginx
Version: 1.3.14
Release: 3%{?dist}
Summary: A high performance web server and reverse proxy server
Group: System Environment/Daemons
# BSD License (two clause)
# http://www.freebsd.org/copyright/freebsd-license.html
License: BSD
URL: http://nginx.org/
#BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Source0: http://nginx.org/download/nginx-%{version}.tar.gz
Source1: nginx.init
Source2: nginx.logrotate
Source3: nginx.conf
Source4: default.conf
Source5: ssl.conf
Source6: virtual.conf
Source7: nginx.sysconfig
Source10: http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
Source100: index.html
Source101: poweredby.png
Source102: nginx-logo.png
Source103: 404.html
Source104: 50x.html
# removes -Werror in upstream build scripts. -Werror conflicts with
# -D_FORTIFY_SOURCE=2 causing warnings to turn into errors.
Patch0: nginx-auto-cc-gcc.patch
BuildRequires: GeoIP-devel
BuildRequires: gd-devel
BuildRequires: libxslt-devel
BuildRequires: openssl-devel
BuildRequires: pcre-devel
BuildRequires: perl-devel
BuildRequires: perl(ExtUtils::Embed)
BuildRequires: zlib-devel
Requires: GeoIP
Requires: gd
Requires: openssl
Requires: pcre
Requires: perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))
Requires(pre): shadow-utils
Requires(post): chkconfig
Requires(preun): chkconfig, initscripts
Requires(postun): initscripts
Provides: webserver
%description
Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
IMAP protocols, with a strong focus on high concurrency, performance and low
memory usage.
%prep
%setup -q
%patch0 -p0
%setup -T -D -a 10
%build
# nginx does not utilize a standard configure script. It has its own
# and the standard configure options cause the nginx configure script
# to error out. This is is also the reason for the DESTDIR environment
# variable.
export DESTDIR=%{buildroot}
./configure \
--prefix=%{nginx_datadir} \
--sbin-path=%{_sbindir}/nginx \
--conf-path=%{nginx_confdir}/nginx.conf \
--error-log-path=%{nginx_logdir}/error.log \
--http-log-path=%{nginx_logdir}/access.log \
--http-client-body-temp-path=%{nginx_home_tmp}/client_body \
--http-proxy-temp-path=%{nginx_home_tmp}/proxy \
--http-fastcgi-temp-path=%{nginx_home_tmp}/fastcgi \
--http-uwsgi-temp-path=%{nginx_home_tmp}/uwsgi \
--http-scgi-temp-path=%{nginx_home_tmp}/scgi \
--pid-path=%{_localstatedir}/run/nginx.pid \
--lock-path=%{_localstatedir}/lock/subsys/nginx \
--user=%{nginx_user} \
--group=%{nginx_group} \
--with-file-aio \
--with-ipv6 \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-http_perl_module \
--with-mail \
--with-mail_ssl_module \
--with-cc-opt="%{optflags} $(pcre-config --cflags)" \
--with-ld-opt="-Wl,-E" \
--add-module=%{_builddir}/nginx-%{version}/ngx_cache_purge-2.0
make %{?_smp_mflags}
mv %{_builddir}/nginx-%{version}/ngx_cache_purge-2.0/CHANGES %{_builddir}/nginx-%{version}/ngx_cache_purge-2.0/CHANGES.ngx_cache_purge
mv %{_builddir}/nginx-%{version}/ngx_cache_purge-2.0/README.md %{_builddir}/nginx-%{version}/ngx_cache_purge-2.0/README.md.ngx_cache_purge
%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot} INSTALLDIRS=vendor
find %{buildroot} -type f -name .packlist -exec rm -f '{}' \;
find %{buildroot} -type f -name perllocal.pod -exec rm -f '{}' \;
find %{buildroot} -type f -empty -exec rm -f '{}' \;
find %{buildroot} -type f -exec chmod 0644 {} \;
find %{buildroot} -type f -iname '*.so' -exec chmod 0755 '{}' \;
chmod 0755 %{buildroot}%{_sbindir}/nginx
install -p -D -m 0755 %{SOURCE1} %{buildroot}%{_initrddir}/nginx
install -p -D -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/logrotate.d/nginx
install -p -D -m 0644 %{SOURCE7} %{buildroot}%{_sysconfdir}/sysconfig/nginx
install -p -d -m 0755 %{buildroot}%{nginx_confdir}/conf.d
install -p -d -m 0755 %{buildroot}%{nginx_home_tmp}
install -p -d -m 0755 %{buildroot}%{nginx_logdir}
install -p -d -m 0755 %{buildroot}%{nginx_webroot}
install -p -m 0644 %{SOURCE3} %{buildroot}%{nginx_confdir}
install -p -m 0644 %{SOURCE4} %{SOURCE5} %{SOURCE6} %{buildroot}%{nginx_confdir}/conf.d
install -p -m 0644 %{SOURCE100} %{buildroot}%{nginx_webroot}
install -p -m 0644 %{SOURCE101} %{SOURCE102} %{buildroot}%{nginx_webroot}
install -p -m 0644 %{SOURCE103} %{SOURCE104} %{buildroot}%{nginx_webroot}
install -p -D -m 0644 %{_builddir}/nginx-%{version}/man/nginx.8 %{buildroot}%{_mandir}/man8/nginx.8
# convert to UTF-8 all files that give warnings.
for textfile in CHANGES
do
mv $textfile $textfile.old
iconv --from-code ISO8859-1 --to-code UTF-8 --output $textfile $textfile.old
rm -f $textfile.old
done
%clean
rm -rf %{buildroot}
%pre
if [ $1 -eq 1 ]; then
getent group %{nginx_group} > /dev/null || groupadd -r %{nginx_group}
getent passwd %{nginx_user} > /dev/null || \
useradd -r -d %{nginx_home} -g %{nginx_group} \
-s /sbin/nologin -c "Nginx web server" %{nginx_user}
exit 0
fi
%post
if [ $1 == 1 ]; then
/sbin/chkconfig --add %{name}
fi
%preun
if [ $1 = 0 ]; then
/sbin/service %{name} stop >/dev/null 2>&1
/sbin/chkconfig --del %{name}
fi
%postun
if [ $1 == 2 ]; then
/sbin/service %{name} upgrade || :
fi
%files
%defattr(-,root,root,-)
%doc LICENSE CHANGES README
%{nginx_datadir}/
%{_sbindir}/nginx
%{_mandir}/man3/nginx.3pm*
%{_mandir}/man8/nginx.8*
%{_initrddir}/nginx
%dir %{nginx_confdir}
%dir %{nginx_confdir}/conf.d
%dir %{nginx_logdir}
%config(noreplace) %{nginx_confdir}/fastcgi.conf
%config(noreplace) %{nginx_confdir}/fastcgi.conf.default
%config(noreplace) %{nginx_confdir}/fastcgi_params
%config(noreplace) %{nginx_confdir}/fastcgi_params.default
%config(noreplace) %{nginx_confdir}/koi-utf
%config(noreplace) %{nginx_confdir}/koi-win
%config(noreplace) %{nginx_confdir}/mime.types
%config(noreplace) %{nginx_confdir}/mime.types.default
%config(noreplace) %{nginx_confdir}/nginx.conf
%config(noreplace) %{nginx_confdir}/nginx.conf.default
%config(noreplace) %{nginx_confdir}/scgi_params
%config(noreplace) %{nginx_confdir}/scgi_params.default
%config(noreplace) %{nginx_confdir}/uwsgi_params
%config(noreplace) %{nginx_confdir}/uwsgi_params.default
%config(noreplace) %{nginx_confdir}/win-utf
%config(noreplace) %{nginx_confdir}/conf.d/*.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/nginx
%config(noreplace) %{_sysconfdir}/sysconfig/nginx
%dir %{perl_vendorarch}/auto/nginx
%{perl_vendorarch}/nginx.pm
%{perl_vendorarch}/auto/nginx/nginx.so
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home}
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home_tmp}
%changelog
* Sun Oct 28 2012 Jamie Nguyen - 1.0.15-3
- add nginx man page (#870738)
- link to official documentation not the community wiki (#870733)
- default.conf: add "default_server" to the "listen" directive (#842738)
~省略~
オプションをしこたまつけた上にngx_cache_purgeまで追加しているという豪華仕様である。デフォルトのnginxよりもできることの幅は広がっているはずだ。例えば、リバースプロキシの後ろにあるPHPではクライアントのIPアドレスを取得できない。これを解決するためにApacheではmod_rpafモジュールを使うが、nginxではReal IPモジュールを使う。このReal IPモジュールはデフォルトのNginxビルドには含まれていないが、今回ビルドしたてんこ盛りビルドでは余裕で含まれている。SSLのセキュアなWebサイトもデフォルトビルドでは作れない。Apacheのようにあとからモジュールを追加できないnginxではいずれ自分でビルドするという道は避けては通れぬ道なのだ。
因みに、BuildRequires:で指定されてるパッケージは前提条件なのでビルドする前にyumでいれとかないけない。入れておくのは以下の通り。入ってれば入れなおす必要はない。
# yum --enablerepo=epel install GeoIP-devel
# yum install gd-devel
# yum install libxslt-devel
# yum install openssl-devel
# yum install pcre-devel
# yum install perl-devel
# yum install perl(ExtUtils::Embed)
# yum install zlib-devel
6.ビルドして、インストール
rpmをビルドして、インストールする。
ビルド!
# rpmbuild -bb nginx.spec
~省略~
+ exit 0
ほんで、できたrpmをインストール
# cd ~/rpmbuild/RPMS/x86_64
#
# rpm -ivh ./nginx-1.3.14-3.el6.x86_64.rpm
準備中... ########################################### [100%]
1:nginx ########################################### [100%]
#
でけた!あとは前回行ったのと同様に自動起動の設定と、confファイルの編集をすればよい。今回はconfファイルでproxy_cache_purge,fastcgi_cache_purge,scgi_cache_purge,uwsgi_cache_purgeの4つのディレクティブが使えるようになっているはずだ。お試しあれ。yumコマンドでアンインストールしたので、/etc/nginx/nginx.conf.rpmsaveという名前で削除前のコンフファイルが残っているのでそのまま使うと近道だ。ngx_cache_purgeで追加されるディレクティブの詳しい使い方はこちら(README)を参照するとよいだろう。これでページごとにキャッシュクリアできるので、作ったWebサイトを色々いじっくってもすぐに変更が確認できる。よきかな、よきかな。