You can host static files such as HTML, CSS, and JavaScript in an Object Storage Service (OSS) bucket and serve them as a publicly accessible website. No traditional servers are required, which reduces operational costs and technical complexity.
Standard static website
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.
-
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)
Host a single-page application (SPA) with support for client-side routing and page refreshes.
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 hosting parameters to 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 SPA files to the bucket to complete the 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 handles route transitions correctly.
-
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
Configure the following security and performance optimizations for a production-ready static website.
Best practices
-
Secure transport: Enable HTTPS access
Modern browsers display "not secure" warnings for HTTP websites, and search engines rank HTTPS sites higher. Enforce HTTPS access for your custom domain name by following the guide to access OSS over HTTPS. HTTPS uses TLS/SSL to encrypt data in transit, preventing data theft or tampering.
-
Performance optimization: Configure CDN acceleration
If your static website serves a global audience or handles high-concurrency traffic, configure CDN acceleration to cache content on edge nodes worldwide. This improves access speed and reduces OSS traffic costs.
-
Cross-origin resource sharing (CORS): Configure CORS rules
SPAs often call backend APIs or access third-party resources, which can be restricted by a browser's same-origin policy. Configure CORS rules to specify allowed origins, methods, and headers so your application can access the necessary APIs and external resources.
-
Version management: Implement releases and rollbacks
Enable OSS versioning and integrate it with automation tools like Jenkins to create a CI/CD workflow for quick releases and emergency rollbacks.
Risk mitigation
-
Bandwidth theft prevention: Configure hotlink protection
Other websites that directly link to your static resources increase traffic costs and server load. Configure Referer-based hotlink protection to create a whitelist of allowed domains and prevent unauthorized bandwidth usage.
-
Access monitoring: Enable access logs
Enable real-time log query to record 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. Server-side languages like PHP, Python, or Java are not supported. For dynamic functionality, use client-side rendering with a frontend framework or call backend APIs through serverless services like Function Compute.