Configure NGINX to proxy HTTPS requests to a server HTTP

856 Views Asked by At

I'm trying to use PostgREST API to deal with some queries in a PostgreSQL server, but the API doesn't accept HTTPS request as stated in the API documentation:

PostgREST aims to do one thing well: add an HTTP interface to a PostgreSQL database. To keep the code small and focused we do not implement HTTPS. Use a reverse proxy such as NGINX to add this, here’s how.

Our Nginx server is able to deal with HTTPS requests, but I was unable to find a Nginx configuration example that can proxy those HTTPS requests and pass them to PostgREST API as an HTTP request in order to get the JSON file from the above mentioned API.

Here's what I have tried so far:

server {
        listen 443 ssl http2;

        location = /api {
            return 301 http://$host/$request_uri;
        }
} 

Any help would be amazing!

1

There are 1 best solutions below

0
On

There's a minimal example in the docs on how to use PostgREST with Nginx in the Hardening PostgREST section. You can adapt it to HTTPS by adding that location section to the server configured for HTTPS, like this:

http {
  
  # ...

  # upstream configuration
  upstream postgrest {
    server localhost:3000;
  }

  # ...

  server {

    listen 443 ssl http2;

    # Other configuration for HTTPS (certs, etc.)
    # ...

    # expose to the outside world
    location /api/ {
      default_type  application/json;
      proxy_hide_header Content-Location;
      add_header Content-Location  /api/$upstream_http_content_location;
      proxy_set_header  Connection "";
      proxy_http_version 1.1;
      proxy_pass http://postgrest/;
    }

    # ...

  }

}

For clarity, the comments with #... reference any other code you may already have in your configuration.