5

For using nginx as a caching proxy, is there a way to configure 2 upstream servers in a way that it always tries the first one unless that one returns a 404, and only in that case it will try the second upstream?

I know of the backup option in the upstream configuration block, but that only tries that server if the first one is unavailable. I'm talking about if the first server is available but just that particular requested file returns 404.

2 Answers 2

1

I'm not sure why you would want a time-out or say 50X response to be a valid one, but if you really just want 404's, then the Nginx docs explain it this way:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server

max_fails=number

sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts.

Here's the kicker:

What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream directives.

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream

Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;

http_404 a server returned a response with the code 404;

This answer to a similar post could form the base for your config:

https://stackoverflow.com/a/12878310/4995465

2
  • Thanks for the info, I'm still confused though would you mind putting an example of how the configuration file should look to get it to work like this, just a barebones config would be great to help me understand.
    – user779159
    Nov 11, 2015 at 9:43
  • 1
    See my edit with a link to stackoverflow.com/a/12878310/4995465 Please do post your own working config when you have it confirmed so others can learn from it, then accept an answer if you feel so inclined.
    – JayMcTee
    Nov 11, 2015 at 10:11
0

Yup, you can increase weight of one server in an upstream group, and play with proxy_next_upstream to include or exclude the codes you don't want to switchover occur. This way the server with a bigger weight will serve most request (majority/minority depend on the weight amount, but some requests will be served by the second one anyway).

Still, backup is the proper thing to use. I cannot imagine a reason for not using some server that always contain all the data.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .