Setting up NGINX reverse proxy for S3 hosted websi

2020-03-08 05:36发布


I am working on hosting static websites on amazon S3.The structure of the website would be bucket-name/site-name/files.html.Now,my issue is the user can use his own domain to publish the website.For ex: He owns a domain like www.ABC.com and wants to host his site there.
I have set up a reverse proxy server on an ec2 instance for proxing the requests i.e some one hitting www.ABC.com should see the content from the S3 bucket or the domain name should point to the S3 bucket.
I am aware there are DNS changes and updation of CNAME and A records,but I also need to write RULES in NGINX config to redirect the URL as I want.

This is the structure I have,not working and would like to have experts a look at: Currently I publish my sites on a subdomain sites.development.com/bucket-name/sitename.
This is my default.conf file after nginx installation

     server {
     listen       x.x.0.0:80;
     server_name  x.x.x.x;
     access_log  /var/log/nginx/access.log;
     error_log   /var/log/nginx/error.log;
     root        /usr/local/nginx/html;
     index       index.html;


    location / {
        proxy_pass  http://development.mydomain.com:9585;
        include /etc/nginx/proxy_params;
    }
   }

Currently I am setting it on a development server,whose URL is http://development.mydomain.com(this is a independent ec2 instance). My proxy server is running on a different EC2 instance than http://development.mydomain.com.

I have come up with a some what structure based on suggestions from different sources.
This is it:

    server {
    listen 80;
    server_name x.x.x.x.; //This would be the name on which I have NGINX installed,right?

    set $host_without_www $host; //What would be the host?any host with www pointing to the site on S3?
    if ($host ~* www\.(.*)) {
       set $host_without_www $1;
    }

    location / {
        rewrite ^(.*)$ /$host_without_www$1 break;
        proxy_pass {{s3-bucket-url}};
    }
}

I have no experience with NGINX and proxy servers and hence stuck for some time.
Please share comments based on your experiences and suggest a solution.
Thank you for your attention

0条回答
登录 后发表回答