How to add a fallback to my proxy in nginx?
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?
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;
}