Score:0

nginx how to replace try_files with proxy_pass having multiple paths

us flag

I have a set of rules in Nginx of this type:

location ~ ^/race/[a-z0-9\-_]*/session/[a-z0-9\-]*/topic/[a-z0-9\-_]*$ {
   try_files $uri $uri/ /race/period/session/team/topic/index.html;
}

location /images/assets/ {
   try_files /backup/data$uri /default_images/small_asset.png =404;
}

These rules work when the files exist on the server but now the data is in a remote location (AWS - S3+cloudfront) I need to use proxy_pass, but wondering how to deal with cases when I have multiple choices like:

try_files $uri $uri/ /race/period/session/team/topic/index.html;

And

try_files /backup/data$uri /default_images/small_asset.png =404;

How to handle the first path in a remove endpoint for example:

location /images/assets/ {
   proxy_pass http://abc.cloudfront.net/backup/data 
   # but if I get a 404 here,how to fall back to: 
   proxy_pass http://abc.cloudfront.net/default_images/small_assets.png
}

Any ideas? not necessary I need to use Nginx now that the content is hosted remotely, open to any alternatives.

Richard Smith avatar
jp flag
To fallback on 404 you would need to use `error_page`. See [this document](http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page).
us flag
any idea how to also deal with the regex when using proxy_pass?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.