Fixed duplicates for nginx, fixes #930

Thanks to epek

Signed-off-by: Andreas Burchert (scarya) <scarya@froxlor.org>
This commit is contained in:
Andreas Burchert (scarya)
2011-10-18 13:27:42 +02:00
parent d342300c47
commit 57693f804f
2 changed files with 105 additions and 49 deletions

View File

@@ -28,6 +28,7 @@ class nginx
private $logger = false; private $logger = false;
private $debugHandler = false; private $debugHandler = false;
private $idnaConvert = false; private $idnaConvert = false;
private $nginx_server = array();
// protected // protected
@@ -40,7 +41,6 @@ class nginx
protected $mod_accesslog_loaded = "0"; protected $mod_accesslog_loaded = "0";
protected $vhost_root_autoindex = false; protected $vhost_root_autoindex = false;
protected $known_vhostfilenames = array(); protected $known_vhostfilenames = array();
/** /**
* indicator whether a customer is deactivated or not * indicator whether a customer is deactivated or not
* if yes, only the webroot will be generated * if yes, only the webroot will be generated
@@ -49,13 +49,14 @@ class nginx
*/ */
private $_deactivated = false; private $_deactivated = false;
public function __construct($db, $logger, $debugHandler, $idnaConvert, $settings) public function __construct($db, $logger, $debugHandler, $idnaConvert, $settings, $nginx_server=array())
{ {
$this->db = $db; $this->db = $db;
$this->logger = $logger; $this->logger = $logger;
$this->debugHandler = $debugHandler; $this->debugHandler = $debugHandler;
$this->idnaConvert = $idnaConvert; $this->idnaConvert = $idnaConvert;
$this->settings = $settings; $this->settings = $settings;
$this->nginx_server = $nginx_server;
} }
protected function getDB() protected function getDB()
@@ -87,13 +88,6 @@ class nginx
} }
} }
public function createVirtualHosts()
{
}
public function createFileDirOptions()
{
}
/** /**
* define a default ErrorDocument-statement, bug #unknown-yet * define a default ErrorDocument-statement, bug #unknown-yet
@@ -143,7 +137,10 @@ class nginx
} }
} }
public function createVirtualHosts(){
}
public function createFileDirOptions(){
}
public function createIpPort() public function createIpPort()
{ {
$query = "SELECT * FROM `" . TABLE_PANEL_IPSANDPORTS . "` ORDER BY `ip` ASC, `port` ASC"; $query = "SELECT * FROM `" . TABLE_PANEL_IPSANDPORTS . "` ORDER BY `ip` ASC, `port` ASC";
@@ -237,10 +234,10 @@ class nginx
$this->nginx_data[$vhost_filename].= "\t".'location ~ \.php$ {'."\n"; $this->nginx_data[$vhost_filename].= "\t".'location ~ \.php$ {'."\n";
$this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_index index.php;'."\n"; $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_index index.php;'."\n";
$this->nginx_data[$vhost_filename].= "\t\t".'include /etc/nginx/fastcgi_params;'."\n"; $this->nginx_data[$vhost_filename].= "\t\t".'include /etc/nginx/fastcgi_params;'."\n";
$this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n";
if ($row_ipsandports['ssl'] == '1') { if ($row_ipsandports['ssl'] == '1') {
$this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_param HTTPS on;'."\n"; $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_param HTTPS on;'."\n";
} }
$this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n";
if((int)$this->settings['phpfpm']['enabled'] == 1 if((int)$this->settings['phpfpm']['enabled'] == 1
&& (int)$this->settings['phpfpm']['enabled_ownvhost'] == 1 && (int)$this->settings['phpfpm']['enabled_ownvhost'] == 1
) { ) {
@@ -255,7 +252,7 @@ class nginx
'safemode' => '0', 'safemode' => '0',
'email' => $this->settings['panel']['adminmail'], 'email' => $this->settings['panel']['adminmail'],
'loginname' => 'froxlor.panel', 'loginname' => 'froxlor.panel',
'documentroot' => $mypath 'documentroot' => $mypath,
); );
$php = new phpinterface($this->getDB(), $this->settings, $domain); $php = new phpinterface($this->getDB(), $this->settings, $domain);
@@ -425,9 +422,19 @@ class nginx
if ($this->_deactivated == false) { if ($this->_deactivated == false) {
$vhost_content.= $this->create_pathOptions($domain); $vhost_content.= $this->create_pathOptions($domain);
// $vhost_content.= $this->create_htaccess($domain);
$vhost_content.= $this->composePhpOptions($domain, $ssl_vhost); $vhost_content.= $this->composePhpOptions($domain, $ssl_vhost);
$vhost_content.= $this->getStats($domain);
$l_regex1="/(location\ \/\ \{)(.*)(\})/smU";
$l_regex2="/(location\ \/\ \{.*\})/smU";
$replace_by="";
$replacements=preg_match_all($l_regex1,$vhost_content,$out);
if ($replacements>1){
foreach ($out[2] as $val) {
$replace_by.=$val."\n";
}
$vhost_content=preg_replace($l_regex2,"",$vhost_content,$replacements-1);
$vhost_content=preg_replace($l_regex2,"location / {"."\n\t\t". $replace_by ."\t}"."\n",$vhost_content);
}
if ($domain['specialsettings'] != "") { if ($domain['specialsettings'] != "") {
$vhost_content.= $domain['specialsettings'] . "\n"; $vhost_content.= $domain['specialsettings'] . "\n";
@@ -467,19 +474,22 @@ class nginx
$path_options.= "\t".'error_page 502 503 504 ' . $row['error500path'] . ';' . "\n"; $path_options.= "\t".'error_page 502 503 504 ' . $row['error500path'] . ';' . "\n";
} }
if($row['options_indexes'] != '0') // if($row['options_indexes'] != '0')
{ // {
$path = makeCorrectDir(substr($row['path'], strlen($domain['documentroot']) - 1)); $path = makeCorrectDir(substr($row['path'], strlen($domain['documentroot']) - 1));
mkDirWithCorrectOwnership($domain['documentroot'], $row['path'], $domain['guid'], $domain['guid']); mkDirWithCorrectOwnership($domain['documentroot'], $row['path'], $domain['guid'], $domain['guid']);
if (trim($path) == '/') { $path_options.= "\t".'# '.$path."\n";
if ($path == '/') {
$this->vhost_root_autoindex = true; $this->vhost_root_autoindex = true;
}
else
{
$path_options.= "\t".'location ' . $path . ' {' . "\n"; $path_options.= "\t".'location ' . $path . ' {' . "\n";
if($this->vhost_root_autoindex) {
$path_options.= "\t\t" . 'autoindex on;' . "\n"; $path_options.= "\t\t" . 'autoindex on;' . "\n";
$this->vhost_root_autoindex = false;
}
$path_options.= "\t\t" . 'index index.php index.html index.htm;'."\n";
// $path_options.= "\t\t" . 'try_files $uri $uri/ @rewrites;'."\n";
// check if we have a htpasswd for this path // check if we have a htpasswd for this path
// (damn nginx does not like more than one // (damn nginx does not like more than one
// 'location'-part with the same path) // 'location'-part with the same path)
@@ -487,8 +497,12 @@ class nginx
{ {
foreach($htpasswds as $idx => $single) foreach($htpasswds as $idx => $single)
{ {
if($path == $single['path']) switch($single['path']){
{ case '/awstats/':
case '/webalizer/':
break;
default:
if ($single['path']=='/'){
$path_options.= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; $path_options.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$path_options.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n"; $path_options.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
// remove already used entries so we do not have doubles // remove already used entries so we do not have doubles
@@ -496,14 +510,24 @@ class nginx
} }
} }
} }
}
$path_options.= "\t".'}' . "\n"; $path_options.= "\t".'}' . "\n";
$this->vhost_root_autoindex = false; $this->vhost_root_autoindex = false;
} }
else
{
$path_options.= "\t".'location ' . $path . ' {' . "\n";
if($this->vhost_root_autoindex) {
$path_options.= "\t\t" . 'autoindex on;' . "\n";
$this->vhost_root_autoindex = false;
} }
$path_options.= "\t\t" . 'index index.php index.html index.htm;'."\n";
}
// }
/** /**
* Perl suport * Perl support
* required the fastCGI wrapper to be running to receive the CGI requests. * required the fastCGI wrapper to be running to receive the CGI requests.
*/ */
if(customerHasPerlEnabled($domain['customerid']) if(customerHasPerlEnabled($domain['customerid'])
@@ -533,11 +557,24 @@ class nginx
{ {
foreach($htpasswds as $idx => $single) foreach($htpasswds as $idx => $single)
{ {
//if($single['path'] != "/")
//{
switch($single['path'])
{
case '/awstats/':
case '/webalizer/':
$path_options.= $this->getStats($domain,$single);
unset($htpasswds[$idx]);
break;
default:
$path_options.= "\t" . 'location ' . $single['path'] . ' {' . "\n"; $path_options.= "\t" . 'location ' . $single['path'] . ' {' . "\n";
$path_options.= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; $path_options.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$path_options.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n"; $path_options.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$path_options.= "\t".'}' . "\n"; $path_options.= "\t".'}' . "\n";
} }
//}
unset($htpasswds[$idx]);
}
} }
return $path_options; return $path_options;
@@ -545,7 +582,9 @@ class nginx
protected function getHtpasswds($domain) protected function getHtpasswds($domain)
{ {
$query = "SELECT * FROM " . TABLE_PANEL_HTPASSWDS . " WHERE `customerid`='" . $domain['customerid'] . "'"; //$query = "SELECT * FROM " . TABLE_PANEL_HTPASSWDS . " WHERE `customerid`='" . $domain['customerid'] . "'";
$query = "SELECT DISTINCT * FROM " . TABLE_PANEL_HTPASSWDS . " AS a JOIN " . TABLE_PANEL_DOMAINS . " AS b ON a.customerid=b.customerid WHERE b.customerid='" . $domain['customerid'] . "' AND a.path LIKE CONCAT(b.documentroot,'%') AND b.domain='" . $domain['domain'] . "'" ;
$result = $this->db->query($query); $result = $this->db->query($query);
$returnval = array(); $returnval = array();
@@ -581,12 +620,12 @@ class nginx
{ {
$phpopts = "\t".'location ~ \.php$ {'."\n"; $phpopts = "\t".'location ~ \.php$ {'."\n";
$phpopts.= "\t\t".'fastcgi_index index.php;'."\n"; $phpopts.= "\t\t".'fastcgi_index index.php;'."\n";
$phpopts.= "\t\t".'include /etc/nginx/fastcgi_params;'."\n"; //$phpopts.= "\t\t".'fastcgi_pass ' . $this->settings['system']['nginx_php_backend'] . ';' . "\n";
//$phpopts.= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n";
//$phpopts.= "\t\t".'include /etc/nginx/fastcgi_params;'."\n";
if ($domain['ssl'] == '1' && $ssl_vhost) { if ($domain['ssl'] == '1' && $ssl_vhost) {
$phpopts.= "\t\t".'fastcgi_param HTTPS on;'."\n"; $phpopts.= "\t\t".'fastcgi_param HTTPS on;'."\n";
} }
$phpopts.= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n";
$phpopts.= "\t\t".'fastcgi_pass ' . $this->settings['system']['nginx_php_backend'] . ';' . "\n";
$phpopts.= "\t".'}'."\n"; $phpopts.= "\t".'}'."\n";
} }
return $phpopts; return $phpopts;
@@ -611,18 +650,22 @@ class nginx
$webroot_text.= "\t".'location / {'."\n"; $webroot_text.= "\t".'location / {'."\n";
$webroot_text.= "\t\t".'index index.php index.html index.htm;'."\n"; $webroot_text.= "\t\t".'index index.php index.html index.htm;'."\n";
$webroot_text.= "\t\t" . 'try_files $uri $uri/ @rewrites;'."\n";
if($this->vhost_root_autoindex) { if($this->vhost_root_autoindex) {
$webroot_text.= "\t\t".'autoindex on;'."\n"; $webroot_text.= "\t\t".'autoindex on;'."\n";
$this->vhost_root_autoindex = false; $this->vhost_root_autoindex = false;
} }
$webroot_text.= "\t".'}'."\n"; $webroot_text.= "\t".'}'."\n\n";
$webroot_text.= "\tlocation @rewrites {\n";
$webroot_text.= "rewrite ^ /index.php last;\n";
$webroot_text.= "}\n\n";
return $webroot_text; return $webroot_text;
} }
protected function getStats($domain) protected function getStats($domain,$single=array())
{ {
$stats_text = ''; $stats_text = '';
@@ -635,17 +678,20 @@ class nginx
{ {
$stats_text.= "\t" . 'location /awstats {' . "\n"; $stats_text.= "\t" . 'location /awstats {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/awstats/' . $domain['domain']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/awstats/' . $domain['domain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
$stats_text.= "\t" . 'location /awstats-icon {' . "\n"; $stats_text.= "\t" . 'location /awstats-icon {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n";
$stats_text.= "\t\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
} }
else else
{ {
$stats_text.= "\t" . 'location /webalizer {' . "\n"; $stats_text.= "\t" . 'location /webalizer {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/webalizer/' . $domain['domain']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/webalizer/' . $domain['domain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
} }
} }
else else
@@ -654,15 +700,19 @@ class nginx
{ {
$stats_text.= "\t" . 'location /awstats {' . "\n"; $stats_text.= "\t" . 'location /awstats {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/awstats/' . $domain['parentdomain']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/awstats/' . $domain['parentdomain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
$stats_text.= "\t" . 'location /awstats-icon {' . "\n"; $stats_text.= "\t" . 'location /awstats-icon {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n";
$stats_text.= "\t\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
} }
else else
{ {
$stats_text.= "\t" . 'location /webalizer {' . "\n"; $stats_text.= "\t" . 'location /webalizer {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/webalizer/' . $domain['parentdomain']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/webalizer/' . $domain['parentdomain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
} }
@@ -676,6 +726,8 @@ class nginx
{ {
$stats_text.= "\t" . 'location /awstats {' . "\n"; $stats_text.= "\t" . 'location /awstats {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/awstats/' . $domain['domain']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['customerroot'] . '/awstats/' . $domain['domain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
$stats_text.= "\t" . 'location /awstats-icon {' . "\n"; $stats_text.= "\t" . 'location /awstats-icon {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n";
@@ -685,6 +737,8 @@ class nginx
{ {
$stats_text.= "\t" . 'location /webalizer {' . "\n"; $stats_text.= "\t" . 'location /webalizer {' . "\n";
$stats_text.= "\t\t" . 'root ' . makeCorrectFile($domain['customerroot'] . '/webalizer/' . $domain['domain']) . ';' . "\n"; $stats_text.= "\t\t" . 'root ' . makeCorrectFile($domain['customerroot'] . '/webalizer/' . $domain['domain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
} }
@@ -697,10 +751,12 @@ class nginx
{ {
$stats_text.= "\t" . 'location /awstats {' . "\n"; $stats_text.= "\t" . 'location /awstats {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['documentroot'] . '/awstats/' . $domain['domain']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectFile($domain['documentroot'] . '/awstats/' . $domain['domain']) . ';' . "\n";
$stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n";
$stats_text.= "\t\t" . 'auth_basic_user_file ' . $single['usrf'] . ';'."\n";
$stats_text.= "\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
$stats_text.= "\t" . 'location /awstats-icon {' . "\n"; $stats_text.= "\t" . 'location /awstats-icon {' . "\n";
$stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n"; $stats_text.= "\t\t" . 'alias ' . makeCorrectDir($this->settings['system']['awstats_icons']) . ';' . "\n";
$stats_text.= "\t\t" . '}' . "\n"; $stats_text.= "\t" . '}' . "\n";
} }
} }

View File

@@ -38,13 +38,13 @@ class nginx_phpfpm extends nginx
$phpconfig = $php->getPhpConfig((int)$domain['phpsettingid']); $phpconfig = $php->getPhpConfig((int)$domain['phpsettingid']);
$php_options_text = "\t".'location ~ \.php$ {'."\n"; $php_options_text = "\t".'location ~ \.php$ {'."\n";
// $php_options_text.= "\t\t".'fastcgi_index index.php;'."\n"; $php_options_text.= "\t\t".'fastcgi_pass unix:' . $php->getInterface()->getSocketFile() . ';' . "\n";
// $php_options_text.= "\t\t".'include /etc/nginx/fastcgi_params;'."\n"; $php_options_text.= "\t\t".'fastcgi_index index.php;'."\n";
$php_options_text.= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n";
$php_options_text.= "\t\t".'include /etc/nginx/fastcgi_params;'."\n";
if ($domain['ssl'] == '1' && $ssl_vhost) { if ($domain['ssl'] == '1' && $ssl_vhost) {
$php_options_text.= "\t\t".'fastcgi_param HTTPS on;'."\n"; $php_options_text.= "\t\t".'fastcgi_param HTTPS on;'."\n";
} }
$php_options_text.= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n";
$php_options_text.= "\t\t".'fastcgi_pass unix:' . $php->getInterface()->getSocketFile() . ';' . "\n";
$php_options_text.= "\t".'}'."\n"; $php_options_text.= "\t".'}'."\n";
// create starter-file | config-file // create starter-file | config-file