@@ -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