Apacheは「NameVirtualHost」と「Listen」にポートを設定します。

Apacheは「NameVirtualHost」と「Listen」にポートを設定します。

ports.confApacheでは

NameVirtualHost *:80
Listen 80

なぜNameVirtualHost *:80別のListen 80

どういう意味ですか?彼らが違うとしたらどうでしょうか?

ベストアンサー1

~からApacheのウェブサイト:

Listen ディレクティブは仮想ホストを実装しません。プライマリサーバーにリッスンするアドレスとポートを通知するだけです。ディレクティブを使用しないと、サーバーは許可されたすべての要求を同じ方法で処理します。ただし、1つ以上のアドレスまたはポートに対して異なる動作を指定するために使用できます。 VirtualHostを実装する前に、使用するアドレスとポートをリッスンするようにサーバーに指示する必要があります。次に、指定されたアドレスとポートに対する対応する仮想ホストの動作を設定するためのセクションを作成する必要があります。サーバーが受信しないアドレスとポートを設定すると接続できませんのでご注意ください。

Apacheはとても柔軟です。最も基本的な使用方法は、仮想ホストを使用しないことです。仮想ホストを使用していない場合は、このListenディレクティブを使用して使用するネットワークインターフェイスとポートを指定できます。デフォルトでは、http.confファイルの仮想ホストで指定できるすべてのオプションを指定できます(最後に確認したときにApache.orgがパッケージ化した方法です)。

VirtualHostディレクティブはこのデフォルトの動作をオーバーライドします。これは、Apacheに特定のIPとポートの組み合わせを異なる方法で処理するように指示します。 Apacheに両方がない場合は、仮想ホストを使用する必要があります。これは、Webホスティングが最初に発明されたときに大きな問題でした。当時のブラウザはこれを処理する方法を知りませんでした。古くから、ほとんどのApacheディストリビューションは現在柔軟性が高く、IP、ポート、または名前によって異なる方法で応答できるため、デフォルトで仮想ホストを使用しています。

仮想ホストがある場合でも、ドメインの既定の構成を指定できることは便利です。誰かが間違ったサブドメイン名を入力した状況を考えてみてください。デフォルトのウェブサイトがあるので、誰かがそれにアクセスしようとしたときにそれを使用してカスタムページ\サイトを表示できます。

その逆も事実かもしれません。必要に応じsites-enabledて、共有フォルダを作成し、サーバーごとにポート構成ファイルの内容を調整することで、複数のサーバー間で負荷分散を行うことができます。

しかし、私はApacheが仮想ホストの設定に応じて自動的に設定する方法がないようだと思うのは奇妙だと思います。

楽しみにして、これを自動的に実行するスクリプトを作成しました。必要に応じて試してみることはできますが、ほとんど完全にテストされていないため、使用時に注意が必要であることを警告します。これは本番コードではないことを意味します。

<?php
$path=dirname(__FILE__); // The current file path. It is used so all other paths can reliably be set relatively.

$cfg=array(
   'output_file'=>"$path/var/ports.conf.out", // This is the file to be generated.
   'template_file'=>"$path/tpl_default.php",
   'glob_patterns'=>array( // This array contains a list of directories to search for virtual hosts in.
      "$path/test-enabled/*", // For now I'd just test some copies which you can play with. Once everything is
   ),   // good then you can change this to /etc/apache/sites-enabled

);

##############
define('IS_CLI', PHP_SAPI==='cli');

echo "Auto Vhost Script\n------------------\n\n";

//echo "Arguments: \n"; print_r(arguments($argv));

echo "Output File:\n\t{$cfg['output_file']}\n";
if(!isset($cfg['output_file'])||!is_writable(dirname($cfg['output_file'])))
   die("ERROR: Cannot write to output file.\n");

echo "Template File:\n\t{$cfg['template_file']}\n";
if(!isset($cfg['template_file'])||!is_readable(dirname($cfg['template_file'])))
   die("ERROR: Cannot read to template file.\n");

echo "Search Paths:\n";
foreach($cfg['glob_patterns'] as $pattern) {
   echo "\t$pattern\n";
}
echo "\nReading configuration files...\n";

$vhosts=array();
foreach($cfg['glob_patterns'] as $pattern) {
   echo ">> $pattern\n";
   $files=glob($pattern);
   foreach($files as $file) {
      echo "\t>> ". basename($file) ."\n";
      $handle=@fopen($file, "r");
      if($handle) {
         while(($buffer=fgets($handle, 4096))!==false) {
            $status=procLine($buffer);
            if(!$status) die("ERROR: Failed reading input line.\n");
            if($status===TRUE) continue;
            $vhosts[]=$status;
         }
         if(!feof($handle)) die("ERROR: Unexpected fgets() fail.\n");
         fclose($handle);
      }
   }
}
echo "\n\nGenerating template data...\n";
$nvhost=array();
$listen=array();
foreach($vhosts as $vhost) {
   // We're just going to assume that if you have multiple VirtualHost for the same port that means you want to use
   // *:port . You could improve this by actually checking to see if multiple hosts have been assigned to this port
   // but you'd need to rearrange the data a little.
   if($vhost['is_name']||isset($nvhost[$vhost['port']])) {
      $nvhost[$vhost['port']]='NameVirtualHost *:'.$vhost['port'];
      $listen[$vhost['port']]='Listen '.$vhost['port'];
   } else {
      $nvhost[$vhost['port']]='NameVirtualHost '.$vhost['host'].':'.$vhost['port'];
      if($vhost['host']=='*')
         $listen[$vhost['port']]='Listen '.$vhost['port'];
      else $listen[$vhost['port']]='Listen '.$vhost['host'].':'.$vhost['port'];
   }
}
echo "\n\nWriting output file...\n";

$tpl=file_get_contents($cfg['template_file']);
if($tpl) {
   $tpl=str_replace('{NAME_VHOST}', implode("\n", $nvhost), $tpl);
   $tpl=str_replace('{LISTEN}', implode("\n", $listen), $tpl);
   file_put_contents($cfg['output_file'], $tpl);
}

echo "\n\nDone.\n";

function procLine($line) {
   if(!preg_match("/^(\w)*<VirtualHost(.)*>(\w)*/", $line)) return true;
   $host=substr($line, strpos($line, 'VirtualHost')+12, strlen($line)-strrpos($line, '>')+2);
   $last_square=strrpos($host, ']'); // This is in case the host is specified in IPv6 format
   $cln=strrpos($host, ':');
   if($cln!==FALSE && $cln+1<strlen($host) && $cln>$last_square) {
      $port=substr($host, $cln+1);
      $host=substr($host, 0, $cln);
   } else $port='80';
   $is_range=strpos($host, '*')!==FALSE;
   $is_ip=$is_range||filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)!==FALSE;
   $is_name=!$is_ip;
   return array('host'=>$host, 'port'=>$port, 'is_name'=>$is_name, 'is_ip'=>$is_ip, 'is_range'=>$is_range);
}

設定する必要があるのは、$cfgスクリプトの上部にある変数とポートプロファイルテンプレートのみです。これを使用するには、PHP:で実行しますphp path/to/auto_vhost.php。一度機能すると、上部近くにPHP呼び出しを追加できます(または実際には少し良い呼び出しがありますが、/etc/init.d/apache2実際に必要なコマンドでは機能しないためです)。 )。service apache2 stopこれにより、Apacheが起動または再起動される前にスクリプトが実行され、スクリプトがロードされる可能性があります。

以下はサンプルテンプレートファイルです。

# This file was generated from a template by auto_vhost.php

{NAME_VHOST}

{LISTEN}

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

おすすめ記事