A dynamic web-site written wholly in nginx.conf? Introducing mdoc.su!

Constantine A. Murenin cnst++ at FreeBSD.org
Mon Feb 18 08:06:08 UTC 2013


This is a multi-part message in MIME format.
--------------030707040502000103070707
Content-Type: text/plain; charset=KOI8-R; format=flowed
Content-Transfer-Encoding: 7bit

Dear nginx@,

I'm not sure if this has already been done before and to what extent, 
but I'd like to demonstrate that a whole web-service "portal" can be 
written exclusively in nginx.conf, without any php, perl, python, java 
or cgi, or even any files external to nginx.conf.

Introducing  http://mdoc.su/ .

The service provides deterministic URL tinyfication / URI shortening for 
BSD manual pages.

How does it work?

It operates on 3 inputs: the operating system, the section and the 
manual page.

For example, to see the kqueue(2) manual page from FreeBSD, you can 
point your browser to  http://mdoc.su/f/kqueue , or mdoc.su/f/kqueue.2, 
or mdoc.su/f/2/kqueue, or you can even use "FreeBSD" or "freebsd" in 
place of "f", as in, http://mdoc.su/freebsd/kqueue etc.

When nginx receives the request, it quickly gets re-written, and a 
redirect to FreeBSD.org/cgi/man.cgi is produced.

Same for OpenBSD, NetBSD and DragonFly BSD, of course.

Forgot how to specify timeouts for ssh(1)?  http://mdoc.su/o/ssh

The site even has a start page, also exclusively through nginx.conf, and 
supports Google Webmaster Tools site verification, through the "HTML 
file upload" option, through nginx.conf (of course!).  (BTW, the format 
of those verification files has changed a couple of years back, where 
special and unique file content is now required, and the new file format 
itself is a very-very big secret, that Google will not share with anyone 
without a file-save-capable browser or an NDA!  Reverse-engineered with 
nginx.conf, too!)

Notice that the whole ordeal runs entirely out of nginx and is 
controlled by an nginx.conf file, which I think is pretty nifty. :-)

The source code is available at https://github.com/cnst/mdoc.su , and 
might also be attached inline to this message.

Comments, questions and suggestions are very welcome.

Best regards,
Constantine.

--------------030707040502000103070707
Content-Type: text/plain; charset=KOI8-R;
 name="mdoc.su.nginx.conf"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="mdoc.su.nginx.conf"

# cnst: mdoc.su.nginx.conf, 2013-02-14/17
# Deterministic URL shortener for BSD manual pages, written in nginx.conf
# Copyright (c) 2013 Constantine A. Murenin <cnst++ at FreeBSD.org>
# http://mdoc.su/
# https://github.com/cnst/mdoc.su
server {
	listen *:80;
	listen [::]:80;
	server_name
		mdoc.su
		www.mdoc.su
		*.mdoc.su;
	if ($host != "mdoc.su") {
		rewrite	^	http://mdoc.su$request_uri?	redirect;
	}
	location = / {
		default_type	text/html;
		return	200
"<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-CA' lang='en-CA'>
<head>
<title>mdoc.su — Manual Pages for FreeBSD, OpenBSD, NetBSD and DragonFly BSD!</title>
<meta name='description' content='Deterministic URL shortener for BSD manual pages, written in nginx.conf'/>
</head>
<body>
<div style='text-align: center;'>
<h1 style='margin: 4pt auto; color: navy;'>mdoc.su</h1>
<h2 style='margin: 4pt auto;'>man pages for FreeBSD, NetBSD, OpenBSD and DragonFly</h2>
<hr style='width: 48em; font-family: monospace; border: none; border-top: 1px solid navy;'/>
<pre style='margin: 0 auto; width: 48em; font-family: monospace; text-align: left;'>Usage:
	<strong>mdoc.su/b/p</strong>
		or
	<strong>mdoc.su/b/0/p</strong>
		or
	<strong>mdoc.su/b/p.0</strong>
	, where
		b is
			f|n|o|d, or 
			FreeBSD|NetBSD|OpenBSD|DragonFly, or 
			same lower case
	, and
		p is the name of the manual page
	, and
		0 is the section number
	.

Now, what's mdoc?
See:
	<a href='http://mdoc.su/f/mdoc'>http://mdoc.su/f/mdoc</a> — according to FreeBSD
	<a href='http://mdoc.su/n/mdoc'>http://mdoc.su/n/mdoc</a> — according to NetBSD
	<a href='http://mdoc.su/o/mdoc'>http://mdoc.su/o/mdoc</a> — according to OpenBSD
	<a href='http://mdoc.su/d/mdoc'>http://mdoc.su/d/mdoc</a> — according to DragonFly

Or, if you will,
	<a href='http://mdoc.su/f/mdoc.7'>http://mdoc.su/f/mdoc.7</a>
	<a href='http://mdoc.su/f/7/mdoc'>http://mdoc.su/f/7/mdoc</a>

</pre>
<hr style='width: 48em; font-family: monospace; border: none; border-top: 1px solid darkred;'/>
<p><small>© 2013 Constantine A. Murenin (cnst)</small></p>
<hr/>
<p>nginx/$nginx_version at $host</p>
</div>
</body>
</html>";
	}
	location = /google2a7d1d40a6b37a23.html {
		rewrite ^/(.*)	$1;
		return 200 "google-site-verification: $uri";
	}
	location /FreeBSD {	rewrite	^/FreeBSD(/.*)?$	/f$1;	}
	location /f {
		set	$fb	"http://www.freebsd.org/cgi/man.cgi?query=";
		set	$fs	"&sektion=";
		rewrite	^/freebsd(/.*)?$	/.$1;
		rewrite	^/./([^/.]+)/([^/]+)$		$fb$2$fs$1	redirect;
		rewrite	^/./([^/]+)\.([1-9])$		$fb$1$fs$2	redirect;
		rewrite	^/./([^/]+)$			$fb$1$fs	redirect;
		rewrite	^/./?$	/	last;
		return	404;
	}
	location /NetBSD {	rewrite	^/NetBSD(/.*)?$	/n$1;	}
	location /n {
		set	$nb	"http://netbsd.gw.com/cgi-bin/man-cgi?";
		rewrite	^/netbsd(/.*)?$	/.$1;
		rewrite	^/./([a-z]+[0-9]*[kx]?)/([^/]+)/([^/]+)$	$nb$3+$2.$1	redirect;
		rewrite	^/./([^/]+)/([^/]+)$		$nb$2+$1	redirect;
		rewrite	^/./([^/]+)\.([1-9]\.[a-z]+[0-9]*[kx]?)$	$nb$1+$2	redirect;
		rewrite	^/./([^/]+)\.([1-9])$		$nb$1+$2	redirect;
		rewrite	^/./([^/]+)$			$nb$1	redirect;
		rewrite	^/./?$	/	last;
		return	404;
	}
	location /OpenBSD {	rewrite	^/OpenBSD(/.*)?$	/o$1;	}
	location /o {
		set	$ob	"http://www.openbsd.org/cgi-bin/man.cgi?query=";
		set	$os	"&sektion=";
		rewrite	^/openbsd(/.*)?$	/.$1;
		rewrite	^/./([a-z]+[0-9]*[k]?)/([1-9]|3p)/([^/]+)$	$ob$3$os$2&arch=$1	redirect;
		rewrite	^/./([^/.]+)/([^/]+)$		$ob$2$os$1	redirect;
		rewrite	^/./([^/]+)\.([1-9]|3p)\.([a-z]+[0-9]*[k]?)$	$ob$1$os$2&arch=$3	redirect;
		rewrite	^/./([^/]+)\.([1-9]|3p)$	$ob$1$os$2	redirect;
		rewrite	^/./([^/]+)$			$ob$1$os	redirect;
		rewrite	^/./?$	/	last;
		return	404;
	}
	location /DragonFly {	rewrite	^/DragonFly(BSD)?(/.*)?$	/d$2;	}
	location /d {
		set	$db	"http://leaf.dragonflybsd.org/cgi/web-man?command=";
		set	$ds	"&section=";
		rewrite	^/dragonfly(bsd)?(/.*)?$	/d$2;
		rewrite	^/d(ragon)?fly(/.*)?$	/d$2;
		rewrite	^/./([^/.]+)/([^/]+)$		$db$2$ds$1	redirect;
		rewrite	^/./([^/]+)\.([1-9])$		$db$1$ds$2	redirect;
		rewrite	^/./([^/]+)$			$db$1$ds	redirect;
		rewrite	^/./?$	/	last;
		return	404;
	}
	location / {	return 403;	}
	access_log	logs/mdoc.su/mdoc.su.access.log	combined;
	error_log	logs/mdoc.su/mdoc.su.error.log	warn;
}

--------------030707040502000103070707--



More information about the nginx mailing list