From 2655f9315464236f2e41d2b6467d9cf97cb2b34b Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Fri, 30 Mar 2012 11:15:14 -0400
Subject: [PATCH] - nginx: added function to merge duplicate locations in the nginx vhost configuration.
---
server/conf/nginx_vhost.conf.master | 2
server/plugins-available/nginx_plugin.inc.php | 70 ++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 2 deletions(-)
diff --git a/server/conf/nginx_vhost.conf.master b/server/conf/nginx_vhost.conf.master
index d771eb1..c958969 100644
--- a/server/conf/nginx_vhost.conf.master
+++ b/server/conf/nginx_vhost.conf.master
@@ -118,7 +118,7 @@
</tmpl_loop>
<tmpl_loop name="basic_auth_locations">
- location ^~ <tmpl_var name='htpasswd_location'> {
+ location <tmpl_var name='htpasswd_location'> {
auth_basic "Members Only";
auth_basic_user_file <tmpl_var name='htpasswd_path'>.htpasswd;
}
diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index 42d6120..69cd032 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -911,7 +911,7 @@
if(file_exists($vhost_file)) copy($vhost_file,$vhost_file.'~');
//* Write vhost file
- file_put_contents($vhost_file,$tpl->grab());
+ file_put_contents($vhost_file,$this->nginx_merge_locations($tpl->grab()));
$app->log('Writing the vhost file: '.$vhost_file,LOGLEVEL_DEBUG);
unset($tpl);
@@ -1600,6 +1600,74 @@
}
}
+ private function nginx_merge_locations($vhost_conf){
+
+ $lines = explode("\n", $vhost_conf);
+
+ if(is_array($lines) && !empty($lines)){
+
+ $locations = array();
+ $islocation = false;
+ $linecount = sizeof($lines);
+
+ for($i=0;$i<$linecount;$i++){
+ $l = trim($lines[$i]);
+ if(substr($l, 0, 8) == 'location' && !$islocation){
+
+ $islocation = true;
+ $level = 0;
+
+ // Remove unnecessary whitespace
+ $l = preg_replace('/\s\s+/', ' ', $l);
+
+ $loc_parts = explode(' ', $l);
+ // see http://wiki.nginx.org/HttpCoreModule#location
+ if($loc_parts[1] == '=' || $loc_parts[1] == '~' || $loc_parts[1] == '~*' || $loc_parts[1] == '^~'){
+ $location = $loc_parts[1].' '.$loc_parts[2];
+ } else {
+ $location = $loc_parts[1];
+ }
+ unset($loc_parts);
+
+ if(!isset($locations[$location]['open_tag'])) $locations[$location]['open_tag'] = ' location '.$location.' {';
+ if(!isset($locations[$location]['location'])) $locations[$location]['location'] = '';
+ if(!isset($locations[$location]['end_tag'])) $locations[$location]['end_tag'] = ' }';
+ if(!isset($locations[$location]['start_line'])) $locations[$location]['start_line'] = $i;
+ unset($lines[$i]);
+
+ } else {
+
+ if($islocation){
+ if(strpos($l, '{') !== false){
+ $level += 1;
+ }
+ if(strpos($l, '}') !== false && $level > 0){
+ $level -= 1;
+ $locations[$location]['location'] .= $lines[$i]."\n";
+ } elseif(strpos($l, '}') !== false && $level == 0){
+ $islocation = false;
+ } else {
+ $locations[$location]['location'] .= $lines[$i]."\n";
+ }
+ unset($lines[$i]);
+ }
+
+ }
+ }
+
+ if(is_array($locations) && !empty($locations)){
+ foreach($locations as $key => $val){
+ $new_location = $val['open_tag']."\n".$val['location'].$val['end_tag'];
+ $lines[$val['start_line']] = $new_location;
+ }
+ }
+ ksort($lines);
+ $vhost_conf = implode("\n", $lines);
+ }
+
+ return $vhost_conf;
+ }
+
function client_delete($event_name,$data) {
global $app, $conf;
--
Gitblit v1.9.1