1 min read

Serving a Static Site using Nginx from Linode Object Storage

Based on the documentation provided in Deploy a Static Site using Hugo and Object Storage, it is recommended to initialize the bucket as a website to enable accessing the / (index page) without explicitly using index.html. This can be achieved by executing the following command:

s3cmd ws-create --ws-index=index.html --ws-error=404.html s3://my-bucket

However, an alternative approach to achieve the same effect is by utilizing a special endpoint for the website. For instance, if your bucket address is my-bucket.ap-south-1.linodeobjects.com, appending website- before the region results in my-bucket.website-ap-south-1.linodeobjects.com.

To configure Nginx as a reverse proxy for this endpoint, incorporate the following location snippet within your server scope in the Nginx configuration:

location /my-site/ {
    set $bucket "my-bucket.website-ap-south-1.linodeobjects.com";
 
    resolver 1.1.1.1;
 
    proxy_http_version 1.1;
 
    # replace "sites" with your actual path
    proxy_pass http://$bucket/sites$request_uri;
 
    proxy_set_header Host $bucket; # Use bucket as Host
    proxy_set_header Connection ""; # KeepAlive
    proxy_set_header Authorization ""; # Remove authorizationheader
    proxy_set_header X-Real-IP $remote_addr; # Pass client IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Pass client IP
 
    add_header X-Proxy-Pass "HIT";
    add_header Cache-Control "public, max-age=31536000";
}

Assuming your site is located at my-bucket.website-ap-south-1.linodeobjects.com/sites/my-site, you can access it through your main domain using a URL like http://example.com/my-site/, all without explicitly referencing index.html.