nginx 文件服务器 s3,利用S3+nginx实现静态站点的托管

需求描述:用户有一个静态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的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值