nginx で cocproxy みたいなこと
cocproxy とかいうのを使うと、よそのサイトの一部ファイルを書き換えたりできて便利ですよ、という話を聞いた。 部分置換とかはキツそうだけど、ファイル置き換えくらいなら nginx 単体でも似たようなことが出来そうな気がしたので、考えてみた。
nginx-cocproxy.conf:
daemon off;
error_log  /dev/stderr debug;
worker_processes  1;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    client_body_temp_path temp/client;
    proxy_temp_path       temp/proxy;
    fastcgi_temp_path     temp/fastcgi;
    uwsgi_temp_path       temp/uwsgi;
    scgi_temp_path        temp/scgi;
    access_log  /dev/stdout;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       3128;
        server_name  _;
        resolver 127.0.0.1;
        location / {
            root files;
            # XXX: basename 取る方法が分からん
            try_files /$http_host/$uri $uri @origin;
        }
        location @origin {
            proxy_pass $scheme://$http_host$request_uri;
        }
    }
}
nginx -p ./ -c nginx-cocproxy.conf みたいに起動してブラウザの proxy に設定してやれば、
./files/example.com/test/foo/bar.css./files/test/foo/bar.css
の順でファイルを探して置き換えてくれる。
./files/bar.css./files/example.com/bar.css
からも探せるようにする。