A couple of nginx utility scripts
In my previous nginx related post I promised to show a couple of utility scripts we use to simplify managing of multiple virtual hosts both under Apache and nginx. Well, here they come
show_not_nginxed_hosts
#!/usr/bin/php <?php if(!isset($argv[1]) || !isset($argv[2])) { echo "Usage:" . basename(__FILE__) . " <apache-vhosts-file> <nginx-vhosts-file>n"; exit(1); } $APACHE_VHOSTS_FILE = $argv[1]; $NGINX_VHOSTS_FILE = $argv[2]; function extract_apache_servers($file) { $server_names = array(); preg_match_all('~ServerNames*(S+)~', file_get_contents($file), $matches); foreach($matches[1] as $match) $server_names[] = $match; return $server_names; } function extract_nginx_servers($file) { $server_names = array(); preg_match_all('~server_names*([^;]+);~', file_get_contents($file), $matches); foreach($matches[1] as $match) { foreach(explode(' ', $match) as $server_name) { if($server_name) $server_names[] = $server_name; } } return $server_names; } $apache_servers = extract_apache_servers($APACHE_VHOSTS_FILE); $nginx_servers = extract_nginx_servers($NGINX_VHOSTS_FILE); foreach(array_diff($apache_servers, $nginx_servers) as $diff) echo "$diffn";
This script simply echos which Apache virtual hosts have no nginx virtual hosts equivalents. It does so by comparing configuration files for both servers. It can be used as follows:
$ show_not_nginxed_hosts /etc/apache2/virtual_hosts.conf /etc/nginx/proxied_hosts.conf
In my company admins add Apache virtual hosts first and add nginx equivalents later(as I showed in my previous nginx post it’s possible to have some default virtual host which passes control to Apache immediately, however adding a fine tuned virtual host allows to speed up static serving drastically). Sometimes they forget about nginx virtual host and this script run by cron(which sends an email if the script produced some output) helps them to refresh their memories
gen_apache2nginx_proxy
#!/usr/bin/php <?php if(!isset($argv[1]) || !isset($argv[2])) { echo "Usage: php " . basename(__FILE__) . " <apache-vhosts-file> <ip:port> [<vhost>] n"; exit(1); } $APACHE_VHOSTS_FILE = $argv[1]; $LISTEN = $argv[2]; $VHOST = isset($argv[3]) ? $argv[3] : ''; $LOCATION_REGEX = '^.+.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|' . 'exe|pdf|ppt|txt|html?|tar|mid|midi|wav|bmp|rtf|js|swf)$'; $TEMPLATE = <<<EOD server { listen %s; server_name %s; include /etc/nginx/proxy.conf; location ~* %s { expires 30d; #access_log off; root %s; include /etc/nginx/proxy_fallback.conf; } } EOD; $apache_vhosts = spliti('<VirtualHost', file_get_contents($APACHE_VHOSTS_FILE)); if($apache_vhosts) unset($apache_vhosts[0]); foreach($apache_vhosts as $vhost) { preg_match('~ServerNames*(S+)~i', $vhost, $m); $orig_server_name = $server_name = $m[1]; //supports only one alias if(preg_match('~ServerAliass*(.*)~i', $vhost, $m)) $server_name .= ' ' . trim($m[1]); preg_match('~DocumentRoots*(S+)~i', $vhost, $m); $root = rtrim($m[1], '/'); if(!$VHOST || ($VHOST && strtolower($VHOST) == strtolower($orig_server_name))) echo sprintf($TEMPLATE, $LISTEN, $server_name, $LOCATION_REGEX, $root) . "n"; }
This script simply generates nginx virtual host server settings for the scheme I described before. It can be used as follows:
$ gen_apache2nginx_proxy /etc/apache/virtual_hosts 164.x.y.z:80 my.com
The script parses the Apache virtual hosts configuration file and outputs an nginx virtual host equivalent settings for the specified host. If the host is omitted the scripts echoes virtual hosts for each Apache virtual host.
All scripts require PHP to be installed on the server with –enable-cli, I could use perl or even bash but this time PHP is my “weapon of choice” ;). And don’t forget to cast chmod +x on these scripts.