The static website hosting feature of Object Storage Service (OSS) lets you publish static files, such as HTML, CSS, and JavaScript, from a bucket as a publicly accessible website. This eliminates the need for traditional servers, allowing you to deploy a highly available website while significantly reducing operational costs and technical complexity.
Standard static website
This guide shows you how to deploy a traditional multi-page static website, such as a corporate homepage or product showcase, and ensure reliable online access.
For security reasons, when you access an HTML file by using a bucket domain name, browsers force a download instead of displaying the file online. To allow users to view the website, which is the primary purpose of static website hosting, you must bind a custom domain name. For more information, see Access OSS by using a custom domain name. If your bucket is in the Chinese mainland, an ICP filing is required for the custom domain name.
Step 1: Configure static website hosting
Configure a default homepage and an error page to set up basic access rules. This ensures users can browse your site and receive a helpful error message when an error occurs.
-
Go to the Buckets page, click the name of the target bucket, and then in the left-side navigation pane, click .
-
Click Settings and configure the following parameters:
-
Default Homepage: Set to
index.html. -
Subfolder Homepage: Choose whether to enable a separate homepage for subfolders based on your website's structure.
-
Disabled (Default): Suitable for simple websites that do not require separate homepages for subfolders. With this option, any URL path that ends with a forward slash (
/) returns the default homepage from the root directory. -
Enabled: Suitable for complex websites with multiple independent content sections, such as
/blog/or/docs/, where each section has its own homepage. When enabled, you must also configure the Subfolder 404 Rule to define how the system responds when a user requests a file that does not exist:-
Redirect (Default): Redirects to the directory URL. If a requested file does not exist, the system checks for a homepage in the corresponding directory. If one exists, the system returns a
302redirect response, and the browser's address bar updates to the directory path. For example,.../subdirchanges to.../subdir/. -
Index: Returns homepage content directly. Similar to Redirect, but if a homepage is found, the system returns its content with a
200status code. The browser's address bar remains unchanged. -
NoSuchKey: Returns a 404 error directly. This is the strictest rule. If a requested file does not exist, the system returns a
404error, regardless of whether a homepage exists in the corresponding directory.
-
-
-
Default 404 Page: Set to
error.html. -
Error Page Status Code: Select 404.
-
-
Click Save.
Step 2: Upload website files
Upload your prepared HTML files to the bucket to provide content for your website.
-
Download and extract the sample file html.zip, or use your own project files.
-
In the left-side navigation pane, click .
-
Drag the sample files or your existing project files to the upload window. After the files are added to the list, click Upload Object.
Step 3: Set bucket permissions
Configure public read permissions to make your website content accessible to internet users.
Public read permission makes all objects in the bucket publicly accessible. Anyone who knows an object's URL can download it. Therefore, use this bucket for public resources like HTML, CSS, and JavaScript only. Store sensitive data in a separate bucket with stricter access controls.
-
The Block Public Access feature is enabled by default when you create an OSS bucket. This feature prevents you from setting the bucket's access control list (ACL) to public-read or public-read-write. You must disable this feature first.
-
In the left-side navigation pane, click .
-
Click the Block Public Access toggle. In the dialog box that appears, enter I confirm that I want to disable Block Public Access, and then click OK.
-
-
Set the bucket's access permissions to public-read.
-
Go to the Access Control List tab and click Set.
-
Set Bucket ACL to Public Read. In the dialog box that appears, click Continue.
-
Click Save.
-
Step 4: Verify the website configuration
Test your website to confirm that the static website hosting feature is working correctly.
-
Verify homepage access: In a browser, go to your custom domain name, for example,
http://example.com, to view the result.If the page displays the default homepage for OSS static website hosting, titled Welcome to OSS with the subtitle This is a static homepage, and has three buttons (Product Details, Product Console, and Product Documentation) at the bottom, your static website hosting configuration is working.
-
Verify the 404 page: In a browser, try to access a file that does not exist, for example,
http://example.com/missing-object.If the page displays the title Welcome to OSS and the message This is a 404 page, your 404 error page is configured correctly.
Single-page application (SPA)
This specialized hosting solution for single-page applications (SPAs) supports client-side routing and page refreshes, meeting the deployment needs of modern web applications.
For security reasons, when you access an HTML file by using a bucket domain name, browsers force a download instead of displaying the file online. To allow users to view the website, which is the primary purpose of static website hosting, you must bind a custom domain name. For more information, see Access OSS by using a custom domain name. If your bucket is in the Chinese mainland, an ICP filing is required for the custom domain name.
Step 1: Configure SPA hosting
Configure the hosting parameters to optimize for an SPA's routing features and ensure that client-side routing works correctly.
-
Go to the Buckets page, click the name of the target bucket, and then in the left-side navigation pane, click .
-
Click Settings and configure the following parameters:
-
Default Homepage: Set to
index.html. -
Subfolder Homepage: Select Disabled. With this setting, any request to the static website domain or any URL under that domain ending with a forward slash (
/) returns the root directory's default homepage. -
Default 404 Page: Set to
index.html(critical configuration: redirects all routes to the application entry point). -
Error Page Status Code: Select 200 (ensures that route transitions return the correct status code).
-
-
Click Save.
Step 2: Upload application files
Upload your prepared SPA files to the bucket to complete the application deployment.
-
Download and extract the sample application demo.zip, or use your own application files.
-
In the left-side navigation pane, click .
-
Drag all files from the
demodirectory or your own application files to the upload window. After all files are added to the list, click Upload Object.
Step 3: Set bucket permissions
Public read permission makes all objects in the bucket publicly accessible. Anyone who knows an object's URL can download it. Therefore, use this bucket for public resources like HTML, CSS, and JavaScript only. Store sensitive data in a separate bucket with stricter access controls.
Configure public access permissions so that your SPA is accessible to users.
-
Disable Block Public Access for the bucket.
-
In the left-side navigation pane, click .
-
Click the Block Public Access toggle. In the dialog box that appears, enter I confirm that I want to disable Block Public Access, and then click OK.
-
-
Set the bucket's access permissions to public-read.
-
Go to the Access Control List tab and click Set.
-
Set Bucket ACL to Public Read. In the dialog box that appears, click Continue.
-
Click Save.
-
Step 4: Verify application deployment
Test your website to confirm that the SPA is deployed successfully and can handle route transitions.
-
Verify homepage access: In a browser, go to your custom domain name (for example,
http://example.com) to access the SPA. The result is shown in the following figure.
-
Verify the 404 behavior: In a browser, try to access a file that does not exist, for example,
http://example.com/missing-object. The request is redirected to the application's entry point, and a200 OKstatus code is returned.
Deploying to a production environment
To ensure your static website runs reliably and provides a good user experience in a production environment, configure the following security and performance optimizations.
Best practices
-
Secure transport: Enable HTTPS access
Modern browsers display "not secure" warnings for HTTP websites, and search engines give higher SEO rankings to HTTPS sites. You should enforce HTTPS access for your custom domain name by following the guide to access OSS over HTTPS. The HTTPS protocol uses TLS/SSL to encrypt data in transit, which prevents data from being stolen or tampered with and improves website security and user trust.
-
Performance optimization: Configure CDN acceleration
If your static website serves a global audience or receives high-concurrency traffic, you can configure CDN acceleration. This caches your content on edge nodes around the world, which significantly improves access speed and reduces your OSS traffic costs.
-
Cross-origin resource sharing (CORS): Configure CORS rules
Single-page applications often need to call backend APIs or access third-party resources, which can be restricted by a browser's same-origin policy. By configuring cross-origin resource sharing (CORS) rules, you can specify allowed origins, methods, and headers to ensure your application can access the necessary API services and external resources.
-
Version management: Implement releases and rollbacks
A production environment requires the ability to release new versions quickly and perform emergency rollbacks. Enable the OSS versioning feature and integrate it with automation tools like Jenkins to create a full CI/CD workflow. This improves release efficiency and system stability.
Risk mitigation
-
Bandwidth theft prevention: Configure hotlink protection
When other websites directly link to your static resources, it can lead to extra traffic costs and server load. By configuring Referer-based hotlink protection, you can create a whitelist of allowed domains. This effectively prevents unauthorized websites from using your bandwidth and helps control costs.
-
Access monitoring: Enable access logs
Production environments require complete access records for security audits, performance analysis, and troubleshooting. Enable real-time log query to record detailed information for all access requests. This helps you identify unusual access patterns, analyze user behavior, and optimize website performance.
FAQ
404 error on SPA page refresh
In a single-page application, all routes are handled by client-side JavaScript. When you directly access or refresh a non-root route, the server cannot find a corresponding physical file. To solve this, set the Default 404 Page to index.html and the Error Page Status Code to 200. This configuration redirects all "not found" paths to the application's entry point, allowing the client-side router to handle them correctly.
Disabling static website hosting
-
Go to the Buckets page, click the name of the target bucket, and then in the left-side navigation pane, click .
-
Click Settings, clear the Default Homepage and Default 404 Page settings, and then click Save to disable the static website hosting feature.
Support for dynamic content
Static website hosting supports only static files, such as HTML, CSS, and JavaScript. It does not support server-side dynamic languages like PHP, Python, or Java. If you need dynamic functionality, you can use client-side rendering with a frontend framework or call backend APIs by using serverless services like Function Compute.