How to add a fallback to my proxy in nginx?

13,270

Solution 1

I found the answer:

server {
    listen       8080;
    server_name  mydomain;
    access_log   /log/path/logging.log;
    error_page   400 401 402 403 404 405 500 501 502 503 504  @error_page;

    location     @error_page {
        root       /var/www/html/;
        rewrite ^  https://domain.com/error/index.html;
        break;
    }

    location / {
        proxy_redirect          off;
        proxy_pass_header       Server;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Scheme $scheme;
        proxy_set_header        Host $http_host;
        proxy_set_header        X-NginX-Proxy true;
        proxy_connect_timeout   5;
        proxy_read_timeout      240;
        proxy_intercept_errors  on;

        proxy_pass              http://127.0.0.1:1337;
    }
}

This will redirect all traffic from maindomain:8080 to https://domain.com/error/index.html if the service on http://127.0.0.1:1337 is unavailable(all errors).

Hope this helps someone in the future.

Solution 2

You're missing that Nginx is doing exactly what it should, and your expectations are incorrect. You're telling Nginx "use this gateway", then when the gateway isn't there it's correctly reporting this.

What would you want or expect to happen if your node application stops working?

Update - here's how I do fallback from HHVM to PHP5, because HHVM fails fairly regularly.

upstream php {
  server 127.0.0.1:9001;
}
upstream php-fpm {
  server unix:/var/run/php-fpm/php-fpm.sock;
}

# Inside server block
location ~ \.(hh|php)$ {
    error_page 502 = @fallback;
    fastcgi_pass   php;
}
Share:
13,270

Related videos on Youtube

Dominik
Author by

Dominik

👋 Hi there, I work on Design Systems and was fortunate enough to have been given the opportunity to built the Westpac GEL and the Australian federal governments own design system and on the Gov.au UI-Kit known as GOLD and am currently working on Shopifys Polaris. Transforming the digital landscape of any company starts(and ends) with the culture and a lot of empathy around the past the current situation. A design system is above all a community that is owned by all in the business. Coming together and learning from each other is all we need to do. And that is what I love the most.

Updated on September 18, 2022

Comments

  • Dominik
    Dominik almost 2 years

    I am trying to build a proxy for my node application through nginx thinking this will give me error pages if the node application failed or is not available on the port.

    The node app runs on 127.0.0.1:1337 and I am listening to mydomain:8080 and forward it.

    server {
        listen       8080;
        server_name  mydomain;
        access_log   /log/path/logging.log;
        root         /path/to/root/;
        error_page   400 401 402 403 404 500 501 502 503 504  /error/index.html;
    
        location / {
            proxy_redirect          off;
            proxy_pass_header       Server;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Scheme $scheme;
            proxy_set_header        Host $http_host;
            proxy_set_header        X-NginX-Proxy true;
            proxy_connect_timeout   5;
            proxy_read_timeout      240;
            proxy_intercept_errors  on;
    
            proxy_pass              http://127.0.0.1:1337;
        }
    }
    

    Unfortunately it gives my a 502 bad gateway when I turn off the node app and request mydomain:8080. This is the expected behaviour from nginx though. What I want is a page I can define that will display when the service of the node app is down.

    Any ideas what how to do that?

  • Dominik
    Dominik over 8 years
    Exactly. I am only listening to port 8080 but want to forward all traffic to port 80 if the node proxy_pass generates an error.
  • Dominik
    Dominik over 8 years
    Updated question. Hope that nails what I'm looking for better.
  • Tim
    Tim over 8 years
    Great. Happy that I could set you off in the right direction :)