The realization of micro-blog shortened web site

Source: Internet
Author: User
Tags curl epoll lowercase pow nginx server url forwarding

With the advent of Twitter-like microblogging sites, web site shortening services are growing due to the number of characters. This business has been growing with the addition of Web site-shortening service providers that provide Web site tracking services. The main business of bit.ly, a well-known web site, is to provide Web site shortening services for Weibo Twitter. such as Sina Weibo sinaurl.cn, Tencent Weibo url.cn and so on.

The implementation principle is very simple, mainly is the user submits the URL address translates into a unique string, this string corresponds to the real URL, how realizes this kind of transformation.

URL conversion excerpt from: http://www.cnblogs.com/sunli/archive/2010/03/25/1696183.html

The database has only two fields of seq (self-growth number) and URL (numeric URL address, indexed).

The user enters a URL address, whether the query table contains this URL, and if so, returns the number of Seq.

If not, insert the database to get a newly incremented SEQ number, and in order to shorten the number of characters, we can use the uppercase and lowercase letters of ABC. So 10 digits, 26 lowercase letters, and 26 letters of size make up a 62. For example, the number 10000000000 (10 billion) after the conversion is AUKYOA, only 6, so you can shorten a lot of URLs.

<?php//Decimal go to other system function Dec2any ($num, $base =62, $index =false) {if (! $base) {$base = strlen ($index);} elseif (! $inde X) {$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. su BSTR ($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 = Su BSTR ("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); Out }?>

After getting a shortened URL, how to implement the URL forwarding it. You can use Ttserver to shorten the network-shrinking string as a key, the real URL address as value, stored in the ttserver. The Ttserver itself provides HTTP access, and only minor modifications can be made directly using Ttserver to shorten the forwarding of URLs:

In the Ttserver source directory to find ttserver.c This file, here I use the tokyotyrant-1.1.39, jump to line No. 2981, the following lines to the figure shown:

Save exit, compile and install ttserver, there are many installation tutorials on the Web, you can refer to.

Start the Ttserver, and write a value to the inside that the key is aaaaaa,value to http://www.baidu.com.

Curl-x put http://127.0.0.10:11221/aaaaaa-d "http://www.baidu.com"

The main purpose is to use HTTP to access Ttserver directly to the real URL and do forwarding. This is convenient, but not secure, and Ttserver HTTP also supports deleting, modifying, inserting data (and, of course, modifying Ttserver HTTP access gates, masking these operations). Load balancing, can be added by adding more than a record randomly forwarded to different ttserver machines, but so that each machine stored data must be the same, online also said that Ttserver save tens of millions of data after not too stable.

The use of Nginx can be a good solution to the problem of direct use of ttserver, with Nginx filter out HTTP access ttserver Delete, modify, insert operation, and for multiple ttserver provide reverse proxy function. As shown in the following illustration:

Install Nginx, I use here is nginx-0.8.36.tar.gz. Installation Nginx Please refer to: http://blog.s135.com/nginx_php_v6.

To open the nginx.conf configuration file:

#user nobody; #启动 8 Nginx Process 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 {# with Epoll, maximum number of connections 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; # due to only forwarding, the timeout is set to 0 keepalive_timeout 0; #gzip on; # reverse proxy ttserver 1th, where I put on a machine three different ports upstream Backend_1 {server 127.0.0.10:11221 weight=5 max_fails=3; # reverse Agent Ttserver 2nd upstream backend_2 {server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s; # reverse Proxy Ttserv ER 3rd machine upstream backend_3 {server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;} server {listen; Server_na Me url.cn; #charset Koi8-r; #access_log Logs/host.access.loG Main; #当路径包含/count, the agent requests data to the Ttserver backend. #请注意, the Put,delete,post method is shielded, and only get is used, primarily for security purposes, #因为DELETE, Post,put is the location ~*/count (. *) {if ($request _) that can modify the data. method = Put) {return 403.} if ($request _method = DELETE) {return 403;} if ($request _method = POST) {return 403;} Proxy_method get; #将以 A-Z is the first character URL proxy to ttserver 1th location ~* "^/([A-z]{1}) ([a-za-z0-9]{5})" {Proxy_pass http://backend_1;} #将以 A- Z proxy for first character to Ttserver 2nd location ~* "^/([A-z]{1}) ([a-za-z0-9]{5})" {Proxy_pass http://backend_2;} #将以 0-9 is the first character URL proxy to the ttserver 3rd machine location ~* "^/([0-9]{1}) ([a-za-z0-9]{5})" {Proxy_pass http://backend_3;} #error_page 404/404.h Tml # REDIRECT Server error pages to the static page/50x.html # Error_page 502 503 504/50x.html; Location =/50x.html {root html;}} }

Save Nginx.conf Exit, now you can start the Ttserver, I do demo here, in order to facilitate the three ports on a single machine started three ttserver. As shown in figure:

Here use/ttserver/url_1 to store the data of Ttserver 1th, and so on, start ttserver in 11222, 11223, respectively.

Then start Nginx:

Ulimit-shn 65535/usr/local/nginx/sbin/nginx

Then insert the test data on the server with the following command:

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 "

Configure your machine's hosts to point to the Nginx server:

127.0.0.10 url.cn

Now we can open the browser, input http://url.cn/aaaaaa can jump to Baidu, Http://url.cn/Aaaaaa can jump to Soso, HTTP://URL.CN/1AAAAA can jump to QQ. This configuration is complete, nginx only do forwarding work, to deal with large-scale access should be no problem, this is what nginx is good at. Ttserver data value operation is also very fast, in the following can open a few more ttserver, scattered a large number of access load.

When the foreground program generates a short URL based on the URL submitted by the user, it can be written to a ttserver according to the previous Nginx distribution rules. Nginx also supports the balanced URL hash, but you need to install a third-party module Ngx_http_upstream_hash_module, specific reference: Http://blog.sina.com.cn/s/blog_ 5426e0180100dwsp.html

by xhttp.cn HTTP://WWW.XHTTP.CN/2010/07/22

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.