title: Nginx: setting the error page to a static file
body: <p>I have the following nginx configuration:</p>
  
  <pre><code>server {
    listen          80;
    server_name     default;
  
    location / {
      proxy_set_header Host $host;
      proxy_pass http://127.0.0.1:8080/;
    }
  
  }
  </code></pre>
  
  <p>When the internal service at <code>http://127.0.0.1:8080/</code> is not responding nginx return a <em>502 Bad Gatway error</em>.</p>
  
  <p><strong>My question</strong>: how can I configure nginx to returning a static html file, e.g. <code>/var/www/error.html</code>, when such error occurs?</p>
  
  <hr>
  
  <p><strong>What I tried</strong></p>
  
  <p>Taking a cue <a href="https://stackoverflow.com/questions/13621915/nginx-error-pages-one-location-rule-to-fit-them-all/13624139#13624139">from here</a> I tried this:</p>
  
  <pre><code>server {
    listen          80;
    server_name     default;
  
    location / {
      proxy_set_header Host $host;
      proxy_pass http://127.0.0.1:8080/;
    }
  
    error_page 404 502 /error.html;
  
    location = /error.html {
      internal;
      root /var/www;
    }
  
  }
  </code></pre>
  
  <p>It's working fine if the service (on the port <code>8080</code>) is down, but when the service is up and I try to access the url <code>/error.html</code> nginx match the last location (returning me the static file) and I would like to avoid it.</p>