Can NGINX change the response code after a proxy_pass?

nickbreaton

So I have an internal API server namespaced under /api/, and I want to pass all other requests to an Amazon S3 static site using proxy_pass. This all works fine, it's just since Amazon is serving a single page app, I want to always return the same HTML file. They way I did this with the S3 server, was to set the index and error page as the same file. It all looks fine on the surface, but for all other requests besides /, the S3 instance returns a 404. Can I use NGINX to change this to a 200 before returning it to the client?

server {
  listen 80;
  server_name example.com;

  location /api/ {
    # serve internal app
  }

  location / {
    proxy_pass http://example.amazonaws.com/;
    # ALWAYS RETURN A 200
  }
}
Lochnair

You should be able to use the error_page and proxy_intercept_errors directives to achieve this. Something like this should do the trick.

location / {
    proxy_pass http://example.amazonaws.com/;
    proxy_intercept_errors on;
    error_page 404 =302 /your_html_file
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related