nginx 0.7 + wsgi on Gentoo

今の開発してるサービスは、nginx 0.6.36とかをつかって開発してたのですが、
ふと気がつくと、stableは0.7.60とかになってるので、nginxのアップデートをした。

もともとwsgiを使えるようにしたnginxのebuildを0.6系のために書いていたのだけれど、それでは0.7.60はコンパイルが通らないようになってた。
Gentoo BugZillaも見てみたけど、いまいちそのまま使えるebuildはなさげだったので、自分でpatchとebuildをでっち上げた。

とりあえず、動いてるようなんだけれども、これでいいかどうかはよーわからん。
とりあえず、ebuidとパッチを貼り付けてみる。USEフラグにpythonがはいってると、mod-wsgiが組み込まれる。

基本的には、もともとportageに含まれる0.7.*のものをコピーしてきて、
wsgiをadd-moduleしているだけ。

ただ、
ngx_conf_merge_path_valueの引数が変わってるので、他のモジュールから同じような感じでパクったパッチ
ngx_http_discard_bodyていう関数はないみたいなので、ngx_http_discard_request_bodyに変更したパッチ
(ちなみに、ngx_http_discard_request_bodyのほうのパッチは0.6系でも必要。)
というパッチをあてているだけ。

ぶっちゃけnginxのソースとか見ずに、適当に書き換えたので合っているかどうか不安だけど、
とりあえず問題なくうごいているよう。

ちなみに、このebuildはUSEフラグにsslがないとコンパイルに失敗する。
md5.hをさがすconfigureのところがおかしいっぽいけど、ssl使うしまあいいやってことで放置しているので注意。

wsgi serverとして、nginx以外の選択肢として、fapws3を試してみたけど、segfaultで落ちた。。。
さすがに今日はおっかける気力がなかったので、とりあえず放置。

まあ、なにはともあれ、nginx 0.7.60にアップデートできたのでよかったよかった。

nginx-0.7.60.ebuild

inherit eutils ssl-cert

DESCRIPTION="Robust, small and high performance http and reverse proxy server"

WSGIVER="8994b058d2db"

HOMEPAGE="http://nginx.net/"
SRC_URI="http://sysoev.ru/nginx/${P}.tar.gz"
LICENSE="BSD"
SLOT="0"
KEYWORDS="amd64 ~ppc x86"
IUSE="addition debug fastcgi flv imap pcre perl random-index ssl status sub webdav zlib python"


DEPEND="dev-lang/perl dev-libs/openssl
        pcre? ( >=dev-libs/libpcre-4.2 )
        ssl? ( dev-libs/openssl )
        zlib? ( sys-libs/zlib )
        perl? ( >=dev-lang/perl-5.8 )"

pkg_setup() {
        ebegin "Creating nginx user and group"
        enewgroup nginx
        enewuser nginx -1 -1 /dev/null nginx
        eend ${?}
}

src_unpack() {
        unpack ${A}
        if use python ; then
                cd ${DISTDIR}
                wget "http://hg.mperillo.ath.cx/nginx/mod_wsgi/archive/${WSGIVER}.tar.gz"
                unpack ${WSGIVER}.tar.gz
                mv mod_wsgi-${WSGIVER} ${WORKDIR}/${P}/
                cd ${WORKDIR}/${P}/mod_wsgi-${WSGIVER}
                epatch "${FILESDIR}/nginx-all-wsgi_handler.patch"
                epatch "${FILESDIR}/nginx-0.7.60-temp_path.patch"
        fi
}

src_compile() {
        local myconf

        # threads support is broken atm.
        #
        # if use threads; then
        #       einfo
        #       ewarn "threads support is experimental at the moment"
        #       ewarn "do not use it on production systems - you've been warned"
        #       einfo
        #       myconf="${myconf} --with-threads"
        # fi

        use addition && myconf="${myconf} --with-http_addition_module"
        use fastcgi     || myconf="${myconf} --without-http_fastcgi_module"
        use fastcgi     && myconf="${myconf} --with-http_realip_module"
        use flv         && myconf="${myconf} --with-http_flv_module"
        use zlib        || myconf="${myconf} --without-http_gzip_module"
        use pcre        || {
                myconf="${myconf} --without-pcre --without-http_rewrite_module"
        }
        use debug       && myconf="${myconf} --with-debug"
        use ssl         && myconf="${myconf} --with-http_ssl_module"
        use imap        && myconf="${myconf} --with-imap" # pop3/imap4 proxy support
        use perl        && myconf="${myconf} --with-http_perl_module"
        use status      && myconf="${myconf} --with-http_stub_status_module"
        use webdav      && myconf="${myconf} --with-http_dav_module"
        use sub         && myconf="${myconf} --with-http_sub_module"
        use python      && myconf="${myconf} --add-module=mod_wsgi-${WSGIVER}"
        use random-index        && myconf="${myconf} --with-http_random_index_module"

        ./configure \
                --prefix=/usr \
                --conf-path=/etc/${PN}/${PN}.conf \
                --http-log-path=/var/log/${PN}/access_log \
                --error-log-path=/var/log/${PN}/error_log \
                --pid-path=/var/run/${PN}.pid \
                --http-client-body-temp-path=/var/tmp/${PN}/client \
                --http-proxy-temp-path=/var/tmp/${PN}/proxy \
                --http-fastcgi-temp-path=/var/tmp/${PN}/fastcgi \
                --with-md5-asm --with-md5=/usr/include \
                --with-sha1-asm --with-sha1=/usr/include \
                ${myconf} || die "configure failed"

        emake || die "failed to compile"
}

src_install() {
        keepdir /var/log/${PN} /var/tmp/${PN}/{client,proxy,fastcgi}

        dosbin objs/nginx
        cp "${FILESDIR}"/nginx-r1 "${T}"/nginx
        doinitd "${T}"/nginx

        cp "${FILESDIR}"/nginx.conf-r4 conf/nginx.conf

        dodir "${ROOT}"/etc/${PN}
        insinto "${ROOT}"/etc/${PN}
        doins conf/*

        dodoc CHANGES{,.ru} README

        use perl && {
                cd "${S}"/objs/src/http/modules/perl/
                einstall DESTDIR="${D}"|| die "failed to install perl stuff"
        }
}

pkg_postinst() {
        use ssl && {
                if [ ! -f "${ROOT}"/etc/ssl/${PN}/${PN}.key ]; then
                        dodir "${ROOT}"/etc/ssl/${PN}
                        insinto "${ROOT}"etc/ssl/${PN}/
                        insopts -m0644 -o nginx -g nginx
                        install_cert /etc/ssl/nginx/nginx
                fi
        }
}

nginx-0.7.60-temp_path.patch

--- mod_wsgi-8994b058d2db/src/ngx_http_wsgi_module.c    2008-03-27 04:35:15.000000000 +0900
+++ /tmp/nginx-0.7.60.new/mod_wsgi-8994b058d2db/src/ngx_http_wsgi_module.c      2009-06-19 16:1
3:52.000000000 +0900
@@ -62,6 +62,10 @@ static ngx_conf_enum_t  ngx_http_wsgi_re
   { ngx_null_string, 0 }
 };
 
+static ngx_path_init_t  ngx_http_wsgi_temp_path = {
+    ngx_string(NGX_HTTP_WSGI_TEMP_PATH), { 1, 2, 0 }
+};
+
 static ngx_command_t  ngx_http_wsgi_commands[] = {
 
   /* main configuration commands */
@@ -156,7 +160,7 @@ static ngx_command_t  ngx_http_wsgi_comm
     ngx_conf_set_path_slot,
     NGX_HTTP_LOC_CONF_OFFSET,
     offsetof(ngx_http_wsgi_loc_conf_t, temp_path),
-    (void *) ngx_garbage_collector_temp_handler },
+    NULL },
 
   { ngx_string("wsgi_var"),
     NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
@@ -342,10 +346,11 @@ ngx_http_wsgi_merge_loc_conf(ngx_conf_t 
   ngx_conf_merge_size_value(conf->write_buffer_size, prev->write_buffer_size,
                             (size_t) ngx_pagesize);
 
-  ngx_conf_merge_path_value(conf->temp_path,
+  ngx_conf_merge_path_value(cf, &conf->temp_path,
                             prev->temp_path,
-                            NGX_HTTP_WSGI_TEMP_PATH, 1, 2, 0,
-                            ngx_garbage_collector_temp_handler, cf);
+                            &ngx_http_wsgi_temp_path);
+//                         NGX_HTTP_WSGI_TEMP_PATH, 1, 2, 0,
+//                          ngx_garbage_collector_temp_handler, cf);
 
   ngx_conf_merge_value(conf->use_main_interpreter,
                        prev->use_main_interpreter, 0);

nginx-all-wsgi_handler.patch

diff -pruN mod_wsgi-8994b058d2db/src/ngx_http_wsgi_handler.c mod_wsgi-8994b058d2db-new/src/ngx_
http_wsgi_handler.c
--- mod_wsgi-8994b058d2db/src/ngx_http_wsgi_handler.c   2008-03-27 04:35:15.000000000 +0900
+++ mod_wsgi-8994b058d2db-new/src/ngx_http_wsgi_handler.c       2009-04-13 17:01:17.000000000 +
0900
@@ -71,7 +71,7 @@ ngx_http_wsgi_handler(ngx_http_request_t
 
   if (r->method == NGX_HTTP_GET || r->method == NGX_HTTP_HEAD) {
     /* XXX not sure */
-    rc = ngx_http_discard_body(r);
+    rc = ngx_http_discard_request_body(r);
 
     if (rc != NGX_OK && rc != NGX_AGAIN) {
       return rc;