title: How to change the status code of a proxied server response in nginx?
body: <p>I'm having a hard time configuring nginx to act as a proxy of a public S3 endpoint. My use case necessitates altering the status code of the S3 response, while preserving the response payload. </p>
  
  <p>The possible status codes returned by S3 include 200 and 403. For my use case, I need to map those status codes to 503.</p>
  
  <p>I have tried the following which does not work:</p>
  
  <pre><code>location ~* ^/.* {
    [...]
    proxy_intercept_errors on;
    error_page             200 =503 $upstream_http_location
  }
  </code></pre>
  
  <p>Nginx outputs the following error:</p>
  
  <blockquote>
    <p>nginx: [emerg] value "200" must be between 300 and 599 in /etc/nginx/nginx.conf:xx</p>
  </blockquote>
  
  <p>Here's a more complete snippet:</p>
  
  <pre><code>server {
    listen       80;
  
    location ~* ^/.* {
      proxy_http_version         1.1;
      proxy_method               GET;
      proxy_pass                 http://my-s3-bucket-endpoint;
      proxy_pass_request_body    off;
  
      proxy_set_header       Content-Length "";
      proxy_set_header       Connection "";
      proxy_set_header       Host my-s3-bucket-endpoint;
      proxy_set_header       Authorization '';
  
      proxy_hide_header      x-amz-id-2;
      proxy_hide_header      x-amz-request-id;
      proxy_hide_header      Set-Cookie;
      proxy_ignore_headers   "Set-Cookie";
  
      proxy_cache            S3_CACHE;
      proxy_cache_valid      200 403 503 1h;
      proxy_cache_bypass     $http_cache_purge;
      add_header             X-Cached $upstream_cache_status;
  
      proxy_intercept_errors on;
      error_page             200 =503 $upstream_http_location;
    }
  }
  </code></pre>
  
  <p>Is it possible to achieve what I need with nginx?</p>