Skip to content

Commit 2655f93

Browse files
committed
- nginx: added function to merge duplicate locations in the nginx vhost configuration.
1 parent e2c00a1 commit 2655f93

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

server/conf/nginx_vhost.conf.master

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ server {
118118
</tmpl_loop>
119119

120120
<tmpl_loop name="basic_auth_locations">
121-
location ^~ <tmpl_var name='htpasswd_location'> {
121+
location <tmpl_var name='htpasswd_location'> {
122122
auth_basic "Members Only";
123123
auth_basic_user_file <tmpl_var name='htpasswd_path'>.htpasswd;
124124
}

server/plugins-available/nginx_plugin.inc.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ function update($event_name,$data) {
911911
if(file_exists($vhost_file)) copy($vhost_file,$vhost_file.'~');
912912

913913
//* Write vhost file
914-
file_put_contents($vhost_file,$tpl->grab());
914+
file_put_contents($vhost_file,$this->nginx_merge_locations($tpl->grab()));
915915
$app->log('Writing the vhost file: '.$vhost_file,LOGLEVEL_DEBUG);
916916
unset($tpl);
917917

@@ -1600,6 +1600,74 @@ private function php_fpm_pool_delete ($data,$web_config) {
16001600
}
16011601
}
16021602

1603+
private function nginx_merge_locations($vhost_conf){
1604+
1605+
$lines = explode("\n", $vhost_conf);
1606+
1607+
if(is_array($lines) && !empty($lines)){
1608+
1609+
$locations = array();
1610+
$islocation = false;
1611+
$linecount = sizeof($lines);
1612+
1613+
for($i=0;$i<$linecount;$i++){
1614+
$l = trim($lines[$i]);
1615+
if(substr($l, 0, 8) == 'location' && !$islocation){
1616+
1617+
$islocation = true;
1618+
$level = 0;
1619+
1620+
// Remove unnecessary whitespace
1621+
$l = preg_replace('/\s\s+/', ' ', $l);
1622+
1623+
$loc_parts = explode(' ', $l);
1624+
// see http://wiki.nginx.org/HttpCoreModule#location
1625+
if($loc_parts[1] == '=' || $loc_parts[1] == '~' || $loc_parts[1] == '~*' || $loc_parts[1] == '^~'){
1626+
$location = $loc_parts[1].' '.$loc_parts[2];
1627+
} else {
1628+
$location = $loc_parts[1];
1629+
}
1630+
unset($loc_parts);
1631+
1632+
if(!isset($locations[$location]['open_tag'])) $locations[$location]['open_tag'] = ' location '.$location.' {';
1633+
if(!isset($locations[$location]['location'])) $locations[$location]['location'] = '';
1634+
if(!isset($locations[$location]['end_tag'])) $locations[$location]['end_tag'] = ' }';
1635+
if(!isset($locations[$location]['start_line'])) $locations[$location]['start_line'] = $i;
1636+
unset($lines[$i]);
1637+
1638+
} else {
1639+
1640+
if($islocation){
1641+
if(strpos($l, '{') !== false){
1642+
$level += 1;
1643+
}
1644+
if(strpos($l, '}') !== false && $level > 0){
1645+
$level -= 1;
1646+
$locations[$location]['location'] .= $lines[$i]."\n";
1647+
} elseif(strpos($l, '}') !== false && $level == 0){
1648+
$islocation = false;
1649+
} else {
1650+
$locations[$location]['location'] .= $lines[$i]."\n";
1651+
}
1652+
unset($lines[$i]);
1653+
}
1654+
1655+
}
1656+
}
1657+
1658+
if(is_array($locations) && !empty($locations)){
1659+
foreach($locations as $key => $val){
1660+
$new_location = $val['open_tag']."\n".$val['location'].$val['end_tag'];
1661+
$lines[$val['start_line']] = $new_location;
1662+
}
1663+
}
1664+
ksort($lines);
1665+
$vhost_conf = implode("\n", $lines);
1666+
}
1667+
1668+
return $vhost_conf;
1669+
}
1670+
16031671
function client_delete($event_name,$data) {
16041672
global $app, $conf;
16051673

0 commit comments

Comments
 (0)