'Nginx local proxy with backup to prod server

I am trying to setup nginx proxy on my local machine. What I want to achieve is like this:

site.local -> proxy_pass -> HTTP://localhost:3000 if localhost:3000 is down, then proxy to -> site.com

So far this is my config:

upstream mysite {
  server localhost:3000 fail_timeout = 5 max_fails = 1;
  server example.com backup max_fails = 2;
}

server {
  server_name site.local;
  listen ssl;
  ssl_certificate /path/to/site-local.crt;
  ssl_certificate_key /path/to/site-local.key;
  ssl_ciphers HIGH: !aNULL: !MD5;


  location / {
    proxy_pass https://mysite;
    proxy_set_header Host site.com;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

And I've made entry in hosts file for site.local 127.0.0.1.

I am getting multiple errors with slightly different configurations:

  1. I am doing proxy_pass HTTP://mysite, then it is 301 redirecting to https://example.com, and not proxying to https.
  2. If I did proxy_pass: https://mysite, then it is giving 502.
  3. If I am doing server site.com:443 then it is saying non https traffic to 443 server is not allowed.

Any ideas around how to achieve this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source