Show HN: Building Pastebin for text using Nginx and Lua


At situations I take advantage of diversified companies for sharing textual instruct material or code snippets. Pastebin is a effectively known term for such companies (where you paste whatever you should share). The performance is moreover easy (largely) and that’s why I obvious to homicide a pastebin provider using absolute best NGINX.

NGINX and Lua

I truly had been keen on NGINX the 2d I learnt of it. In my earlier days (about 5 yrs ago), I was an Apache guy. But the 2d I grew to change into mindful of NGINX and its gigantic vitality, I never checked out any other webserver tool.

I most steadily share textual instruct material or code snippets online and that’s the reason, It used to be fun to homicide it with out using any “backend”. NGINX by myself used to be sufficient with its awesome Lua module which presents limitless probabilities. I desired to bear it easy so there isn’t great in it rather than the absolute best feature of storing undeniable-textual instruct material pastes.

NGINX Config

The final backend of this pastebin is internal of the NGINX config. The Lua code internal content_by_lua_block is all liable for saving the paste. The code is saving the pastes to the native filesystem but there are unending probabilities (for example, storing in a database fancy mysql or redis).

The next space block is liable for all /api/pastes requests. The directory to which the paste is saved must moreover be writeable by the webserver user.

# api for storing paste
space = /api/pastes {
	default_type "textual instruct material/undeniable";

		client_max_body_size 128good sufficient;
		client_body_buffer_size 128good sufficient;
	content_by_lua_block {
		native blueprint = ngx.req.get_method()

		if blueprint == "POST" then
			native args, err = ngx.req.get_post_args(1)

			if err == "truncated" then
				ngx.jabber("error: absolute best one POST parameter allowed.")

			if not args then
				ngx.jabber("error: didn't win put up args: ", err)
			native instruct material = args.instruct material

			if instruct material == nill then
				ngx.jabber("error: required parameter 'instruct material' not put of residing")
			elseif instruct material == "" then
				ngx.jabber("error: required parameter 'instruct material' is empty")

			native random = math.random
			native characteristic randid()
				native template ='xxxx4xxxxxxyxxxx'
				return string.gsub(template, '[xy]', characteristic (c)
					native v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
					return string.format('%x', v)
			native title = randid()
			native file, err = io.launch("/var/www/nginx-pastebin/pastes/" .. title, "w")
			if file == nill then
				ngx.jabber("error: couldn't launch file: ", err)
				file:write(instruct material)
			ngx.jabber("https://",, "/", title)

		elseif blueprint == "GET" then
			ngx.jabber("ship a put up quiz with 'instruct material=your_text_content'")
			ngx.jabber("error: not allowed")

Here’s an example of how companies interesting easy (and even sophisticated?) logic can even be written using Lua internal NGINX.

ngx has many other APIs and likewise you would perhaps well win almost every NGINX-linked action for your Lua code (rewriting, redirects, caching, serving instruct material and much more).

Demo and Complete Code

I truly bear put of residing this up on and the provision code has been pushed to

Read More

Leave A Reply

Your email address will not be published.