Skip to content

Instantly share code, notes, and snippets.

@Kilobyte22
Last active August 29, 2015 14:00
<html>
<head><title>${ID} ${name}</title></head>
<body bgcolor="white">
<center><h1>${ID} ${name}</h1></center>
<center><iframe width="640" height="390" src="//www.youtube.com/embed/${video}?autoplay=1" frameborder="0" allowfullscreen></iframe></center>
<hr><center>nginx, what else?</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
page_template: error.html
config_path: /etc/nginx/error_pages.cfg
error_dir: /var/www
error_path: /
file_extension: .html
pages:
403: {name: 'Forbidden', video: 'oT3mCybbhf0'}
404: {name: 'Not Found', video: 'vctPOz4RL9g'}
414: {name: 'Request-URI Too Long', video: 'q-9KqwCFDJs'}
reload_command: service nginx reload
#!/usr/bin/env ruby
$cfile = 'error_page_config.yml'
def command?(name)
system("which #{name} > /dev/null 2>&1")
end
unless File.exist? $cfile
default = <<URMOM
page_template: error.html
config_path: /etc/nginx/error_pages.cfg
error_dir: /var/www/error
error_path: /error
file_extension: .html
pages:
403: {name: 'Request-URI Too Long'}
404: {name: 'Not Found'}
reload_command: RLCMD
URMOM
default.gsub! 'RLCMD',
if command? 'systemctl' # systemd based
'systemctl reload nginx'
elsif File.exist? '/usr/sbin/service' # whatever
'/usr/sbin/service nginx reload'
else
'echo \'\e[0;31mWarning: not reloaded, please specify a reload_command in config\e[0m\''
end
File.write($cfile, default)
puts "Generated default config in #{$cfile}"
exit 0
end
exec 'sudo', *ARGV.unshift($0) if Process.euid != 0 # root me if i am not yet
require 'yaml'
$config = YAML.load_file($cfile)
$page_template = File.read($config['page_template'])
$config_path = $config['config_path']
$error_path = $config['error_path']
$error_path += '/' unless $error_path =~ /\/$/
$error_dir = $config['error_dir']
$error_dir += '/' unless $error_dir =~ /\/$/
$reload_command = $config['reload_command']
$ext = $config['file_extension']
$pages = $config['pages']
$pages.each_pair do |k, v|
file = "#{$error_dir}#{k}#{$ext}"
puts "Generating #{file}..."
data = $page_template.gsub '${ID}', k.to_s
v.each_pair do |name, value|
data.gsub! "${#{name}}", value
end
File.write file, data
end
$cfg = ($pages.keys.map {|v| "error_page #{v} #{$error_path}#{v}#{$ext};"}.join "\n") + "\n"
$cfg = "# File is computer generated, do not edit manually or changes will get overwritten.\n# include this file in your server block in nginx config\n#{$cfg}"
File.write $config_path, $cfg
puts "reloading nginx config... [#{$reload_command}]"
if system($reload_command)
puts 'Success.'
else
puts 'Failed.'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment