Postgrest: Allow url prefix for API

Created on 14 Feb 2017  路  15Comments  路  Source: PostgREST/postgrest

Allow using either server-proxy-uri = '/api'
setting or other setting to put all API under prefix.

Most helpful comment

I can't speak for this project's owners, but you can solve this with NGINX config that proxies back to postgrest. Having some kind of reverse-proxy in front lets you solve lots of other problems too, which are apparently considered out of scope of postgrest, like

So it's generally a convenient thing to have set up :)

All 15 comments

I can't speak for this project's owners, but you can solve this with NGINX config that proxies back to postgrest. Having some kind of reverse-proxy in front lets you solve lots of other problems too, which are apparently considered out of scope of postgrest, like

So it's generally a convenient thing to have set up :)

I am using Nginx.

However using Nginx as reverse proxy and putting all under /api prefix results in OpenAPI (Swagger JSON) specification being incorrect.

I can`t use Swagger UI with proxied URL, I must use direct :3000 port to access UI and this is not always possible/desirable.

Can you give an example of how the OpenAPI results are incorrect?

In nginx I have reverse proxy setting:

location /myapp/api {
proxy_pass http://localhost:3000;
}

When requesting http://localhost/myappi/api I receive Swagger JSON / OpenAPI schema which includes
host: "0.0.0.0:3000"
basePath: "/"
Settings which results in Swagger UI generating wrong URLs.

Try using a complete URI for server-proxy-uri. There's an example in https://postgrest.readthedocs.io/en/v0.4/admin.html#configuration

There is certain situations when complete URI is not known to postgrest. Can this URI be specified as relative? host and basePath is not a required fields as per Swagger JSON specification.
http://swagger.io/specification/#swagger-object-14

For debugging let's see if a full uri works, and then if so how much we are able to omit and have it continue to work.

Any progress?

Full uri works fine - tried both Windows 7 x64 and Ubuntu Trusty 14.04.

I am packaging PostgREST as part of vagrant (and docker for production) build and setting full URL of proxied inside container is wrong as URL can change.

I`ve solved this declaring some server-proxy-uri and stripping it from response using Nginx http://nginx.org/en/docs/http/ngx_http_sub_module.html

sub_filter ',"host":"localhost:8080","basePath"' ',"basePath"';
sub_filter_types *;
sub_filter_once on;

I'm glad you found a workaround, but couldn't you start postgrest with a different server-proxy-uri config parameter for each deployment?

Tentatively closing this issue.

for each deployment?
That is not possible because in general, backend does not knows about frontend.
For example, cloud.docker.com generated URLs with dynamic DNS.

I would also appreciate this functionality, for similar reasons to @huksley. My PostgREST deployment is in a composed set of Docker containers, behind a reverse proxy, and though I know which local URI should point to the REST API, the deployment will not know the hostname ahead of time.

If setting the basePath property seperately from the rest of the URL is not possible, would determining it based on the Host HTTP header be an option?

I'm also interested in this. There are many ways to deploy where the full URL isn't known before hand, but the basePath is.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daurnimator picture daurnimator  路  5Comments

begriffs picture begriffs  路  4Comments

begriffs picture begriffs  路  3Comments

a-mckinley picture a-mckinley  路  3Comments

priyank-purohit picture priyank-purohit  路  3Comments