9

I like to run nginx as a local proxy server, serving stale cache files if the upstream server is unavailable.

This works well, except the local machine is restarted, while the internet connection faults. In this case, nginx won't start up at all, saying 'host not found in upstream ....'.

I need nginx start in every case, otherwise the stale cache could not be served on upstream connection fail.

So how to make nginx ignore the connection fault once on startup?

4
  • Some internet source pointed out this can be acquired by setting proxy_pass with some variables, however this would require a resolve ... directive, which won't let nginx start either, if no DNS is available at start time.
    – dronus
    Mar 12, 2015 at 11:59
  • You could setup local DNS caching server so it will always will be available to nginx. Probably you even already have one (I've got one on my linux laptop).
    – Alexey Ten
    Mar 13, 2015 at 8:53
  • For systems configured by NetworkManager for example, this is not feasable as NetworkManager starts and tears down the local dns depending on the connection state. So no connection also means no local DNS. Running an own local DNS would be problematic too, as it needs to rebind interfaces going up and down by NetworkManager's control.
    – dronus
    Mar 21, 2015 at 20:26
  • This question could be improved by including a snippet of the Nginx configuration that seems to be related to the failure to start, as well as the exact error message experienced. Also, phrase the title as a question. Dec 10, 2015 at 20:10

2 Answers 2

3

You can setup an upstream server with backup option, then won't be normally hit.

upstream cache {
    server 192.168.1.2:8080 fail_timeout=5s max_fails=3;
    server 127.0.0.1:82 backup;
}

location / {
    proxy_pass http://cache;
    proxy_next_upstream error http_502;
}

Make sure your primary cache server return consistent error, so that the failure is quickly detected.

1
  • Won't work without an resolver at all, which is the state on my systems if connection fails.
    – dronus
    Mar 24, 2020 at 16:33
2

Nice trick (not only for docker deployments) is mentioned at https://sandro-keil.de/blog/let-nginx-start-if-upstream-host-is-unavailable-or-down/

All kudos to Sandro Keil -- just for reference -- basically he defines a resolver with timeout and all upstream servers through variables:

server {
    # this is the internal Docker DNS, cache only for 30s
    resolver 127.0.0.11 valid=30s;

    location ^~ /api/ {
        # other config entries omitted for breavity

        set $upstream api.awesome.com:9000;

        # nginx will now start if host is not reachable
        fastcgi_pass    $upstream; 
        fastcgi_index   index.php;
    }
}

I use this solution for local development (allows me to start only subset of services with single nginx config) and also at production.

1
  • Won't work without an resolver at all, which is the state on my systems if connection fails.
    – dronus
    Mar 24, 2020 at 16:33

You must log in to answer this question.

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