Hosting multiple websites in a BUCKET on Amazon S3

雨燕双飞 提交于 2019-12-03 20:27:46

To use the static website feature of s3, you can only map one domain per bucket. There is no way to tell a domain to use the folder of the bucket instead of the bucket itself.

coderunner

This is a bit tricky but doable.After some research I found a solution that can work.Steps are as follows:

  1. We need a proxy server in order to perform the operations.(eg:Nginx)
  2. We need to make config changes to the default.conf file to proxy requests to the bucket you have your website.
  3. This is the conf file:

    server {
        # Listen on port 80 for all IPs associated with your machine
        listen 80;
    
        # Catch all other server names
        server_name _;
    
        # This code gets the host without www. in front and places it inside
        # the $host_without_www variable
        # If someone requests www.coolsite.com, then $host_without_www will have the value coolsite.com
        set $host_without_www $host;
        if ($host ~* www\.(.*)) {
            set $host_without_www $1;
    
        }
    
        location / {
            # This code rewrites the original request, and adds the host without www in front
            # E.g. if someone requests
            # /directory/file.ext?param=value
            # from the coolsite.com site the request is rewritten to
            # /coolsite.com/directory/file.ext?param=value
            set $foo 'http://sites.abcd.com';
            # echo "$foo";
            rewrite ^(.*)$ $foo/$host_without_www$1 break;
    
    
            # The rewritten request is passed to S3
            proxy_pass http://sites.abcd.com;
            include /etc/nginx/proxy_params;
        }
    }
    
  4. Now in your DNS settings change your CNAME to your proxy server address(Something like router.abcd.com).The proxy server will take your request and forward it to the S3 bucket where your site is hosted.

  5. Also, you can use wwwizer.com ip address for @ record.This will send your request to correct destination irrespective of the www in your URL.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!