使用nginx搭建creates.io鏡像的教程詳解
在Rust開發(fā)中,經(jīng)常需要使用Cargo從crates.io下載依賴,而國內(nèi)幾乎沒有好用的crates.io鏡像,大多都只對crates.io-index和crates.io進行了鏡像,而最重要的static.crates.io卻沒有鏡像。迫不得已只能自己搭建。眾所周知,Cargo下載依賴,實際分為三步:
- 獲取索引(Fetch index)
- 查詢下載路徑(Redirect location)
- 下載資源(*.crate文件)
因此,搭建crates.io鏡像也需要分為三個模塊:
- crates.io-index
- crates.io
- static.crates.io
零、啟用nginx的緩存功能
注:此處均為與搭建creates.io鏡像相關(guān)的配置項
http {
# 開啟全局緩存,并配置存儲路徑(/var/lib/crates.io)和大小(32g)
proxy_cache_path /var/lib/crates.io keys_zone=STATIC:100m levels=1:2 inactive=120h max_size=32g;
}
一、緩存creates.io
server {
listen 7011;
location / {
proxy_pass https://crates.io;
proxy_ssl_server_name on;
proxy_buffering on;
proxy_cache STATIC;
proxy_cache_valid 301 302 307 308 72h;
proxy_cache_valid any 10s;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_redirect https://static.crates.io/ http://$host:7012/;
}
}
此處,我們將http://localhost:7011反向代理到https://crates.io,并對結(jié)果進行緩存。其中狀態(tài)碼為301,302,307,308的緩存72小時,其余狀態(tài)碼緩存10秒。
二、緩存static.crates.io
server {
listen 7012;
location / {
proxy_pass https://static.crates.io;
proxy_ssl_server_name on;
proxy_buffering on;
proxy_cache STATIC;
proxy_cache_valid 200 72h;
proxy_cache_valid 400 502 504 10s;
proxy_cache_valid any 1m;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
}
此處,我們將http://localhost:7012反向代理到https://static.crates.io,并對結(jié)果進行緩存。
需要注意到在 緩存creates.io 部分,proxy_redirect參數(shù),就是將原本重定向目標從https://static.crates.io/修改為http://$host:7012/
三、緩存creates.io-index
在前幾步中,我們已經(jīng)將一個creates.io鏡像的內(nèi)容部分搭建完畢,最后就是索引,眾所周知,crates.io是使用git倉庫 https://github.com/rust-lang/crates.io-index.git 作為官方索引的,因此我們只需要將這個倉庫clone到本地
cd /home/ # 此處將索引clone到/home目錄下 git clone https://github.com/rust-lang/crates.io-index.git
然后修改/home/crates.io-index/config.json,將https://crates.io修改為我們剛剛的代理地址http://localhost:7011,除了lo地址也可以用本機的其他ip地址。
sed -i 's+https://crates.io+http://localhost:7011+g' /home/crates.io-index/config.json
最后依然是nginx配置:
server {
listen 7010;
location /crates.io-index/ {
root /home;
}
}
以上,一個鏡像源就配置完成啦~~~
四、在開發(fā)環(huán)境配置鏡像源
修改HOME目錄下的.cargo/config.toml文件(也可以是不帶后綴名的config文件)如果沒有就新建一個。
Linux/Unix系統(tǒng)為 $HOME/.carog/config.toml
Windows系統(tǒng)為 $USERPROFILE/.carog/config.toml
內(nèi)容為:
[source.crates-io] replace-with = 'local' [source.local] registry = "sparse+http://localhost:7010/crates.io-index/"
五、寫在最后
目前能夠找到的搭建creates.io鏡像源的文章,全部都是使用基于git over https的傳統(tǒng)方式,本篇文章則是采用最新的sparse稀疏索引方式,大幅加快獲取包的速度,搭建過程更加便捷。
另外,對于索引的更新,可以通過shell腳本+crontab定時任務來完成自動更新:
#!/usr/bin/sh cd /home/crates.io-index/ git pull
保存為/home/crates.io-index/update-index.sh,并添加執(zhí)行權(quán)限:
chmod 755 /home/crates.io-index/update-index.sh
添加定時任務:
# crontab -e 0 * * * * /home/crates.io-index/update-index.sh > /home/crates.io-index/update-index.log 2>&1 &
到此這篇關(guān)于使用nginx搭建creates.io鏡像的教程詳解的文章就介紹到這了,更多相關(guān)nginx搭建creates.io鏡像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux下Nginx負載均衡多個tomcat配置的方法步驟
這篇文章主要介紹了Linux下Nginx負載均衡多個tomcat配置的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
nginx內(nèi)部訪問特性如何實現(xiàn)靜態(tài)資源授權(quán)訪問
這篇文章主要介紹了nginx內(nèi)部訪問特性如何實現(xiàn)靜態(tài)資源授權(quán)訪問方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Nginx通過用戶IP獲取所在國家及地理位置的實現(xiàn)方法
Nginx是一款高性能、輕量級的Web服務器和反向代理服務器,今天講解Nginx十分常用的功能之一,通過IP獲取用戶所在的國家,一般廣泛應用在各類需要定位的網(wǎng)站上面,來定位用戶首次訪問的國家,通過IP解析庫GeoLite2-Country來實現(xiàn)功能,需要的朋友可以參考下2023-10-10
采用ngxtop實現(xiàn)nginx實時訪問數(shù)據(jù)統(tǒng)計
這篇文章主要介紹了采用ngxtop實現(xiàn)nginx實時訪問數(shù)據(jù)統(tǒng)計,需要的朋友可以參考下2014-07-07
nginx配置訪問圖片路徑以及html靜態(tài)頁面的調(diào)取方法
這篇文章主要介紹了詳解nginx配置訪問圖片路徑以及html靜態(tài)頁面的調(diào)取方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。2016-12-12

