Skip to content

Instantly share code, notes, and snippets.

/*
A little node.js server for testing html5 ajax file uploads.
It serves up the current directory and receives uploads at /upload.
This is for use with xhr.send(file) where the entire request body is the file.
It just pauses one second between chunks so that client-side progress events
get a chance to fire. On my laptop it looks like the maximum chunk size is
around 40K, so you would still need images in the range of hundreds of kilobytes
to really be able to test it.
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);
/// <summary>
/// Normally, we tokenize based on white space. But there are times we want to keep words across whitespace, e.g.
/// "in basket" should not become "in" "basket". This class allows you to do that.
/// </summary>
class SpecialWordsTokenFilter : TokenFilter
{
readonly TermAttribute termAttribute;
readonly Dictionary<string, string> TwoWords = new Dictionary<string, string>();
private Queue<string> bufferBuffer = new Queue<string>();
geo $limited {
default 1;
# Wordpress Jetpack ranges:
192.0.0.0/16 0;
66.135.0.0/16 0;
66.155.0.0/16 0;
76.74.0.0/16 0;
}
http {
map $http_user_agent $limit_bots {
default '';
~*(google|bing|yandex|msnbot) $binary_remote_addr;
}
limit_req_zone $limit_bots zone=bots:10m rate=1r/m;
server {
user nobody;
worker_processes auto;
error_log logs/error.log notice;
worker_rlimit_nofile 65535;
pid /var/run/nginx.pid;
events {
accept_mutex off;
location /aaa/ {
# internal;
set $upstream "";
set $referer "";
set $ref $arg_ref;
set $url $arg_url;
set $tohost "";
set $newuri "";
# add_header Content-Type text/plain;
## Get the size of the image file using Lua.
## Cf. http://wiki.nginx.org/HttpLuaModule#set_by_lua.
location ~* \.(?:gif|jpe?g|png)$ {
set_by_lua $img_file_size '
function fsize (file)
local current = file:seek() -- get current position
local size = file:seek("end") -- get file size
file:seek("set", current) -- restore position
return size
end
# based on http://uberblo.gs/2011/06/high-performance-url-shortening-with-redis-backed-nginx
# using code from http://stackoverflow.com/questions/3554315/lua-base-converter
# "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
# example location parts of nginx.conf
# add your own AWS keys, server lines etc, and set your aws domains, paths
http {
# you will need the luacrypto in the cpath, download from http://luacrypto.luaforge.net/
lua_package_cpath "/home/justin/lua/luacrypto-0.2.0/src/l?.so.0.2.0;;";
server {
listen 80;