Help us understand the problem. What is going on with this article?

nginxで404の時だけ別サーバから取得してくる

More than 5 years have passed since last update.

ウェブインターフェースのアプリケーションで何かしらのデータを動的に生成して配信したいけど効率的な配信とかキャッシュは面倒だしnginxの力を借りたい。

proxy_storeとかを使って、nginxでは404の時だけ後ろのアプリケーションサーバに取りに行く。

server {
    listen 8888;
    server_name  localhost;

    location / {
        root /tmp/data;
        error_page 404 = @data;
    }   

    location @data {
        internal;

        proxy_pass http://localhost:4567;
        proxy_store on;
        proxy_store_access user:rw group:r all:r;
        proxy_temp_path /tmp/data/temp;

        root /tmp/data;
    }
}

こんなアプリケーションと一緒に動かす。
(とりあえずここではSinatraでサクッと)

#!/usr/bin/ruby
require 'sinatra'

get '/*' do
  "Generated at #{Time.now.to_s}\n"
end

delete '/:path' do
  path = "/tmp/data/#{params[:path]}"
  File.unlink path
  "delete #{path}\n"
end

組み合わせて使うと

$ curl http://localhost:8888/hoge
Generated at 2012-08-02 23:03:25 +0900
$ date
2012年 8月 2日 木曜日 23時03分27秒 JST
$ curl http://localhost:8888/hoge
Generated at 2012-08-02 23:03:25 +0900
$ cat /tmp/data/hoge
Generated at 2012-08-02 23:03:25 +0900

このように、初めてアクセスがあった時だけ後ろのSinatraに問い合わせてローカルに保存。そして

$ curl --request DELETE http://localhost:4567/hoge
delete /tmp/data/hoge
$ curl http://localhost:8888/hoge
Generated at 2012-08-02 23:03:40 +0900

SinatraのDELETEメソッドを叩きhogeを削除してまたアクセスすると、再度hogeを生成する。ぼちぼち使い道がありそう。

hogelog
hogelog http://hogel.org/
http://hogel.org/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした