需求描述:用户有一个静态website,都是html和js/css/img这些文件,同时还有一个域名static-website.com,需要将用户访问的请求由nginx转发到后端的S3。
website文件夹结构如下
website:
-- index.html #首页文件
-- js/ js目录
-- css/ css目录
-- img/ img目录
1. 上传文件并设置对应权限
使用s3cmd上传website文件夹并设置所有文件为public-read权限,以bucket名称为website-bucket为例
s3cmd put website s3://website-bucket --recursive --acl-public
2. nginx转发配置
设置/etc/nginx/conf.d/default.conf,,内容如下,具体内容各位根据实际情况进行修改
server {
listen 80;
server_name static-website.com *.static-website.com;
location ~ ^/(img|js|css)/ {
proxy_set_header Host 'website-bucket.s3.endpoint.com';
proxy_pass http://website-bucket.s3.endpoint.com:80;
}
location /index.html {
proxy_set_header Host 'website-bucket.s3.endpoint.com';
proxy_pass http://website-bucket.s3.endpoint.com:80;
}
location / {
rewrite ^/$ /index.html last;
}
}
3.测试访问
curl http://static-website.com #可以看到index.html的内容