<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bluegold &#187; nginx</title>
	<atom:link href="http://blog.bluegold.me/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bluegold.me</link>
	<description>OpenSolaris と MacBook で自宅ネットワークを構築するメモ</description>
	<lastBuildDate>Sat, 06 Aug 2011 16:10:12 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>nginxでWordPressの記事を静的配信する</title>
		<link>http://blog.bluegold.me/2011/01/wordpress-to-static-html-with-nginx/</link>
		<comments>http://blog.bluegold.me/2011/01/wordpress-to-static-html-with-nginx/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 16:59:09 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=608</guid>
		<description><![CDATA[nginx の try_files を使うことで、WordPress を静的HTML に変換して配信する方法について説明しています。]]></description>
			<content:encoded><![CDATA[<p style="font-size: 80%;">前に途中まで書いていた記事をようやく公開。（twitter の書き込みを見ると 2010年10月ごろか）</p>
<p>WordPress はユーザ数が多いだけに、必要なものはたいていプラグインとして用意されている非常に使いやすいCMSだと思いますが、毎回動的にしかページを作れないという点にはちょっと不満を持っていました。</p>
<p>nginx や Vernish などのプロキシサーバを別に立ててキャッシュさせる方法を試そうと思っていたのですが、はまりポイントが多いらしく躊躇していました。そんな時に <a href="http://wordpress.org/extend/plugins/really-static/">Really Static</a> というプラグインを見つけたときから、今回の話は始まります。</p>
<h3 style="border-left:7px solid #ccc;border-bottom:1px solid #ccc;padding:.6em .8em;">Really Static プラグインの問題点</h3>
<p>Really Static プラグインは、ブログの記事の HTML を PHP の curl 関数でダウンロードして static フォルダの中に置いておき、それ以降は PHP で動的に HTML を作るのではなく、静的な HTML ファイルを返す、といった事をしてくれるプラグインです。ちょうど、Ruby on Rails のページキャッシュみたいなイメージですね。</p>
<p>インストールも設定も難しくなく、HTML のダウンロードも WP-cron で少しずつ行われるため、完全に WordPress 内に閉じた環境で実現できるすぐれたプラグインだと思いますが、（私にとっては）ひとつだけ大きな欠点がありました。</p>
<p><span id="more-608"></span></p>
<p>それは、Really Static プラグインがサイトの構造(URL)を変えてしまうことでした。<br />
例えば、http://blog.bluegold.me/sample という URL の記事に対して、Really Static の導入後にアクセスすると http://blog.bluegold.me/static/sample.html に 301 redirect されてしまいます。<br />
さらには他のページに書かれたリンクも静的ファイルの URL を直接さすように変更されてしまうので、一度導入してしまうと、後で使うのをやめるときに元に戻すのが難しくなってしまうのではないかと思いました。</p>
<p>最初は Really Static プラグインに手を入れて、リンクの URL を変更させないようにしようと思ったんですが、コードを読むと仕組みは意外と単純だったので、Wordpress に統合することを考えなければ、nginx の try_files だけで、似たようなことが出来ることに気がつきました。</p>
<h3 style="border-left:7px solid #ccc;border-bottom:1px solid #ccc;padding:.6em .8em;">nginx の機能を使う</h3>
<p><a href="http://wiki.nginx.org/HttpCoreModule#try_files">nginx の try_files</a> はリクエストを処理する際に実際のファイルを探す順番を記述する nginx に固有の設定項目で、Apache では mod_rewrite の複雑なルールを駆使して実現できるようなことをシンプルなルールで記述できるようにしたものです。</p>
<p>現在のこのブログの nginx.conf には以下のような記述がされています。</p>
<pre class="brush: perl;">
location {
  gzip_static on;
#  try_files /system/maintenance.html /static/$uri/index.html $uri /index.php?q=$uri&amp;$args;
  try_files /system/maintenance.html /static/$uri/index.html$args $uri /index.php?q=$uri&amp;$args;
}
</pre>
<p>具体的な設定は WordPress のパーマリンクの設定で変わります。<br />
$uri にはリクエストされた URL のパスの部分が入っています。リクエストを処理する際は以下を順番に調べていって、最初に見つかったものを返します。</p>
<pre class="bq" style="font-size: 75%; white-space:normal;">
※ 2011/01/30 追記
静的HTML化した投稿を表す /static/$url/index.html には $args も付けておかないと、プレビューが出来ませんでしたので修正しました。
</pre>
<ul>
<li>メンテナンス画面(/system/maintenance.html)</li>
<li>今回追加した静的HTMLのキャッシュ</li>
<li>css や画像などの静的ファイル</li>
<li>それ以外は WordPress に処理を渡す</li>
</ul>
<p>この設定だけで静的なHTMLファイルの配信は出来るようになります。
</p>
<h3 style="border-left:7px solid #ccc;border-bottom:1px solid #ccc;padding:.6em .8em;">静的HTMLの作成</h3>
<p>あとはブログの各記事を HTML としてダウンロードするだけです。</p>
<pre class="bq">
$ wget -r --follow-tags=a http://blog.bluegold.me
</pre>
<p>（あくまでも例なので、自分のサイト意外にはやらないでくださいね。）
</p>
<p>静的HTMLに変換しておくことで、このくらい速くなりました。(それぞれ３回試行)</p>
<table>
<tr>
<th>&nbsp;</th>
<th>平均</th>
<th>最速</th>
<th>最遅</th>
</tr>
<tr>
<th>WordPressのみ</th>
<td>0.544s</td>
<td>0.385s</td>
<td>0.832s</td>
</tr>
<tr>
<th>静的HTML</th>
<td>0.086s</td>
<td>0.084s</td>
<td>0.088s</td>
</tr>
</table>
<p style="font-size: small;">実はこのブログは WPtouch プラグインも使っているので、このままでは iPhone 用の画面が表示されません。<br />
この対処法はまた次回。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2011/01/wordpress-to-static-html-with-nginx/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>nginx を SMF で管理する</title>
		<link>http://blog.bluegold.me/2010/08/management-nginx-with-smf/</link>
		<comments>http://blog.bluegold.me/2010/08/management-nginx-with-smf/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 16:36:59 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[OpenSolaris]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[opensolaris]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=500</guid>
		<description><![CDATA[nginx や PHP の fast-cgi のプロセスを OpenSolaris の SMF で管理するための manifest の紹介。]]></description>
			<content:encoded><![CDATA[<p>もう1年以上前に書いていた下書きをようやく公開。</p>
<p><a href="/2008/11/nginx-php-fastcgi/">前回（と言っても1年半前か）</a> までで nginx と PHP の fast-cgi のビルドと設定が終わったので、OS の起動時に自動的に起動するようにサービスとして登録を行います。</p>
<p>OpenSolaris ではサービスの管理に <a href="http://en.wikipedia.org/wiki/Service_Management_Facility">SMF</a> という仕組みを使用しています。SMF はサービスの依存関係を記述できたり、サービスの起動を監視して自動的に再起動してくれたりと、Linux などで使われている /etc/init.d の rc スクリプトに比べるとメリットがありますが、コマンドが独自だったり、自分で SMF にサービスを登録するには manifest と呼ばれる XML ファイルを書く必要があるので、慣れるまではハードルがあります。（この辺は Mac OS X の launchd と似ている。）</p>
<p><span id="more-500"></span></p>
<p> 以下が、私が nginx を管理するために作成した nginx.xml です。</p>
<pre class="brush: xml;">
&lt;?xml version='1.0'?&gt;
&lt;!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'&gt;
&lt;service_bundle type='manifest' name='export'&gt;
  &lt;service name='network/nginx' type='service' version='0'&gt;
    &lt;create_default_instance enabled='true'/&gt;
    &lt;single_instance/&gt;
    &lt;dependency name='fs' grouping='require_all' restart_on='none' type='service'&gt;
      &lt;service_fmri value='svc:/system/filesystem/local'/&gt;
    &lt;/dependency&gt;
    &lt;dependency name='net' grouping='require_all' restart_on='none' type='service'&gt;
      &lt;service_fmri value='svc:/network/loopback'/&gt;
    &lt;/dependency&gt;
    &lt;exec_method name='start' type='method' exec='/usr/local/sbin/nginx -c /usr/local/conf/nginx.conf' timeout_seconds='60'&gt;
      &lt;method_context working_directory='/usr/local/logs'&gt;
        &lt;method_credential user='root' group='root'/&gt;
        &lt;method_environment&gt;
          &lt;envvar name='PATH' value='/usr/bin:/bin:/usr/local/bin'/&gt;
        &lt;/method_environment&gt;
      &lt;/method_context&gt;
    &lt;/exec_method&gt;
    &lt;exec_method name='stop' type='method' exec=':kill' timeout_seconds='60'&gt;
      &lt;method_context/&gt;
    &lt;/exec_method&gt;
  &lt;/service&gt;
&lt;/service_bundle&gt;
</pre>
<p>このファイルを以下のように SMF に登録します。</p>
<pre class="bq">
# svccfg -v import nginx.xml
</pre>
<p>同様に PHP の FastCGI のプロセスも SMF に登録します。</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE service_bundle SYSTEM &quot;/usr/share/lib/xml/dtd/service_bundle.dtd.1&quot;&gt;
&lt;service_bundle type=&quot;manifest&quot; name=&quot;php-fcgi&quot;&gt;
 &lt;service name=&quot;network/php-fcgi&quot; type=&quot;service&quot; version=&quot;0&quot;&gt;
   &lt;create_default_instance enabled=&quot;true&quot;/&gt;
   &lt;single_instance/&gt;

   &lt;dependency name=&quot;net&quot; grouping=&quot;require_all&quot; restart_on=&quot;none&quot; type=&quot;service&quot;&gt;
     &lt;service_fmri value=&quot;svc:/network/loopback&quot;/&gt;
   &lt;/dependency&gt;

   &lt;exec_method name=&quot;start&quot; type=&quot;method&quot; exec=&quot;/usr/local/sbin/php-fcgi.sh&quot; timeout_seconds=&quot;60&quot;&gt;
     &lt;method_context working_directory=&quot;/usr/local/www&quot;&gt;
      &lt;method_credential user=&quot;php&quot; group=&quot;webservd&quot; privileges=&quot;basic,net_privaddr&quot;/&gt;
      &lt;!--method_credential user=&quot;root&quot; group=&quot;root&quot; /--&gt;
       &lt;method_environment&gt;
         &lt;envvar name=&quot;PATH&quot; value=&quot;/usr/php/5.2/bin:/usr/local/sbin:/usr/bin:/bin&quot; /&gt;
       &lt;/method_environment&gt;
     &lt;/method_context&gt;
   &lt;/exec_method&gt;

    &lt;exec_method name='stop' type='method' exec=':kill' timeout_seconds='60'&gt;
      &lt;method_context/&gt;
    &lt;/exec_method&gt;

 &lt;/service&gt;
&lt;/service_bundle&gt;
</pre>
<p>この xml も同じように svccfg コマンドで登録します。</p>
<p>起動用の shell スクリプトは<a href="http://drupal.org/node/110224">こちらのサイト</a>のものを、ほぼそのまま使っています。</p>
<pre class="brush: bash;">
#!/bin/bash

LC_ALL=ja_JP.UTF-8

#FastCGI Webserver path
FCGI_WEB_SERVER_ADDRS=&quot;127.0.0.1&quot;

## ABSOLUTE path to the PHP binary
PHPFCGI=&quot;/usr/php/bin/php-cgi&quot;

## tcp-port to bind on
FCGIPORT=&quot;9000&quot;

## IP to bind on
FCGIADDR=&quot;127.0.0.1&quot;

## number of PHP children to spawn
PHP_FCGI_CHILDREN=5

## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000

# allowed environment variables sperated by spaces
ALLOWED_ENV=&quot;PATH&quot;

## if this script is run as root switch to the following user
USERID=webservd

################## no config below this line

ALLOWED_ENV=&quot;$ALLOWED_ENV PHP_FCGI_CHILDREN&quot;
ALLOWED_ENV=&quot;$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS&quot;
ALLOWED_ENV=&quot;$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS&quot;

if test x$UID = x0; then
  EX=&quot;/bin/su -m -c \&quot;$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\&quot; $USERID&quot;
else
  EX=&quot;$PHPFCGI -b $FCGIADDR:$FCGIPORT&quot;
fi

echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E=&quot;$E $i=${!i}&quot;
done

# clean environment and set up a new one
nohup env - $E sh -c &quot;$EX&quot; &amp;
</pre>
<p>サービスが SMF に登録できれば、svcadm コマンドで起動停止を切り替える事ができます。</p>
<h4>起動</h4>
<pre class="bq">
# svcadm enable nginx
# svcadm enable php-fastcgi
</pre>
<h4>停止</h4>
<pre class="bq">
# svcadm disable nginx
# svcadm disable php-fastcgi
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2010/08/management-nginx-with-smf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginx: SNI拡張を使ってVirtualHostでTLSを設定</title>
		<link>http://blog.bluegold.me/2010/02/nginx-server-name-indication-extension/</link>
		<comments>http://blog.bluegold.me/2010/02/nginx-server-name-indication-extension/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 14:36:17 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[pki]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=376</guid>
		<description><![CDATA[OpenSSLのSNI拡張を利用して、TLS で VirtualHost の設定を行います。]]></description>
			<content:encoded><![CDATA[<p><a href="/2010/02/howto-build-64bit-nginx-with-ssl-support-on-opensolaris/">前回の記事</a>でSNI(Server Name Indication)拡張を有効にしたnginxのバイナリを作成したので遊んでみました。SNI拡張は、個々の仮想ホスト(VirtualHost)で独自のサーバ証明書を利用するために作られた TLSv1 の拡張仕様です。</p>
<p>TLSやSSLを使ったウェブサーバーでは一般的には仮想ホストは使えない、という事になっています（何事にも例外はありますが。）仮想ホストは HTTP のリクエストヘッダの Host フィールドの値を元に振り分けられますが，HTTPのリクエストを出すよりも手前の TLS/SSL のハンドシェイク時にサーバ証明書の検証が行われるので，クライアントが接続しようとしているホスト名とサーバ証明書のホスト名が異なってしまい，不正な証明書と表示されてしまいます。</p>
<p>SNIは接続しようとしているホスト名を、クライアントからサーバに伝えるための仕様です。（ホスト名はハンドシェイク前に伝えないと行けないので、平文で送られる事になりますが。。。）これ自体は素晴らしいのですが，Windows XP の IE がサポートしていないので今ひとつ広まっていません。</p>
<p><span id="more-376"></span></p>
<p>まずは SNI を使わないで仮想ホストを設定する場合の例です。</p>
<pre class="bq">
ssl_certificate server_cert.pem;
ssl_certificate_key server_cer.key;
server {
    port 443;
    ssl on;
    server_name host1.example.com;
}
server {
    port 443;
    ssl on;
    server_name host2.example.co.jp;
}
</pre>
<p>サーバ証明書は1枚しか指定できないので，server_cert.pem には host1.example.com と host2.exacmple.co.jp の双方が記述されている必要があります。ホストが同一のドメインに存在する場合はワイルドカード証明書を使う方法もありますが，例のようにドメインが異なる場合は双方のホスト名をCNもしくはsubjectAltNameに記述します。この方法では仮想ホストが増える場合は証明書を発行し直す必要があります。
</p>
<p>次に SNI を使う場合の例です。</p>
<pre class="bq">
server {
    port 443;
    ssl on;
    ssl_certificate host1.example.com.pem;
    ssl_certificate_key host1.example.com.key;
    server_name host1.example.com;
}
server {
    port 443;
    ssl on;
    server_name host2.example.co.jp;
    ssl_certificate host2.example.co.jp.pem;
    ssl_certificate_key host2.example.co.jp.key;
}
</pre>
<p>仮想ホスト毎に証明書が指定する事ができるようになります。
</p>
<p>実際に SNI をサポートしている Firefox などのブラウザでアクセスすると、それぞれ異なる証明書を持つ事が確認できます。</p>
<p>ただ、現在の nginx に固有の問題かもしれませんが，仮想ホスト毎にクライアント証明書による認証の有無を切り替えるのは出来ないみたいです。<em>ssl_verify_client off;</em>な設定の仮想ホストが１つでもあると，クライアント認証を行う仮想ホストに対してもクライアント証明書が送られません。</p>
<p>クライアント認証が必要ない仮想ホストに対しても<em>ssl_verify_client optional;</em>を指定すると大丈夫なようですが。（<a href="http://wiki.nginx.org/NginxHttpSslModule">Nginx の Wiki</a>の説明では<em>optional</em> ではなく、<em>ask</em>になっているので注意が必要です。）</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2010/02/nginx-server-name-indication-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mercurial: nginx で hgwebdir.fcgi のセットアップ</title>
		<link>http://blog.bluegold.me/2010/02/setup-mercurial-hgwebdir-fcgi-on-nginx/</link>
		<comments>http://blog.bluegold.me/2010/02/setup-mercurial-hgwebdir-fcgi-on-nginx/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 15:04:09 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[opensolaris]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=379</guid>
		<description><![CDATA[Mercurial の hgwebdir.fcgi を nginx で使う際の設定例]]></description>
			<content:encoded><![CDATA[<h2>Mercurial 付属の hgwebdir.fcgi を nginx で動かすメモ</h2>
<p>CVS（やSubversion）をバージョン管理システムに使っていた頃はリポジトリの情報を見るのに<a href="http://www.viewvc.org/">ViewVC</a>を使っていましたが、ViewVCはMercurialをサポートしていません。Mercurial にはウェブ用の管理画面(hgweb/hgwebdir)が付属しているので、別のツールを使うニーズが少ないんでしょうね。</p>
<p>Mercurial のリポジトリ毎に対応する Redmine のプロジェクトが存在するので、そちらを見ればおおよその事は分かるのですが、Redmineのリポジトリブラウザはブランチを意識して作られていないので不便といえば不便です。
</p>
<p>hgwebの事は Mercurial への移行を検討していたときにも調べていたんですが、Django, flup, setuptools, ez_setup.py, easy_install, .. とPythonに不慣れな人間にはなじみのない単語が続々と出てきたので、ちょっと敬遠していましたが、ちょっと時間が取れたのでチャレンジしてみました。</p>
<p><span id="more-379"></span></p>
<p>hgwebdir のセットアップをする前に必要なソフトウェアのインストールを行います。環境は OpenSolaris 2009.06 で、可能な限り pkg を使ってインストールしていきます。インストールしたのは以下のパッケージです。</p>
<pre class="bq">
SUNWPython26
SUNWPython26-extra
SUNWpython26-setuptools
</pre>
</p>
<p>OpenSolaris 2009.06 のパッケージ管理システムにも Mercurial はありますが、バージョンが 1.1.2 と古く、クライアントの環境と合わないので、最新バージョンを easy_install からインストールします。</p>
<pre class="brush: bash;">
$ pfexec easy_install Mercurial
$ hg --version
Mercurial Distributed SCM (version 1.4.2)

Copyright (C) 2005-2009 Matt Mackall &lt;mpm@selenic.com&gt; and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</pre>
</p>
<p>次に flup が必要だそうなのでインストールします。</p>
<pre class="brush: bash;">
$ pfexec easy_install flup
</pre>
</p>
<p>hgwebdir を FastCGI で動作させるのにはいろいろな方法があるらしいのですが、Python特有のやり方はよく分からないので、lighttpd の <a href="http://redmine.lighttpd.net/projects/spawn-fcgi">spawn-fcgi</a>を使いました。インストールは以下の通り簡単です。</p>
<pre class="brush: bash;">
$ wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
$ gzip -dc spawn-fcgi-1.6.3.tar.gz | tar xvf -
$ cd pawn-fcgi-1.6.3
$ ./configure
$ make
$ pfexec make install
</pre>
</p>
<p>ここまででようやく下準備がおわりました。<br />
hgwebdir.fcgi は /usr/demo/mercurial/hgwebdir.fcgi をコピーして使っています。<br />
UTF-8 で表示するために、HGENCODING の設定を行う部分のコメントをはずしています。</p>
<pre class="bq">
os.environ["HGENCODING"] = "UTF-8"
</pre>
</p>
<p>hgweb.config は単純です。</p>
<pre class="bq">
[paths]
/ = /path/to/repos/*
</pre>
<p>これを以下のようなスクリプトで起動しています。</p>
<pre class="bq">
#!/bin/sh

LANG=ja_JP.UTF-8
export LANG

FCGI=/opt/local/bin/spawn-fcgi
exec $FCGI -s /tmp/.fcgi_hgwebdir -G webserved -M 0660 ./hgwebdir.fcgi
</pre>
</p>
<p>nginx の設定ファイルは以下のようになりました。<br />
初めて try_files を使ってみましたが、設定がとてもシンプルになりますね。<br />
hgwebdir は PATH_INFO を利用してブラウズするリポジトリを探すようなので、その部分の置き換えにちょっと手を入れています。</p>
<pre class="bq">
server {
    listen       80;
    server_name  hg.example.com;
    root   /path/to/public;
    index  index.html;

    access_log  /var/log/nginx/hg-access.log  main;

    location / {
        try_files $uri @hgweb;
    }

    location @hgweb {
        fastcgi_pass   unix:/tmp/.fcgi_hgwebdir;
        fastcgi_read_timeout    5m;
        set $path_info "";
        if ($fastcgi_script_name ~ "^/(.*)$") {
            set $path_info $1;
        }
        fastcgi_param PATH_INFO $path_info;
    }
}
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2010/02/setup-mercurial-hgwebdir-fcgi-on-nginx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSolarisで64bitのnginxをビルドする</title>
		<link>http://blog.bluegold.me/2010/02/howto-build-64bit-nginx-with-ssl-support-on-opensolaris/</link>
		<comments>http://blog.bluegold.me/2010/02/howto-build-64bit-nginx-with-ssl-support-on-opensolaris/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 09:05:29 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[OpenSolaris]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[opensolaris]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[メモ]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=362</guid>
		<description><![CDATA[OpenSolaris 2009.06 で 64bit 版の nginx をビルドする手順]]></description>
			<content:encoded><![CDATA[<div class="content">
<p><a href="http://nginx.net/">nginx</a>の新しいバージョン(0.7.65)がリリースされていたので，バージョンアップしてみました。</p>
<p>しばらく前に見つかっていた<a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3555">SSLの脆弱製</a>の実験をやろうと、久しぶりに SSL を有効にしたバイナリを作ろうとしたのですが，オプションの設定の仕方を忘れていて苦労しました。また、忘れても大丈夫なようにメモ。</p>
<p>OpenSolaris 2009.06 に含まれるOpenSSLは 0.9.8a と古いので、現時点での最新バージョンである<a href="http://www.openssl.org/source/openssl-0.9.8l.tar.gz">openssl-0.9.8l</a>をダウンロードしてきて使いました。今回は<a href="http://en.wikipedia.org/wiki/Server_Name_Indication">SNI(Server Name Indication)</a>を有効にしたいので<strong>enable-tlsext</strong>オプションを渡しています。
</p>
<pre class="brush: bash;">
./config --prefix=/usr/local no-shared enable-tlsext
make
pfexec make install
</pre>
<p>これだけでもビルドは出来るのですが，OpenSolaris 2009.06 のデフォルトの GCC 3.2.3 ではなく新しい /usr/bin/gcc-4.3.2 を使いたいので，少し手を入れています。</p>
<pre class="brush: bash;">
export CC=/usr/bin/gcc-4.3.2
export CFLAGS='-DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -march=native -O3 -Wall -DL_ENDIAN -DMD32_REG_T=int -DOPENSSL_BN_ASM_MONT -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM'
./Configure --prefix=/usr/local no-shared enable-tlsext solaris64-x86_64-gcc
make
pfexec make install
</pre>
<p><span id="more-362"></span></p>
<p><strong>export CC=/usr/bin/gcc-4.3.2</strong>した後に<em>config</em>スクリプトを実行すると，solaris64-x86_64-gcc-4.3.2 みたいな os:compiler になってエラーになってしまうんですよね。回避方法を探すのに手間取ってしまいました。
</p>
<p>次に nginx のビルドですが，普通に configure を実行すると 32bit のバイナリが出来てしまいますので、gcc に渡すオプションも configure に渡します。</p>
<pre class="brush: bash;">
./configure --prefix=/usr/local --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-cc=/usr/bin/gcc-4.3.2 --with-cc-opt='-m64 -march=native -O3 -I/usr/local/include ' --with-cpu-opt=amd64 --with-ld-opt='-m64 -L/usr/local/lib -L/usr/lib/amd64'
make
pfexec make install
</pre>
<p>ビルドした内容を確認</p>
<pre class="brush: bash;">
$ file /usr/local/sbin/nginx
/usr/local/sbin/nginx:  ELF 64-bit LSB executable AMD64 Version 1, dynamically linked, stripped

$ /usr/local/sbin/nginx -V
nginx version: nginx/0.7.65
built by gcc 4.3.2 (GCC)
TLS SNI support enabled
configure arguments: --prefix=/opt/local --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-cc=/usr/bin/gcc-4.3.2 --with-cc-opt='-m64 -march=native -O3 -I/usr/local/include' --with-cpu-opt=amd64 --with-ld-opt='-m64 -L/usr/local/lib -L/usr/lib/amd64'
</pre>
<p>SNIも有効になっています</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2010/02/howto-build-64bit-nginx-with-ssl-support-on-opensolaris/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginx のバージョンアップ</title>
		<link>http://blog.bluegold.me/2009/06/nginx-update-0759/</link>
		<comments>http://blog.bluegold.me/2009/06/nginx-update-0759/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 16:57:50 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=184</guid>
		<description><![CDATA[nginx を 0.6系から0.7系にバージョンアップする手順について。]]></description>
			<content:encoded><![CDATA[<p><img src="http://wp-cdn.bluegold.me/2008/10/nginx-small.png" width="150" height="50" alt="" class="title_img" /></p>
<p><a href="http://nginx.net/">nginx</a> のバージョン 0.7 系が stable になっていたので、サーバソフトウェアのアップデートを行いました。（久しぶりのブログの更新は OpenSolaris 2009.06 について書こうと思ってたんですが、これはまた後で。。。）
</p>
<p>インストールしたのは nginx 0.7.59 です。<a href="http://forum.nginx.org/read.php?2,2335">リリースのアナウンス</a>が 25 May 2009 なので、2週間くらい前に出たばかりだったようです。 0.6系と0.7系の間の変更点についてまとまっている資料を探したのですが、見つかりませんでした。</p>
<p>Change Log を見るとかなりの修正箇所があるようですが、大きくまとめると以下の機能が増えているようです。</p>
<pre class="brush: bash;">
*) caching of proxied and FastCGI servers;
*) &quot;try_files&quot; directive;
*) the &quot;location&quot; and &quot;server_name&quot; directives support captures in regular expressions;
*) XLST and image filters;
*) a preliminary IPv6 support;
*) nginx/Windows.
</pre>
<p>WindowsサポートとIPv6、あまり関係なさそう。。。
</p>
<p><span id="more-184"></span></p>
<p>
try_filesはファイルを探す順番を指定するディレクティブで、例えば「あるファイルが存在する場合はメンテナンス画面を表示する」ような場合、今までは if 文をいくつか書いて条件分岐させていたのをすっきりと記述することが出来るようです。設定例を見るといろいろな応用が出来るようです。
</p>
<p>
インストールはいつもの通り</p>
<pre class="brush: bash;">
./configure --with-http_stub_status_module
                   --with-http_gzip_static_module
                   --with-cc-opt='-O3'
make
make install
</pre>
<p>だけで大丈夫でした。</p>
<p>ただ、私の環境ではなぜか configure が途中で失敗していて、しばらくはまりました。結局は .bash_profile で環境変数 CC と CFLAGS を指定していたのが原因でした。nginx の configure スクリプトは autoconf で作られたものでは無いので、予想もつかないところが問題になるなぁ。
</p>
<p>nginx 0.6 系で使用していた nginx.conf で起動させたところ、2点ほど問題がありました。</p>
<ol>
<li>gzip_types に text/html を設定していると警告が出る。
<pre class="brush: bash;">
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
</pre>
<p>0.6系では上のような設定で問題ありませんでしたが、0.7 では以下の警告が出ます。</p>
<blockquote><p>
duplicate MIME type &#8220;text/html&#8221; in /etc/nginx/nginx.conf
</p></blockquote>
<p>text/html は暗黙的に設定されているようなので、以下のように修正。</p>
<pre class="brush: bash;">
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
</pre>
</li>
<li>特定のPHPファイルだけ BASIC認証させていたが、PHPとして実行されなくなった。
<p>WordPress のログインページを BASIC 認証をさせるために、以下のような設定をしていました。</p>
<pre class="brush: bash;">
    location /wordpress/wp-login.php {
        auth_basic &quot;secret&quot;;
        auth_basic_user_file htpasswd;
    }
</pre>
<p>nginx 0.6 系では BASIC 認証後に wp-login.php が実行されていましたが、0.7系では BASIC認証はされるものの、PHPファイルが実行されずにソースファイルがそのまま転送されてきました。</p>
<p>どのように修正すべきか分からなかったので、以下のように fastcgi の設定を入れて ad hoc に修正しました。</p>
<pre class="brush: bash;">
    location /wordpress/wp-login.php {
        auth_basic &quot;secret&quot;;
        auth_basic_user_file htpasswd;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
        include        fastcgi_params;
    }
</pre>
<p>こういうケースでは今後は try_files ディレクティブを使えって事なんでしょうね。
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2009/06/nginx-update-0759/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginxでphpを利用する</title>
		<link>http://blog.bluegold.me/2008/11/nginx-php-fastcgi/</link>
		<comments>http://blog.bluegold.me/2008/11/nginx-php-fastcgi/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 15:09:58 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=72</guid>
		<description><![CDATA[nginx で php を利用するための FastCGI の設定方法について説明します。]]></description>
			<content:encoded><![CDATA[<p><img src="http://wp-cdn.bluegold.me/2008/10/nginx-small.png" width="150" height="50" alt="" class="title_img" /></p>
<p>
<a href="http://nginx.net/" title="http://nginx.net/">nginx</a>からphpを利用するには、FastCGIを有効にしてphpをビルドしておく必要があります。<br />
php-5.2.6 を以下のようにビルドしました。
</p>
<p><pre class="brush: bash;">
./configure
    --with-curl=/usr --enable-fastcgi
    --enable-mbstring --enable-zend-multibyte
    --enable-mbregex --with-mysql
    --with-mcrypt --with-mhash
    --with-openssl --with-gd
    --enable-gd-native-ttf --enable-gd-jis-conv
    --with-jpeg-dir=/usr --with-xpm-dir=/usr
    --with-freetype-dir=/usr
make
make install
</pre>
</p>
<p>メールで記事を投稿する為に openssl と gd の関係のオプションを追加してます。<br />
openssl は gmail に対して POP で接続する為に、gd は<a href="http://wppluginsj.sourceforge.jp/ktai_entry/" title="Ktai Entry">Ktai Entry</a>で画像を添付したメールを処理するのに必要でした。</p>
<p>FastCGIのプロセスを以下のように起動します。</p>
<pre class="brush: bash;">
/usr/local/bin/php-cgi -q -b 127.0.0.1:9000
</pre>
</p>
<p>127.0.0.1:9000 は FastCGI の接続を待ち受ける IPアドレスとポート番号です。<br />
この値は環境に合わせて別の物に変更する事が可能です。
</p>
<p><span id="more-72"></span></p>
<p>続いて nginx 側の設定ファイルを作成します。</p>
<p><a href="/2008/10/configure-nginx/" title="blog構築メモ: nginx を設定する">前回</a>の記事で基本的な設定は nginx.conf に書いてあるので、ここでは VirtualHost の設定だけを記述します。</p>
<p>phpをFastCGIで実行するのに最低限必要な設定はこれだけです。</p>
<p><pre class="brush: bash;">
server {
    listen       80;
    server_name  bluegold.me blog.bluegold.me;
    root   /var/www/blog;
    index  index.php index.html index.htm;
&amp;lt;p&amp;gt;    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/blog/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
</pre>
</p>
<p>127.0.0.1:9000 の部分は FastCGI のプロセスのオプションと同じ値を設定します。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2008/11/nginx-php-fastcgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginx.conf についてもう少し</title>
		<link>http://blog.bluegold.me/2008/10/nginx_conf/</link>
		<comments>http://blog.bluegold.me/2008/10/nginx_conf/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 15:48:32 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=65</guid>
		<description><![CDATA[軽量ウェブサーバー nginx の設定ファイルについて、ログを Apache の combind フォーマットで出力する方法と、VirtualHost の設定方法を説明しています。]]></description>
			<content:encoded><![CDATA[<p><img src="http://wp-cdn.bluegold.me/2008/10/nginx-small.png" width="150" height="50" alt="" class="title_img" /></p>
<p><a href="/2008/10/configure-nginx/" title="blog構築メモ: nginx を設定する">前回</a>紹介したnginx.conf についてもう少し掘り下げて説明します。</p>
<h2>ログフォーマット</h2>
<pre class="brush: bash;">
    log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '
                      '&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; '
                      '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot; &quot;$gzip_ratio&quot;';

    access_log  /var/log/nginx/access.log  main;
</pre>
<p>nginx は良い所のたくさんあるソフトウェアですが、新しいだけに nginx のデフォルトのログフォーマットのままではAWStatsなどのログ解析ソフトに読み込ませる事ができません。そこで Apache の combind フォーマットと同じになるように設定しています。</p>
<p>この設定はGoogleで検索して見つけたのですが、nginx のバージョンが違うのかそのままでは <span class="caps">AWS</span>tats や Webalizer でエラーになった為、多少変更しています。$request と $status をダブルクォーテーションで囲む必要がありました。</p>
<p><span id="more-65"></span></p>
<h2>VirtualHost設定</h2>
<p>nginxの設定はとても簡単で、単純な VirtualHost の設定は以下のような少ない記述だけで十分です。</p>
<pre class="brush: bash;">
    # error catch
    server {
        listen       80;
        server_name _;
        root   /var/www/temp;

        index  index.html;

        access_log  /var/log/nginx/temp-access.log  main;
    }
</pre>
</p>
<p>root を DocumentRoot, index を DirectoryIndex と読み替えれば、Apacheの設定ができる人にはほぼ説明がいらないくらいだと思います。&#8221;server_name _;&#8221;は全てのリクエストを受け付けるVirtualHostで Host: ヘッダの指定されていないリクエストなどの他の VirtualHost が処理しないリクエストがこの VirtualHost に回ってきます。当然、サービスを提供する VirtualHost には適切な server_name を設定しておきます。</p>
<p>脆弱製のあるサーバを探しているスキャナの多くは IP アドレスを手がかりにサーバに接続してくるので、このような設定にしておく事で、スキャナのリクエストを「何も無い VirtualHost」に閉じ込めてしまう事ができます。実際にこのホストにも phpMyAdmin や xampp の管理画面を探すリクエストが来ています。</p>
<p>この設定は、codered が流行した頃に大量のアタックのログを本来のサービスのログから分離する為に始めたんですが、あまり一般的じゃないのかな。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2008/10/nginx_conf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>blog構築メモ: nginx を設定する</title>
		<link>http://blog.bluegold.me/2008/10/configure-nginx/</link>
		<comments>http://blog.bluegold.me/2008/10/configure-nginx/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 07:56:01 +0000</pubDate>
		<dc:creator>bg</dc:creator>
				<category><![CDATA[技術]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://blog.bluegold.me/?p=4</guid>
		<description><![CDATA[軽量ウェブサーバー nginx のビルド方法と設定ファイルについて簡単に説明しています。]]></description>
			<content:encoded><![CDATA[<p><img src="http://wp-cdn.bluegold.me/2008/10/nginx-small.png" width="150" height="50" alt="" class="title_img" /></p>
<p>せっかく自宅にサーバを構築するので、会社では触らないソフトを使用してみる企画。</p>
<p>第一回はWebサーバとして<a href="http://nginx.net/">nginx</a> を取り上げます。</p>
<p>nginx（えんじん えっくす）はロシアの人が作っているWebサーバで、軽量高速が特徴らしい。 <a href="http://www.wordpress.com">WordPressの本家</a> でも使っているようなので、相性も良いだろうと言う事で。</p>
<p><span id="more-4"></span></p>
<h2>nginx をビルドする</h2>
<p>tarball を取ってきて普通に configure, make, make install だけです。</p>
<pre class="brush: bash;">
tar zxvf  nginx-0.6.32.tar.gz
cd  nginx-0.6.32
./configure --with-openssl=/usr --with-http_stub_status_module
make
sudo paco -D make install
</pre>
</p>
<h2>nginx の設定</h2>
<p>nginx.conf というファイルを編集します。nginx には Apache の .htaccess に相当する設定ファイルは無いようなので、基本的には全ての設定はこのファイルに書く事になります。シンプルと言えばシンプルですが、複数の Virtual Host の設定を１つのファイルに記述すると見通しが悪くなるので、設定ファイルを複数のファイルに分けて　nginx.conf から include する事もできるようになっています。</p>
<p>試行錯誤した結果、このサイトの現在の nginx.conf は下のようになっています。</p>
<pre class="brush: bash;">
user  nobody;
worker_processes  3;

error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '
                      '&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; '
                      '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot; &quot;$gzip_ratio&quot;';

    access_log  /var/log/nginx/access.log  main;
    rewrite_log on;

    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_min_length 1100;
    gzip_buffers 4 8K;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    off;

    keepalive_timeout  65;

    client_max_body_size 10M;

    # error catch
    server {
        listen       80;
        server_name _;
        root   /var/www/temp;

        index  index.html;

        auth_basic &amp;quot;secret&amp;quot;;
        auth_basic_user_file /var/www/htpasswd;

        access_log  /var/log/nginx/temp-access.log  main;
    }

    # load the virtual server settings
    include /usr/local/nginx/conf.d/*.conf;
}
</pre>
</p>
<p>細かい設定の話は次回</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluegold.me/2008/10/configure-nginx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

