Report this

What is the reason for this report?

How can I connect my postgREST http upstream to my Digital Ocean app?

Posted on November 14, 2020

Greetings, I’m trying to connect my postgREST API that’s running from my droplet, to my main domain that’s on Digital Ocean’s app platform. I’m a little confused on how to do so because I cannot manually configure my main domain’s VM. I’m pretty new with networking like this but I believe I’m looking to set up a proxy?

Given from postgREST documentation, the nginx config file looks something like this. if my domain name from the App platform is called, DOMAIN.com, where would that be configured here? I only want my postgREST running on localhost in the VM, but can be accessed using my domains http address.

http {
  ...
  # upstream configuration
  upstream postgrest {
    server localhost:3000;
    keepalive 64;
  }
  ...
  server {
    ...
    # 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/;
    }
    ...
  }
}

Would really appreciate some guidance here. thank you!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

To connect your PostgREST API running on a Droplet to your main domain on the DigitalOcean App Platform, you’re on the right track with setting up a proxy! Utilizing nginx as a reverse proxy can forward requests to your PostgREST API.

Given the provided nginx configuration and your scenario, let’s walk through how you might configure this:

On the Droplet (PostgREST API Host)

  • Nginx Configuration:
http {
    ...
    upstream postgrest {
        server localhost:3000;
        keepalive 64;
    }
    
    server {
        listen 80;
        server_name api.DOMAIN.com;  # Subdomain for the API

        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/;
        }
    }
    ...
}

On the DigitalOcean App Platform (Your Main Domain)

  • Domain Configuration

Supposing you want to use DOMAIN.com for your frontend hosted on the DigitalOcean App Platform, you would configure a custom domain through the App Platform UI.

DNS Settings

Ensure your domain’s DNS settings are configured correctly:

  • DOMAIN.com should point to your App on the DigitalOcean App Platform.
  • api.DOMAIN.com should point to the droplet running your PostgREST API and nginx.

Example DNS Records

A      | DOMAIN.com       | App Platform IP
CNAME  | api.DOMAIN.com   | Droplet IP address

Secure the Connections

Ensure to use HTTPS for your connections:

  1. SSL for Droplet: You should also secure your API using SSL. You can use Certbot to easily add a free Let’s Encrypt certificate to your nginx setup.

  2. SSL for App Platform: For your app running on the DigitalOcean App Platform, SSL certificates are automatically provided, so ensure to access your app using https://DOMAIN.com.

Summary

  • Your API runs on a droplet, and you use nginx to expose it, securing it with SSL. It will be accessible via https://api.DOMAIN.com.

  • Your main app on the DigitalOcean App Platform is accessible via https://DOMAIN.com.

  • The app on https://DOMAIN.com can communicate with your API over the internet securely by making requests to https://api.DOMAIN.com/api/.

Once configured, ensure to test by making a request to https://api.DOMAIN.com/api/ to confirm that nginx is correctly proxying requests to your PostgREST API.

This setup keeps your API on a subdomain and your main app on the primary domain, segregating the two while allowing for secure communication between them. Always remember to keep your software, and configuration, especially security-related, up-to-date.

Hope that this helps!

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.