title: Redirect to separate URL if 403 or 404
body: <p>I have an Nginx setup whereby I included a file with whitelisted IPs that can access my site's admin portal <code>admin.site.com</code>. It usually returns a 403 forbidden or 404 depending on the configuration if an unauthorized IP attempts to access my admin portal, but I feel this is not very nice.
  I need to redirect all users to <code>site.com</code> if the response is 403 or 404. Here is what I have</p>
  
  <pre><code>server {
      # listen on port 80 (http)
      listen 80;
      server_name admin.site.com;
  
      location ~ /.well-known/acme-challenge {
          allow all;
          root /var/www/.well-known/acme-challenge/;
          default_type "text/plain";
          try_files $uri =404;
      }
  
      location / {
          # redirect any requests to the same URL but on https
          return 301 https://$host$request_uri;
      }
  }
  
  server {
      # listen on port 443 (https)
      listen 443 http2 ssl;
      server_name admin.site.com;
  
      root /var/www/admin.site.com/site-frontend/;
      index index.html;
          location / {
         include /etc/nginx/snippets/whitelist.conf;
         try_files $uri $uri/ /index.html;
      }
  
      # location of SSL certificate
      ssl_certificate /etc/some/path/admin.site.com/fullchain.pem;
      ssl_certificate_key /etc/some/path/admin.site.com/privkey.pem;
      ...
      ...
      ... 
  
      # write access and error logs to /var/log
      access_log /var/log/nginx/admin.site.com_access.log;
      error_log /var/log/nginx/admin.site.com_error.log;
  }
  
  </code></pre>
  
  <p>This is the bit responsible for checking if an IP is in the whitelist:</p>
  
  <pre><code>    index index.html;
          location / {
         include /etc/nginx/snippets/whitelist.conf;
         try_files $uri $uri/ /index.html;
      }
  </code></pre>
  
  <p>How do I redirect to <code>site.com</code> if a 403 is returned?</p>