Skip to content

Instantly share code, notes, and snippets.

@ornicar
Last active December 27, 2015 17:39
Show Gist options
  • Save ornicar/7364005 to your computer and use it in GitHub Desktop.
Save ornicar/7364005 to your computer and use it in GitHub Desktop.
Pow (http://pow.cx/) functionality for Linux & nginx.
#!/usr/bin/env ruby
# Must run as root because of `pkill -HUP nginx`
# ---- START OF CONFIG
$sleep = 1
$pow_dir = ENV['HOME'] + '/.pow/'
$sites_dir = ENV['HOME'] + '/.nginx-sites/'
# ---- END OF CONFIG
def refresh_nginx()
system('pkill -HUP nginx')
end
# Immediately verify that the user is allowed to do so
refresh_nginx()
def create(name, port)
config = """server {
server_name %s.dev ~^.+\.%s.dev$;
location / {
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:%s/;
}
}""" % [name, name, port]
File.write($sites_dir + name, config)
refresh_nginx
end
while true do
pows = Dir[$pow_dir + '*'].map { |f| File.basename(f) }
sites = Dir[$sites_dir + '*'].map { |f| File.basename(f) }
pows.each do |pow|
create(pow, File.read($pow_dir + pow)) unless sites.include? pow
end
sites.each do |site|
File.delete($sites_dir + site) unless pows.include? site
end
system('inotifywait --event CREATE --event DELETE ' + $pow_dir)
end

Create required dirs:

mkdir ~/.pow # where play will register the http.port
mkdir ~/.nginx-sites # where pow will create proxy configs

Tell nginx to load sites config from your ~/.nginx-sites by adding this to /etc/nginx.conf:

http {
    ...
    
    include /home/<user>/.nginx-sites/*;
}

Install dnsmasq and add this to /etc/dnsmask.conf:

address=/dev/127.0.0.1

Setup is complete. To run the daemon script:

sudo linux-nginx-pow

The script will run forever, watching for ~/.pow/* files and creating nginx proxy configs accordingly in ~/.nginx-sites. It has to be run as root, so it can refresh nginx config with pkill -HUP nginx.

@cchantep
Copy link

cchantep commented Nov 8, 2013

:+1 (there may even be some ruby lib to use inotify direct)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment