[Yandex Cloud documentation](../../../index.md) > [Yandex Object Storage](../../index.md) > [Step-by-step guides](../index.md) > Hosting static websites > Setting up hosting

# Setting up hosting


You can host your static website in Object Storage. A static website is based on such client-side technologies as HTML, CSS, or JavaScript. It may not contain any scripts that run on the web server side. 

In a static website, pages are [objects](../../concepts/object.md) in a [bucket](../../concepts/bucket.md), where object [keys](../../concepts/object.md#key) match the website page paths, and the full URL of a page has `http(s)://website.yandexcloud.net/<bucket_name>/<object_key>` format. {#static-site-information}

Object Storage buckets support:

* [Static website hosting](#hosting).
* [Redirects for all requests](#redirects).
* [Conditional request redirects](#redirects-on-conditions).

{% note warning %}

Static website hosting is not supported in buckets with the following parameters:

* Bucket [encryption](../../concepts/encryption.md) is enabled. [Disable](../buckets/encrypt.md#del) encryption, [delete](../objects/delete-all.md) the objects from the bucket, and [reupload](../objects/upload.md) them.
* [Public access](../../security/overview.md#anonymous) to the objects and their list is disabled. [Enable](../buckets/bucket-availability.md#open-public-access) public access.
* [Access policies](../../concepts/policy.md) are in place which restrict access to the objects and their list for anonymous users. [Update](../buckets/policy.md#apply-policy) the access policy.
* Access is allowed only from [VPC service connections](../../security/overview.md#pe-vpc). [Restore](../buckets/access-via-vpc.md#disable) access to the bucket from the public network.

For more information, see [Access management methods in Object Storage: Overview](../../security/overview.md).

{% endnote %}

## Static website hosting {#hosting}

{% note info %}

The options of static website hosting and redirection of all requests are mutually exclusive.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select a folder.
  1. Navigate to **Object Storage**.
  1. Select the bucket you want to configure hosting for.
  1. In the left-hand panel, select ![image](../../../_assets/console-icons/wrench.svg) **Settings**.
  1. Navigate to the **General** tab.
  1. In the **Read objects** and **Read object list** fields, select `For all`.
  1. Click **Save**.
  1. Select the **Website** tab.
  1. Under **Hosting**:
      * In the **Home page** field, specify the absolute path to the file in the bucket for the website home page, e.g., `index.html`.
  
        {% note warning %}
        
        The [key of the object](../../concepts/object.md#key) containing your website homepage must not include the `/` character.
        
        {% endnote %}
  
      * Optionally, in the **Error page** field, specify the absolute path to the file in the bucket to show for 4xx errors, e.g., `pages/error404.html`. By default, Object Storage returns its own page.
  
        {% note tip %}
        
        If a bucket contains a [single-page application](https://en.wikipedia.org/wiki/Single-page_application), specify the home page, e.g., `index.html`, as the error page.
        
        {% endnote %}
  
  1. Click **Save**.
  
  Use the link in **Link** to check the hosting.

- Yandex Cloud CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  1. See the description of the CLI command for setting up static website hosting in a bucket:
  
      ```bash
      yc storage bucket update --help
      ```
      
  1. Create a hosting configuration file in JSON format. Here is an example:
      
      ```json
      {
        "index": "index.html",
        "error": "error404.html"
      }
      ```
  
      Where:
  
      * `index`: Absolute path to the website home page file.
  
        {% note warning %}
        
        The [key of the object](../../concepts/object.md#key) containing your website homepage must not include the `/` character.
        
        {% endnote %}
  
      * `error`: Absolute path to the file the user will see in case of `4xx` errors.
  
        {% note tip %}
        
        If a bucket contains a [single-page application](https://en.wikipedia.org/wiki/Single-page_application), specify the home page, e.g., `index.html`, as the error page.
        
        {% endnote %}
  
  1. Run this command:
  
      ```bash
      yc storage bucket update --name <bucket_name> \
        --website-settings-from-file <path_to_file>
      ```
      
      Where:
      * `--name`: Bucket name.
      * `--website-settings-from-file`: Path to the hosting configuration file.
  
      Result:
  
      ```text
      name: my-bucket
      folder_id: b1gjs8dck8bv********
      default_storage_class: STANDARD
      versioning: VERSIONING_SUSPENDED
      max_size: "10737418240"
      acl: {}
      created_at: "2022-12-14T08:42:16.273717Z"
      ```
  
  To make sure the bucket description now contains the hosting settings, run this command:
  
  ```bash
  yc storage --name <bucket_name> bucket get --full
  ```
  
  Result:
  
  ```text
  website_settings:
    index: index.html
    error: error404.html
    redirect_all_requests: {}
  ```

- Terraform {#tf}

  {% note info %}
  
  If you access Object Storage via Terraform under a [service account](../../../iam/concepts/users/service-accounts.md), [assign](../../../iam/operations/sa/assign-role-for-sa.md) to the service account the relevant [role](../../security/index.md#roles-list), e.g., `storage.admin`, for the folder you are going to create the resources in.
  
  {% endnote %}

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.

  Before you start, retrieve the [static access keys](../../../iam/operations/authentication/manage-access-keys.md#create-access-key): a secret key and key ID used for Object Storage authentication.

  {% note info %}
  
  In addition to static access keys, you can use an IAM token for authentication in Object Storage. For more details, see [Creating a bucket](../buckets/create.md) and the [relevant provider documentation](../../../terraform/resources/storage_object.md). 
  
  {% endnote %}


  1. In the configuration file, specify the properties of the resources you want to create:

     ```hcl
     provider "yandex" {
       cloud_id  = "<cloud_ID>"
       folder_id = "<folder_ID>"
       zone      = "ru-central1-a"
     }

     resource "yandex_iam_service_account" "sa" {
       name = "<service_account_name>"
     }

     // Assigning a role to a service account
     resource "yandex_resourcemanager_folder_iam_member" "sa-admin" {
       folder_id = "<folder_ID>"
       role      = "storage.admin"
       member    = "serviceAccount:${yandex_iam_service_account.sa.id}"
     }

     // Creating a static access key
     resource "yandex_iam_service_account_static_access_key" "sa-static-key" {
       service_account_id = yandex_iam_service_account.sa.id
       description        = "static access key for object storage"
     }

     resource "yandex_storage_bucket" "test" {
       access_key = yandex_iam_service_account_static_access_key.sa-static-key.access_key
       secret_key = yandex_iam_service_account_static_access_key.sa-static-key.secret_key
       bucket     = "<bucket_name>"
       acl        = "public-read"
     
       website {
         index_document = "index.html"
         error_document = "error.html"
       }
     
     }
     ```

     Where:

     * `access_key`: Static access key ID.
     * `secret_key`: Secret access key value.
     * `bucket`: Bucket name.
     * `acl`: [ACL](../../concepts/acl.md#predefined-acls) access management parameters.
     * `website`: Website parameters:
       * `index_document`: Absolute path to the website home page file. This is a required setting.

         {% note warning %}
         
         The [key of the object](../../concepts/object.md#key) containing your website homepage must not include the `/` character.
         
         {% endnote %}

       * `error_document`: Absolute path to the file the user will see in case of 4xx errors. This is an optional setting.

        {% note tip %}
        
        If a bucket contains a [single-page application](https://en.wikipedia.org/wiki/Single-page_application), specify the home page, e.g., `index.html`, as the error page.
        
        {% endnote %}

  1. Create the resources:

      1. In the terminal, navigate to the configuration file directory.
      1. Make sure the configuration is correct using this command:
      
         ```bash
         terraform validate
         ```
      
         If the configuration is valid, you will get this message:
      
         ```bash
         Success! The configuration is valid.
         ```
      
      1. Run this command:
      
         ```bash
         terraform plan
         ```
      
         You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
      1. Apply the configuration changes:
      
         ```bash
         terraform apply
         ```
      
      1. Type `yes` and press **Enter** to confirm the changes.

  Terraform will create all the required resources. You can check the new resources and their settings using the [management console](https://console.yandex.cloud).

- API {#api}

  To set up hosting for a static website, use the [update](../../api-ref/Bucket/update.md) REST API method for the [Bucket](../../api-ref/Bucket/index.md) resource, the [BucketService/Update](../../api-ref/grpc/Bucket/update.md) gRPC API call, or the [upload](../../s3/api-ref/hosting/upload.md) S3 API method.

{% endlist %}

## Redirects for all requests {#redirects}

Use redirects of all requests when you need to automatically forward all bucket requests to another URL or bucket, e.g., when changing your domain. This keeps data available without the need to modify client requests.

{% note info %}

The options of static website hosting and redirection of all requests are mutually exclusive.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select a folder.
  1. Navigate to **Object Storage**.
  1. Select the bucket you want to redirect all requests for.
  1. In the left-hand panel, select ![image](../../../_assets/console-icons/wrench.svg) **Settings**.
  1. Select the **Website** tab.
  1. Under **Redirect**, specify:
      * **Domain name** of the host to act as the redirect target for all requests to the bucket.
      * Optionally, **Protocol** if the specified host accepts requests only over a specific protocol.
  1. Click **Save**.

- Yandex Cloud CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  1. See the description of the CLI command for setting up redirects for all requests:

     ```bash
     yc storage bucket update --help
     ```
     
  1. Create a redirect configuration file in JSON format. For example:
     
     ```json
     {
       "redirectAllRequests": {
         "protocol": "PROTOCOL_HTTP",
         "hostname": "example.com"
       }
     }
     ```

     Where:
     * `protocol`: Data transfer protocol, `PROTOCOL_HTTP` or `PROTOCOL_HTTPS`. By default, the original request protocol is used.
     * `hostname`: Domain name of the host to act as the redirect target for all requests to the current bucket.
  
  1. Run this command:

     ```bash
     yc storage bucket update --name <bucket_name> \
       --website-settings-from-file <path_to_file>
     ```

     Where:
     * `--name`: Bucket name.
     * `--website-settings-from-file`: Path to the redirect configuration file.

     Result:

     ```text
     name: my-bucket
     folder_id: b1gjs8dck8bv********
     default_storage_class: STANDARD
     versioning: VERSIONING_SUSPENDED
     max_size: "10737418240"
     acl: {}
     created_at: "2022-12-14T08:42:16.273717Z"
     ```

- Terraform {#tf}
 
  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

  
  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.



  To set up a redirect for all requests:

  1. Open the Terraform configuration file and add the `redirect_all_requests_to` property to the `yandex_storage_bucket` description:

     ```hcl
     ...
     resource "yandex_storage_bucket" "test" {
       access_key = "<static_key_ID>"
       secret_key = "<secret_key>"
       bucket     = "<bucket_name>"
       acl        = "public-read"
     
       website {
         index_document = "<absolute_path_to_website_home_page_file>"
         error_document = "<absolute_path_to_error_file>"
		 redirect_all_requests_to = "<host_name>"
       }
     }
     ...
     ```

     Where:
     * `access_key`: Static access key ID.

        {% note info %}
        
        In addition to static access keys, you can use an IAM token for authentication in Object Storage. For more details, see [Creating a bucket](../buckets/create.md) and the [relevant provider documentation](../../../terraform/resources/storage_object.md). 
        
        {% endnote %}

     * `secret_key`: Secret access key value.
     * `bucket`: Bucket name.
     * `acl`: [ACL](../../concepts/acl.md#predefined-acls) access management parameters.
     * `website`: Website parameters:
       * `index_document`: Absolute path to the website home page file. This is a required setting.
       * `error_document`: Absolute path to the file the user will see in case of 4xx errors. This is an optional setting.
       * `redirect_all_requests_to`: Domain name of the host to act as the redirect target for all requests to the current bucket. You can specify a protocol prefix (`http://` or `https://`). By default, the original request protocol is used.

     For more information about `yandex_storage_bucket` properties in Terraform, see [this provider guide](../../../terraform/resources/storage_bucket.md#static-website-hosting).

  1. Create the resources:

      1. In the terminal, navigate to the configuration file directory.
      1. Make sure the configuration is correct using this command:
      
         ```bash
         terraform validate
         ```
      
         If the configuration is valid, you will get this message:
      
         ```bash
         Success! The configuration is valid.
         ```
      
      1. Run this command:
      
         ```bash
         terraform plan
         ```
      
         You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
      1. Apply the configuration changes:
      
         ```bash
         terraform apply
         ```
      
      1. Type `yes` and press **Enter** to confirm the changes.

     You can use the [management console](https://console.yandex.cloud) to check the request redirect settings.

- API {#api}

  To set up redirects for all requests to a bucket, use the [update](../../api-ref/Bucket/update.md) REST API method for the [Bucket](../../api-ref/Bucket/index.md) resource, the [BucketService/Update](../../api-ref/grpc/Bucket/update.md) gRPC API call, or the [upload](../../s3/api-ref/hosting/upload.md) S3 API method.

{% endlist %}

Request redirects from HTTP to HTTPS are enabled automatically once you set up HTTPS access to a bucket. No other settings are required.

## Conditional request redirects {#redirects-on-conditions}

  With redirect rules, you can redirect requests to remote objects or requests that return an error to another web page. The redirection takes place according to a condition consisting of the key prefix of the object and the HTTP code of the response. Yandex Object Storage has [limits](../../concepts/limits.md#storage-limits) on the maximum number of rules for conditional request redirects.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select a folder.
  1. Navigate to **Object Storage**.
  1. Select the bucket you want to configure conditional request redirects for.
  1. In the left-hand panel, select ![image](../../../_assets/console-icons/wrench.svg) **Settings**.
  1. Select the **Website** tab.
  1. In **Hosting**, under **Redirect**, click **Add forwarding rule**.
  1. Under **Condition**, specify at least one condition for redirects:
      * **Response code**: HTTP code that Object Storage would have returned for the request without a redirect.
      * **Key prefix**: Object key prefix in the request. You can learn more about keys and how static websites work [here](#static-site-information).
    
     If a request matches the conditions of more than one redirect rule, the last matching one will apply regardless of how closely the object key matches the condition prefix.
    
  1. Under **Redirect**, set the following redirect parameters:
      * **Protocol** to use for sending redirected requests.
      * **Domain name** of the host to which all requests meeting the specified condition will be redirected.
      * **Response code** to determine the redirect type.
      * **Replace key**: **None**, **Whole key**, or **Prefix only**, specified in the condition.
  1. Click **Save**.
  
- Yandex Cloud CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  1. See the description of the CLI command for setting up conditional request redirects:

     ```bash
     yc storage bucket update --help
     ```
     
  1. Create a conditional redirect configuration file in JSON format. For example:
     
     ```json
     {
       "index": "index.html",
       "error": "error404.html",
       "routingRules": [
         {
           "condition": {
             "httpErrorCodeReturnedEquals": "404",
             "keyPrefixEquals": "key"
           },
           "redirect": {
             "hostname": "example.com",
             "httpRedirectCode": "301",
             "protocol": "PROTOCOL_HTTP",
             "replaceKeyPrefixWith": "prefix",
             "replaceKeyWith": "key"
           } 
         }
       ]
     }
     ```

     Where:
     * `index`: Absolute path to the website home page file. This is a required setting.

         {% note info %}

         You need to provide the absolute path to the homepage file with conditional redirection settings, even if this parameter is already set for the bucket.

         {% endnote %}

     * `error`: Absolute path to the file the user will see in case of 4xx errors. This is an optional setting.
     * `condition`: Condition to trigger a redirect:
     
       * `httpErrorCodeReturnedEquals`: HTTP response code.
       * `keyPrefixEquals`: Object key prefix.

       If a request matches the conditions of more than one redirect rule, the last matching one will apply regardless of how closely the object key matches the condition prefix.
       
     * `redirect`: Redirect settings:
     
       * `hostname`: Domain name of the host to act as the redirect target for all requests to the current bucket.
       * `httpRedirectCode`: New HTTP response code.
       * `protocol`: New data transfer protocol, `PROTOCOL_HTTP` or `PROTOCOL_HTTPS`. By default, the original request protocol is used. 
       * `replaceKeyPrefixWith`: New object key prefix.
       * `replaceKeyWith`: New object key.
  
  1. Run this command:

     ```bash
     yc storage bucket update --name <bucket_name> \
       --website-settings-from-file <path_to_file>
     ```

     Where:
     * `--name`: Bucket name.
     * `--website-settings-from-file`: Path to the conditional redirect configuration file.

     Result:

     ```text
     name: my-bucket
     folder_id: b1gjs8dck8bv********
     default_storage_class: STANDARD
     versioning: VERSIONING_SUSPENDED
     max_size: "10737418240"
     acl: {}
     created_at: "2022-12-14T08:42:16.273717Z"
     ```

- Terraform {#tf}
 
  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

    
  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.



  To set up conditional request redirects:

  1. Open the Terraform configuration file and add the `routing_rules` parameter to the bucket description:

     ```hcl
     ...
     resource "yandex_storage_bucket" "test" {
       access_key = "<static_key_ID>"
       secret_key = "<secret_key>"
       bucket     = "<bucket_name>"
       acl        = "public-read"
     
       website {
         index_document = "<absolute_path_to_website_home_page_file>"
         error_document = "<absolute_path_to_error_file>"
         routing_rules  = <<EOF
         [
	       {
             "Condition": {
               "KeyPrefixEquals": "<object_key_prefix>",
               "HttpErrorCodeReturnedEquals": "<HTTP_response_code>"
             },
             "Redirect": {
               "Protocol": "<new_schema>",
               "HostName": "<new_domain_name>",
               "ReplaceKeyPrefixWith": "<new_object_key_prefix>",
               "ReplaceKeyWith": "<new_object_key>",
               "HttpRedirectCode": "<new_HTTP_response_code>"
             }
           },
         ...
         ]
         EOF
       }
     }
     ...
     ```

     Where:
     * `access_key`: Static access key ID.

        {% note info %}
        
        In addition to static access keys, you can use an IAM token for authentication in Object Storage. For more details, see [Creating a bucket](../buckets/create.md) and the [relevant provider documentation](../../../terraform/resources/storage_object.md). 
        
        {% endnote %}

     * `secret_key`: Secret access key value.
     * `bucket`: Bucket name.
     * `acl`: [ACL](../../concepts/acl.md#predefined-acls) access management parameters.
     * `website`: Website parameters:
       * `index_document`: Absolute path to the website home page file. This is a required setting.
       * `error_document`: Absolute path to the file the user will see in case of 4xx errors. This is an optional setting.
       * `routing_rules`: Rules for redirecting requests in JSON format. Each rule's `Condition` and `Redirect` fields must contain at least one <q>key-value</q> pair. For more information about the supported fields, see the [data schema](../../s3/api-ref/hosting/upload.md#request-scheme) of the relevant API method (the **For conditionally redirecting requests** tab).

     For more information about `yandex_storage_bucket` properties in Terraform, see [this provider guide](../../../terraform/resources/storage_bucket.md#static-website-hosting).
  1. Create the resources:

      1. In the terminal, navigate to the configuration file directory.
      1. Make sure the configuration is correct using this command:
      
         ```bash
         terraform validate
         ```
      
         If the configuration is valid, you will get this message:
      
         ```bash
         Success! The configuration is valid.
         ```
      
      1. Run this command:
      
         ```bash
         terraform plan
         ```
      
         You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
      1. Apply the configuration changes:
      
         ```bash
         terraform apply
         ```
      
      1. Type `yes` and press **Enter** to confirm the changes.

     You can use the [management console](https://console.yandex.cloud) to check the settings for conditionally redirecting requests.

- API {#api}

  To set up conditional redirects for bucket requests, use the [update](../../api-ref/Bucket/update.md) REST API method for the [Bucket](../../api-ref/Bucket/index.md) resource, the [BucketService/Update](../../api-ref/grpc/Bucket/update.md) gRPC API call, or the [upload](../../s3/api-ref/hosting/upload.md) S3 API method.

{% endlist %}

Request redirects from HTTP to HTTPS are enabled automatically once you set up HTTPS access to a bucket. No other settings are required.

### See also {#see-also}

* [How do I fix an incorrect MIME type of objects when uploading them to a bucket?](../../qa.md#qa-mime-type)
* [Custom domain](own-domain.md)
* [Support for multiple domain names](multiple-domains/index.md)
* [Configuring HTTPS](certificate.md)