Skip to content

Instantly share code, notes, and snippets.

View perusio's full-sized avatar

António P. P. Almeida perusio

View GitHub Profile
@perusio
perusio / gist:1326701
Created October 31, 2011 01:28
Mobile device detection in Nginx with just 7 lines of configuration
### Testing if the client is a mobile or a desktop.
### The selection is based on the usual UA strings for desktop browsers.
## Testing a user agent using a method that reverts the logic of the
## UA detection. Inspired by notnotmobile.appspot.com.
map $http_user_agent $is_desktop {
default 0;
~*linux.*android|windows\s+(?:ce|phone) 0; # exceptions to the rule
~*spider|crawl|slurp|bot 1; # bots
~*windows|linux|os\s+x\s*[\d\._]+|solaris|bsd 1; # OSes
map $request_uri $cache_valitidy {
default 5m;
~this-regex-uri 2h;
}
## Then do like this in the cache definition:
proxy_cache_valid 200 301 $cache_validity;
## For fastcgi cache.
fastcgi_cache_valid 200 301 $cache_validity;
function doHash(str, seed) {
var m = 0x5bd1e995;
var r = 24;
var h = seed ^ str.length;
var length = str.length;
var currentIndex = 0;
while (length >= 4) {
var k = UInt32(str, currentIndex);
@perusio
perusio / gist:1497464
Created December 19, 2011 14:32
How to have an "inline" robots.txt for development/private setups
## Here's the way to have Nginx return a robots.txt file that disallows all crawling by bots.
## This is useful for development and private sites.
location = /robots.txt {
return 200 "User-agent: *\nDisallow: /\n";
}
@perusio
perusio / gist:1518163
Created December 24, 2011 19:32
Better cookies using /dev/urandom
; Add this to your php.ini. E.g., to /etc/php5/fpm/php.ini (with PHP-FPM on Debian based systems).
; Increase the entropy of your session token. The default setting is 0 (disabled).
session_entropy_length = 32
; OS based random data generator as source for entropy.
session.entropy_file = /dev/urandom
@perusio
perusio / drupal_uid_header.module
Created December 27, 2011 07:31
Simple module to add a header with the user ID
<?php
/**
* @file drupal_uid_header.module
* @author António P. P. Almeida <appa@perusio.net>
* @date Tue Dec 27 2011
*
* @brief Sets an HTTP header with the UID (Drupal 6).
*
*/
@perusio
perusio / pingfcgi.sh
Created January 15, 2012 19:31 — forked from mpasternacki/pingfcgi.sh
Ping FCGI server, using cgi-fcgi program from libfcgi library.
#!/bin/sh
set -e
# Ping FCGI server. Uses cgi-fcgi program from libfcgi library.
# Retrieves the root path (/) from host:port specified on command line.
if [ -z "$1" ] ; then
echo "Usage: $0 host:port|path/to/socket" >&2
exit 1
fi
@perusio
perusio / sopa_blackout.conf
Created January 18, 2012 14:11
Nginx SOPA HTML5 blackout page in just 5 lines
### Here's a very simple SOPA blackout page.
## Include this at the top of your vhost configuration (server block): 'include /path/to/sopa_blackout.conf;'
error_page 503 @sopa;
return 503;
location @sopa {
default_type text/html;
return 503 "<!DOCTYPE html><html><head><meta charset='UTF-8'/><style>body{color: white; background-color: black;}</sytle></head><body><center><h1>We're down due to SOPA blackout!</h1></center></body></html>\n";
}
@perusio
perusio / gist:1644446
Created January 20, 2012 01:39
Integer division in PHP
<?php
// Integer division in PHP.
function div($x, $y) {
if ($x == 0) {
return 0;
}
elseif ($y == 0) {
return FALSE;
}
else {
@perusio
perusio / special_header_microcache.module
Created January 20, 2012 20:14
Module that sends an header for disabling microcaching.
<?php
/**
* Implements hook_init().
*/
function drupal_microcache_header_init() {
drupal_add_http_header('X-Accel-Expires', 0);
} // drupal_microcache_header_init