ThomasLau / nginx-url-shorten

Cool! Basic shortening on server with nginx , redis , lua

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nginx + redis url shorten

Created for Devel.cz Lab s.r.o. (Michal Illich)

Usage

Create shorten URL(short.devel.cz/a3) from long URL http://kinohled.cz/praha .

Requirements

Lua 5.1, nginx with modules:

Install

Copy shorten.conf to /etc/nginx/sites-available/ and create symlink to /etc/ngix/sites/enabled/ .

# Nginx configure
./configure \
 --add-module=modules/ngx_devel_kit-* \
 --add-module=moduless/set-misc-nginx-module-* \
 --add-module=modules/redis2-nginx-module \
 --add-module=modules/lua-nginx-module-*

Database scheme

  • database 0: id ~> url
  • database 1: id ~> hits
  • database 2: id ~> [{referer|user_agent}]
  • database 3: id ~> hits (when id is not found)
  • database 4: id ~> [{referer|user_agent}] (when id is not found)
  • database 5: key "count" storing the number of shortened urls; the id is generated by (this number + 1) converted to base 62

Install

 mkdir -p /var/www/shorten.localhost/ngdocs/public/
 touch "short.devel.cz"  >/var/www/shorten.localhost/ngdocs/public/index.html

Install on Production

  • replace shorten.localhost by short.devel.cz (your domain)
  • replace /shorten by secure /shorten-noh1kaa8oepae (secure URL string)
  • replace 127.0.0.1:6379 by redis.localhost:6389 (your redis server)

Test

Target: http://devel.cz/user?destination=home encode by http://meyerweb.com/eric/tools/dencoder/ to http%3A%2F%2Fdevel.cz%2Fuser%3Fdestination%3Dhome

  export Target="http%3A%2F%2Fdevel.cz%2Fuser%3Fdestination%3Dhome"
  wget -O- --post-data "url=${Target}" -q "http://shorten.localhost/shorten"
  wget --max-redirect=0 -O- -q -S $(wget -O- --post-data "url=${Target}" -q "http://shorten.localhost/shorten") 2>&1 | grep Location
  echo -e "\n  Original: "$A

Based on

Based on: https://gist.github.com/MendelGusmao/2356310

Thanks

Thanks to Lex Vjatkin and Dušan Hokův

About

Cool! Basic shortening on server with nginx , redis , lua

License:Apache License 2.0