溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何利用S3+nginx實現(xiàn)靜態(tài)站點的托管

發(fā)布時間:2021-08-17 14:47:20 來源:億速云 閱讀:358 作者:chen 欄目:云計算

這篇文章主要講解了“如何利用S3+nginx實現(xiàn)靜態(tài)站點的托管”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何利用S3+nginx實現(xiàn)靜態(tài)站點的托管”吧!

需求描述:
用戶有一個靜態(tài)website,都是html和js/css/img這些文件,同時還有一個域名static-website.com,需要將用戶訪問的請求由nginx轉發(fā)到后端的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轉發(fā)配置

設置/etc/nginx/conf.d/default.conf,,內(nèi)容如下,具體內(nèi)容各位根據(jù)實際情況進行修改

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的內(nèi)容

感謝各位的閱讀,以上就是“如何利用S3+nginx實現(xiàn)靜態(tài)站點的托管”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對如何利用S3+nginx實現(xiàn)靜態(tài)站點的托管這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI