samthgreat_pt
samthgreat_pt

Reputation: 103

NGINX: Serve static files from different locations

I have 2 servers implemented on two different machines (different IP adresses). Lets call them serverA and serverB.

serverA is where serverB is going to feed for some static files.

serverA configuration file is:

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 configuration file is:

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;
    }

--- end

Now, imagine that this servers are in different continents.. some static files are OK to be served from serverA but the BIG (/static/BIG/) stuff is giving me some trouble because the majority of users are in the same continent of serverB. So I want to cut those BIG static files from serverA and put them on serverB so they can be more easily downloaded.

Does anyone have any idea how can I accomplish that just by making those files available on serverB and changing nginx configuration files?

IMPORTANT: serverA implements a Django application named appA, and serverB implements a different (yet still Django) application named appB. I can't change the code of those two apps.

Thanks in advance!

Upvotes: 0

Views: 1347

Answers (2)

Berto
Berto

Reputation: 712

I stumbled onto this while searching for something similar -- how to serve a different filename than what's requested. My final solution:

location /robots.txt {
       # Try the beta file, which has a disallow everything
       root /location/to/static/files;
       try_files /robots-beta.txt =404;
}

Upvotes: 1

cnst
cnst

Reputation: 27218

You can do a 302 Moved Temporarily redirect from the server which wants another one to serve the big files.

server_name serverA;
location /static/BIG/ {
    return  302 $scheme://serverB$uri;
}

Upvotes: 0

Related Questions