[英]NGINX: Serve static files from different locations
我在兩台不同的機器(不同的IP地址)上實現了2台服務器。 讓我們將它們稱為serverA和serverB 。
serverA是serverB將要饋送一些靜態文件的位置。
serverA的配置文件是:
limit_req_zone $binary_remote_addr zone=lmz_serverA:10m rate=5r/s;
server {
listen 80; ## listen for ipv4; this line is default and implied
server_name serverA;
location /server_a {
limit_req zone=lmz_serverA burst=5 nodelay;
rewrite /server_a/(.*) /$1 break;
proxy_intercept_errors on;
proxy_pass http://0.0.0.0:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/BIG/ {
root /safe/server_a/;
autoindex off;
expires 7d;
}
location /server_a/static/{
root /safe/;
autoindex off;
expires 7d;
}
location = /favicon.ico{
alias /safe/server_a/static/images/favicon.ico;
}
}
serverB的配置文件是:
limit_req_zone $binary_remote_addr zone=lmz_serverB:10m rate=5r/s;
server {
listen 80; ## listen for ipv4; this line is default and implied
server_name serverB;
location /server_b {
limit_req zone=lmz_serverB burst=5 nodelay;
rewrite /server_b/(.*) /$1 break;
proxy_intercept_errors on;
proxy_pass http://1.0.0.0:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /server_b/static/ {
root /safe/;
autoindex off;
expires 7d;
}
- - 結束
現在,想象一下,這服務器在不同的大洲..一些靜態文件確定從serverA的 ,但BIG(/靜態/ BIG /)的東西是給我一些麻煩,因為大多數用戶都在serverB上的同洲擔任。 因此,我想從serverA剪切這些BIG靜態文件並將其放在serverB上,以便可以更輕松地下載它們。
有誰知道如何僅通過在serverB上使這些文件可用並更改nginx配置文件來實現這一目標?
重要提示 :serverA的實現了一個名為的appA Django應用程序,和ServerB實現名為appB的不同(但仍Django的)應用程序。 我無法更改這兩個應用程序的代碼。
提前致謝!
您可以從服務器執行302 Moved Temporarily
重定向,該服務器希望另一個服務器來處理大文件。
server_name serverA;
location /static/BIG/ {
return 302 $scheme://serverB$uri;
}
我在尋找類似的東西時偶然發現了這一點- 如何提供與要求不同的文件名 。 我的最終解決方案:
location /robots.txt {
# Try the beta file, which has a disallow everything
root /location/to/static/files;
try_files /robots-beta.txt =404;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.