├── LICENSE
├── README.md
├── conf
├── nginx.conf
└── lua
│ ├── n3r
│ └── urlshortener_eval.lua
│ └── resty
│ └── redis.lua
└── html
└── shorten-ui.html
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 sanshi0518
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | shorturl-nginx
2 | ----------
3 |
4 | A URL Shortener with analytics based on Nginx and Redis
5 |
6 |
7 | Setup
8 | ---------
9 |
10 | - Start a redis instance
11 | - Build your nginx with **lua-nginx-module** and **set-misc-nginx-module**
12 | - Copy the lua script conf/lua/n3r/**urlshortener_eval.lua** to your own nginx **conf/lua/n3r/** directory, and update the redis configurations in the **connect** function
13 | - Copy the html/**shorten-ui.html** to your own nginx **html** directory
14 | - Download [lua-resty-redis](https://github.com/agentzh/lua-resty-redis) and copy the lib/resty/**redis.lua** to your own nginx **conf/lua/resty/** directory
15 | - Add the follow configurations to your nginx.conf, then start your nginx
16 |
17 | > **http block:** lua_package_path
18 | >
19 | > **server block:** location a)shorten-ui.html b)shorten c)^/0[0-9a-zA-Z]{1,12}$
20 | >
21 | > You could find these configurations in my **conf/nginx.conf**
22 |
23 | Basic Usage
24 | ---------
25 |
26 | #### Create a short url
27 | http://localhost:8088/shorten?url=http://www.google.com
28 | > **NOTE:** Do not forget the **http://** prefix within the **url** parameter
29 |
30 | #### Visit the short url
31 | Just visit the url that the previous step returned
32 |
33 | #### Web UI
34 | http://localhost:8088/shorten-ui.html
35 | > **NOTE:** In this page, you could input a URL, then get the shorten form
36 |
37 | #### Create a random short url
38 | http://localhost:8088/shorten?url=http://www.google.com&random=true
39 |
40 | #### Query source url by the short
41 | http://localhost:8088/shorten-qrybyshort?url=http://t.cn/0asd
42 |
--------------------------------------------------------------------------------
/conf/nginx.conf:
--------------------------------------------------------------------------------
1 | #user nobody;
2 | worker_processes 1;
3 |
4 | #error_log logs/error.log;
5 | #error_log logs/error.log notice;
6 | #error_log logs/error.log info;
7 |
8 | #pid logs/nginx.pid;
9 |
10 |
11 | events {
12 | worker_connections 1024;
13 | }
14 |
15 |
16 | http {
17 | include mime.types;
18 | #default_type application/octet-stream;
19 | default_type text/plain;
20 |
21 | sendfile on;
22 |
23 | #keepalive_timeout 0;
24 | keepalive_timeout 65;
25 |
26 | lua_package_path ';;$prefix/conf/lua/?.lua;';
27 |
28 | server {
29 | listen 8088;
30 | server_name localhost;
31 |
32 |
33 | location /shorten-ui.html {
34 | expires epoch;
35 | root html;
36 | }
37 |
38 | #error_page 404 /404.html;
39 |
40 | # redirect server error pages to the static page /50x.html
41 | error_page 500 502 503 504 /50x.html;
42 | location = /50x.html {
43 | root html;
44 | }
45 |
46 | location = /shorten {
47 | add_header Content-Type text/plain;
48 |
49 | content_by_lua '
50 | local shorten = require "n3r.urlshortener_eval"
51 | shorten.pack(ngx.var.arg_url, ngx.var.arg_prefix, ngx.var.arg_random)
52 | ';
53 | }
54 |
55 | location ~ "^/0[0-9a-zA-Z]{1,12}$" {
56 | content_by_lua '
57 | local shorten = require "n3r.urlshortener_eval"
58 | local notFoundRedirect = "http://www.10010.com"
59 | shorten.unpack(notFoundRedirect)
60 | ';
61 | }
62 |
63 | location = /shorten-qrybyshort {
64 | add_header Content-Type text/plain;
65 |
66 | content_by_lua '
67 | local shorten = require "n3r.urlshortener_eval"
68 | shorten.qry_by_short(ngx.var.arg_url)
69 | ';
70 | }
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/html/shorten-ui.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |