nginx 有个 memcache 的模块,可以读取 memcache 里的数据。但这个模块很简单,只能get,不能set。如果要set 就必须依靠别的方式了。
路过见到,那我就玩玩啦,尝试下不用动态语言实现 url shorten 。
安装 memc module ,按照wiki 上面说的安装吧。
启动 memcache ,memcached -d 。
nginx 配置下,然后启动nginx
…
location / {
root html;
index index.html index.htm;
rewrite “^(.*)$” /hello?key=$1 last;
}
location /hello {
memc_pass 127.0.0.1:11211 ;
set $memc_key $arg_key ;
}
…
写html ,写个form 吧,用 post 把原始的url 提交给 nginx ,然后nginx 在对 memcache 做set 操作。这个是 value 部分,还有一个key 的部分,可以用js 生成一个随机数提交。
这里,我用curl 模拟一下这个 form 的动作,其中,就当我的随机数是 hello 吧
curl –data “<html><body><meta http-equiv=\”refresh\” content=\”0;url=http://blog.helosa.org\”></body></html>” http://127.0.0.1/hello?key=/hello
在这句话里,
key 是 /hello
value 是 <html><body><meta http-equiv=”refresh” content=”0;url=http://blog.helosa.org“></body></html>
然后,我们访问 127.0.0.1/163 是,就会跳转到我的blog首页了。
哈哈,url shorten 的服务就是这样实现了,实在有点山寨!