diff --git a/server/conf/nginx_vhost.conf.master b/server/conf/nginx_vhost.conf.master index d771eb1719ef7949c7eda5eb1649471596cbb2a6..c9589691a824227e766908f479cf85c8c2d2bf17 100644 --- a/server/conf/nginx_vhost.conf.master +++ b/server/conf/nginx_vhost.conf.master @@ -118,7 +118,7 @@ server { - location ^~ { + location { auth_basic "Members Only"; auth_basic_user_file .htpasswd; } diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php index 42d612045923e66132fe3407a2085a357ee51dd7..69cd032e1bcfcfcad1159201ec3b964d43de9a20 100644 --- a/server/plugins-available/nginx_plugin.inc.php +++ b/server/plugins-available/nginx_plugin.inc.php @@ -911,7 +911,7 @@ class nginx_plugin { 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 @@ class nginx_plugin { } } + 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;