サイト <site>

  • 概要
  • 互換性
  • セットアップ
  • 方法
  • 構成
  • サンプル コード

※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。

概要

<site> 構成要素は、インターネット インフォメーション サービス (IIS) 7.0 サーバー上で実行される特定の Web サイトの構成を制御します。たとえば、Default Web Site とその設定は <site> 要素内で定義されています。この要素は <sites> 要素の開始タグと終了タグの間に配置されています。

<site> 要素は、IIS 7.0 で新しい Web サイトを作成するときに構成します。<site> 要素には、Web サイトに割り当てた名前を定義する name 属性と、Web サイトの数値識別子を定義する id 属性が含まれます。また、<site> 要素には、サイト上で実行されるアプリケーションや仮想ディレクトリを定義する子要素、および Web サイトのポート、プロトコル、ログ、追跡に関する構成設定を定義する子要素が含まれます。

互換性

  IIS 7.0 IIS 6.0
説明 <sites> コレクションの <site> は IIS 7.0 で新たに導入された要素です。

<sites> コレクションは、IIS 6.0 の IIsWebServer メタベース オブジェクトに代わるものです。

セットアップ

<site> 要素は、IIS 7.0 の既定のインストールに含まれています。

方法

IIS 7.0 で新しい Web サイトを構成する場合、Web サイトにサイト名および物理パスを割り当てる必要があります。また、さまざまなオプションの構成設定を指定することもできます。IIS 7.0 サーバーで既定の Web サイトをそのまま使用する予定であれば、新しいサイトのバインド情報を変更する必要があります。バインド情報を変更するには、ポートを変更するか、新しい Web サイトのホスト名を入力します。

新しい Web サイトを作成する方法

  1. タスク バーの [スタート] ボタンをクリックして、[管理ツール] をポイントし、[インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。

  2. [接続] ウィンドウで、対象のサーバー名を展開し、[サイト] をクリックします。

  3. [操作] ウィンドウで [Web サイトの追加] をクリックします。

  4. [Web サイトの追加] ダイアログ ボックスで、少なくとも [サイト名] ボックスおよび [物理パス] ボックスに情報を入力します。また、[ホスト名] ボックスに情報を入力したり、[ポート] ボックスの数値を変更したりすることもできます。

    拡大

  5. IIS マネージャーで更新ボタンをクリックして、サイトが開始されたことを確認します。

構成

サーバー レベルの <site> 要素は、ApplicationHost.config ファイルで構成できます。

属性

属性 説明
id オプションの uint 属性。

サイトの作成時に IIS によって割り当てられるランダムな数値識別子を指定します。

既定の Web サイトの数値は 1 です。その他の Web サイトは複数の数字で構成された乱数が割り当てられます。
name オプションの string 属性。

一意に Web サイトを識別するフレンドリ名を指定します (例 : "Contoso HR Forms")。
serverAutoStart オプションの Boolean 属性。

管理サービスの開始時に自動的にサイトを開始するかどうかを指定します。

既定値は true です。

子要素

要素 説明
application オプションの要素。

サイト内のアプリケーションの構成設定のコレクションを指定します。
applicationDefaults オプションの要素。

親サイト内のすべてのアプリケーションの既定の設定を指定します。
bindings オプションの要素。

サイトにアクセスするためのバインドを指定します。
limits オプションの要素。

帯域幅、接続数、またはサイトへの接続時間を制限する設定を構成します。
logFile オプションの要素。

サイトのログ ファイルの処理および保存に関する設定を構成します。
traceFailedRequestsLogging オプションの要素。

サイトの失敗した要求トレースのログ記録に関する設定を指定します。
virtualDirectoryDefaults オプションの要素。

サイト内のすべての仮想ディレクトリの既定の設定を指定します。

構成サンプル

次の構成例は、ApplicationHost.config ファイル内の <site> 要素を示しています。name 属性はサイト名を Contoso として定義しており、id 属性は IIS 固有の識別子を 2 として定義しています。<virtualDirectory> 要素内の physicalPath 属性は、新しいサイトのコンテンツの物理的な場所を定義しています。<binding> 要素の protocol 属性はサイトが使用するプロトコルを定義し、bindingInformation 属性は www.contoso.com を新しい Web サイトが使用するホスト ヘッダーとして定義しています。

<site name="Contoso" id="2">
   <application path="/">
      <virtualDirectory path="/" physicalPath="C:\inetpub\Contoso" />
   </application>
   <bindings>
      <binding protocol="http" bindingInformation="*:80:www.contoso.com" />

   </bindings> 
</site>

サンプル コード

次の例では、ID が 2 の Contoso という名前の新しい Web サイトを作成し、ホスト ヘッダー "www.contoso.com" を使用してポート 80 上の HTTP プロトコルにバインドを設定します。新しい Web サイトの物理パスは、C:\Inetpub\www.contoso.com\wwwroot です。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2',serverAutoStart='True']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2'].bindings.[protocol='http',bindingInformation='*:80:www.contoso.com']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2'].[path='/']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2'].[path='/'].[path='/',physicalPath='C:\Inetpub\www.contoso.com\wwwroot']" /commit:apphost

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

         ConfigurationElement siteElement = sitesCollection.CreateElement("site");
         siteElement["name"] = @"Contoso";
         siteElement["id"] = 2;
         siteElement["serverAutoStart"] = true;

         ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings");
         ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
         bindingElement["protocol"] = @"http";
         bindingElement["bindingInformation"] = @"*:80:www.contoso.com";
         bindingsCollection.Add(bindingElement);

         ConfigurationElementCollection siteCollection = siteElement.GetCollection();
         ConfigurationElement applicationElement = siteCollection.CreateElement("application");
         applicationElement["path"] = @"/";
         ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
         ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
         virtualDirectoryElement["path"] = @"/";
         virtualDirectoryElement["physicalPath"] = @"C:\Inetpub\www.contoso.com\wwwroot";
         applicationCollection.Add(virtualDirectoryElement);
         siteCollection.Add(applicationElement);
         sitesCollection.Add(siteElement);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection

      Dim siteElement As ConfigurationElement = sitesCollection.CreateElement("site")
      siteElement("name") = "Contoso"
      siteElement("id") = 2
      siteElement("serverAutoStart") = True

      Dim bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")
      Dim bindingElement As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement("protocol") = "http"
      bindingElement("bindingInformation") = "*:80:www.contoso.com"
      bindingsCollection.Add(bindingElement)

      Dim siteCollection As ConfigurationElementCollection = siteElement.GetCollection
      Dim applicationElement As ConfigurationElement = siteCollection.CreateElement("application")
      applicationElement("path") = "/"
      Dim applicationCollection As ConfigurationElementCollection = applicationElement.GetCollection
      Dim virtualDirectoryElement As ConfigurationElement = applicationCollection.CreateElement("virtualDirectory")
      virtualDirectoryElement("path") = "/"
      virtualDirectoryElement("physicalPath") = "C:\Inetpub\www.contoso.com\wwwroot"
      applicationCollection.Add(virtualDirectoryElement)
      siteCollection.Add(applicationElement)
      sitesCollection.Add(siteElement)

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;

var siteElement = sitesCollection.CreateNewElement("site");
siteElement.Properties.Item("name").Value = "Contoso";
siteElement.Properties.Item("id").Value = 2;
siteElement.Properties.Item("serverAutoStart").Value = true;

var bindingsCollection = siteElement.ChildElements.Item("bindings").Collection;
var bindingElement = bindingsCollection.CreateNewElement("binding");
bindingElement.Properties.Item("protocol").Value = "http";
bindingElement.Properties.Item("bindingInformation").Value = "*:80:www.contoso.com";
bindingsCollection.AddElement(bindingElement);

var siteCollection = siteElement.Collection;
var applicationElement = siteCollection.CreateNewElement("application");
applicationElement.Properties.Item("path").Value = "/";
var applicationCollection = applicationElement.Collection;
var virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory");
virtualDirectoryElement.Properties.Item("path").Value = "/";
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\\Inetpub\\www.contoso.com\\wwwroot";
applicationCollection.AddElement(virtualDirectoryElement);
siteCollection.AddElement(applicationElement);
sitesCollection.AddElement(siteElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection

Set siteElement = sitesCollection.CreateNewElement("site")
siteElement.Properties.Item("name").Value = "Contoso"
siteElement.Properties.Item("id").Value = 2
siteElement.Properties.Item("serverAutoStart").Value = True

Set bindingsCollection = siteElement.ChildElements.Item("bindings").Collection
Set bindingElement = bindingsCollection.CreateNewElement("binding")
bindingElement.Properties.Item("protocol").Value = "http"
bindingElement.Properties.Item("bindingInformation").Value = "*:80:www.contoso.com"
bindingsCollection.AddElement bindingElement

Set siteCollection = siteElement.Collection
Set applicationElement = siteCollection.CreateNewElement("application")
applicationElement.Properties.Item("path").Value = "/"
Set applicationCollection = applicationElement.Collection
Set virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory")
virtualDirectoryElement.Properties.Item("path").Value = "/"
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\Inetpub\www.contoso.com\wwwroot"
applicationCollection.AddElement(virtualDirectoryElement)
siteCollection.AddElement applicationElement
sitesCollection.AddElement siteElement

adminManager.CommitChanges()