隨著類似Twitter的微型博客網(wǎng)站的出現(xiàn),由于字符數(shù)的限制,網(wǎng)址縮短服務(wù)日漸增多。加上網(wǎng)址縮短服務(wù)提供商提供網(wǎng)址追蹤等服務(wù),這一業(yè)務(wù)日漸興起。知名網(wǎng)址縮短服務(wù)商Bit.ly的主要業(yè)務(wù)便是為微博Twitter提供網(wǎng)址縮短服務(wù)。 比如sina微博的sinaurl.cn,騰訊微博的url.cn等。
實(shí)現(xiàn)原理很簡單,主要是將用戶提交的 url 地址轉(zhuǎn)化成一個(gè)唯一的字串,這個(gè)字串就對(duì)應(yīng)著真實(shí)的 url,怎么樣實(shí)現(xiàn)這種轉(zhuǎn)換呢?
url 的轉(zhuǎn)換摘自:http://www.cnblogs.com/sunli/archive/2010/03/25/1696183.html
數(shù)據(jù)庫只有兩個(gè)字段seq(自增長數(shù)字)和url(數(shù)字的url地址,建立索引)。
用戶輸入一個(gè)url地址,查詢表是否包含此url,如果存在,則返回seq的數(shù)字,如果不存在,則插入數(shù)據(jù)庫,得到一個(gè)新增加的自增seq數(shù)字,為了縮短數(shù)字占用的字符數(shù),我們可以把a(bǔ)bc等字母的大小寫用上。這樣10個(gè)數(shù)字,26個(gè)小寫字母,26個(gè)大小字母就組成了一個(gè)62進(jìn)制了。比如數(shù)字 10000000000(100億)轉(zhuǎn)換后就是aUKYOA,只有6位了,這樣就能縮短很多的網(wǎng)址了。
- <?php
- //十進(jìn)制轉(zhuǎn)到其他制
- function dec2any($num, $base=62, $index=false)
- {
- if (!$base)
- {
- $base = strlen($index);
- }
- elseif(!$index)
- {
- $index = substr("0123456789abcdefghijklmnopqrstuvwxyz
- ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );
- }
- $out = "";
- for ($t = floor(log10($num) / log10($base )); $t >= 0; $t-- )
- {
- $a = floor( $num / pow( $base, $t ) );
- $out = $out . substr( $index, $a, 1 );
- $num = $num - ( $a * pow( $base, $t ) );
- }
- return $out;
- }
- function any2dec($num, $base=62, $index=false)
- {
- if (!$base)
- {
- $base = strlen( $index );
- }
- elseif(!$index)
- {
- $index = substr("0123456789abcdefghijklmnopqrstuvwxyz
- ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base);
- }
- $out = 0;
- $len = strlen($num) - 1;
- for ($t = 0; $t <= $len; $t++)
- {
- $out = $out + strpos($index, substr($num, $t, 1 )) * pow($base, $len - $t);
- }
- return $out;
- }
- ?>
得到縮短的網(wǎng)址以后,怎樣實(shí)現(xiàn)網(wǎng)址的轉(zhuǎn)發(fā)呢?可以利用 ttserver,將縮短網(wǎng)縮字串當(dāng)作key,真實(shí)的 url 地址當(dāng)作 value,存入ttserver中。ttserver本身就提供 http 訪問,只需要稍加修改就可以直接利用 ttserver 進(jìn)行縮短網(wǎng)址的轉(zhuǎn)發(fā):
在 ttserver 源碼目錄下找到 ttserver.c 這個(gè)文件,這里我用的是 tokyotyrant-1.1.39 ,跳到第 2981 行,將下面的幾行改成圖中所示:

保存退出,編譯安裝 ttserver,網(wǎng)上有很多安裝教程,可以參考。
啟動(dòng) ttserver,并向里面寫入一條 key 為 aaaaaa,value為 http://www.baidu.com 的值。
- curl -X PUT http://127.0.0.10:11221/aaaaaa -d "http://www.baidu.com"
主要目的是用 http 訪問 ttserver 時(shí)直接取得到真實(shí)的 url 并做轉(zhuǎn)發(fā)。這樣做很方便,但不安全,ttserver 的 http 還支持刪除、修改、插入數(shù)據(jù)(當(dāng)然也可以修改 ttserver 的 http 訪問入口,屏蔽掉這幾種操作)。負(fù)載均衡方面,可以通過添加多條 A 記錄隨機(jī)轉(zhuǎn)發(fā)到不同的 ttserver 機(jī)器上,但這樣每臺(tái)機(jī)器上存放的數(shù)據(jù)必須相同,網(wǎng)上也有說過ttserver 存過千萬左右的數(shù)據(jù)以后不太穩(wěn)定。
利用 nginx 就能很好解決直接用 ttserver 的問題,用 nginx 過濾掉 http 訪問 ttserver 的刪除、修改、插入的操作,并為多臺(tái) ttserver 提供反向代理的功能。如下圖所示:

安裝 nginx,我這里采用的是 nginx-0.8.36.tar.gz。安裝 nginx 請(qǐng)參考:http://blog.s135.com/nginx_php_v6。
打開 nginx.conf 配置文件:
- #user nobody;
- #啟動(dòng) 8 個(gè) nginx 進(jìn)程
- worker_processes 8;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- # 用 epoll,最大連接數(shù)
- use epoll;
- worker_connections 65535;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- # 由于只做轉(zhuǎn)發(fā),將超時(shí)時(shí)間設(shè)為 0
- keepalive_timeout 0;
- #gzip on;
- # 反向代理 ttserver 1 號(hào)機(jī),這里我放在一臺(tái)機(jī)器上開了三個(gè)不同端口
- upstream backend_1 {
- server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;
- }
- # 反向代理 ttserver 2 號(hào)機(jī)
- upstream backend_2 {
- server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;
- }
- # 反向代理 ttserver 3 號(hào)機(jī)
- upstream backend_3 {
- server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;
- }
- server {
- listen 80;
- server_name url.cn;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- #當(dāng)路徑包含/count的時(shí)候,則代理到ttserver后端進(jìn)行請(qǐng)求數(shù)據(jù)。
- #請(qǐng)注意,這里屏蔽了PUT,DELETE,POST方法,只是使用了GET,主要目的是為了安全性,
- #因?yàn)?span id="0yi22am" class="func">DELETE,POST,PUT是可以修改數(shù)據(jù)的
- location ~* /count(.*) {
- if ($request_method = PUT ) {
- return 403;
- }
- if ($request_method = DELETE ) {
- return 403;
- }
- if ($request_method = POST ) {
- return 403;
- }
- proxy_method GET;
- }
- #將以 a-z 為第一個(gè)字符的 url 代理到 ttserver 1 號(hào)機(jī)
- location ~* "^/([a-z]{1})([a-zA-Z0-9]{5})" {
- proxy_pass http://backend_1;
- }
- #將以 A-Z 為第一個(gè)字符的 url 代理到 ttserver 2 號(hào)機(jī)
- location ~* "^/([A-Z]{1})([a-zA-Z0-9]{5})" {
- proxy_pass http://backend_2;
- }
- #將以 0-9 為第一個(gè)字符的 url 代理到 ttserver 3 號(hào)機(jī)
- location ~* "^/([0-9]{1})([a-zA-Z0-9]{5})" {
- proxy_pass http://backend_3;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
保存 nginx.conf 退出,現(xiàn)在就可以啟動(dòng) ttserver了,我這里做演示,為了方便就在一臺(tái)機(jī)器的三個(gè)端口啟動(dòng)了三個(gè) ttserver。如圖:
這里用 /ttserver/url_1 存放 ttserver 1號(hào)機(jī)的數(shù)據(jù),依此類推,分別在 11222、11223啟動(dòng) ttserver。
接著啟動(dòng) nginx:
- ulimit -SHn 65535
- /usr/local/nginx/sbin/nginx
接著在服務(wù)器上用下面的命令插入測試數(shù)據(jù):
- curl -X PUT http://127.0.0.10:11221/aaaaaa -d "http://www.baidu.com"
- curl -X PUT http://127.0.0.10:11222/Aaaaaa -d "http://www.soso.com"
- curl -X PUT http://127.0.0.10:11223/1aaaaa -d "http://www.qq.com"
配置你機(jī)器的 hosts 指向 nginx 服務(wù)器:
- 127.0.0.10 url.cn
現(xiàn)在我們就可以打開瀏覽器,輸入 http://url.cn/aaaaaa 就可以跳轉(zhuǎn)到 baidu 上了,http://url.cn/Aaaaaa 就可以跳轉(zhuǎn)到 soso 了,http://url.cn/1aaaaa 就可以跳轉(zhuǎn)到 qq 上。至此配置完成,nginx只做轉(zhuǎn)發(fā)工作,應(yīng)付大規(guī)模的訪問應(yīng)該沒什么問題,這也正是 nginx 所擅長的。ttserver 數(shù)據(jù)的取值操作也是很快的,在后面可以多開幾臺(tái) ttserver,分散大量訪問時(shí)的負(fù)載。
前臺(tái)程序根據(jù)用戶提交的 url 生成短的 url 后,根據(jù)前面的 nginx 分發(fā)規(guī)則寫到某一臺(tái) ttserver 中,就可以了。nginx還支持一直 url hash 的均衡,但需要安裝一個(gè)第三方模塊ngx_http_upstream_hash_module,具體可以參考:http://blog.sina.com.cn/s/blog_5426e0180100dwsp.html
by xhttp.cn http://www.xhttp.cn/2010/07/22
原文:http://www.javaeye.com/topic/718695
