postgrest icon indicating copy to clipboard operation
postgrest copied to clipboard

Allow url prefix for API

Open huksley opened this issue 8 years ago • 21 comments

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

huksley avatar Feb 14 '17 15:02 huksley

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 :)

uniphil avatar Feb 14 '17 18:02 uniphil

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.

huksley avatar Feb 16 '17 08:02 huksley

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

begriffs avatar Feb 16 '17 09:02 begriffs

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.

huksley avatar Feb 18 '17 10:02 huksley

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

begriffs avatar Feb 19 '17 19:02 begriffs

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

huksley avatar Feb 19 '17 20:02 huksley

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.

begriffs avatar Feb 19 '17 21:02 begriffs

Any progress?

begriffs avatar Feb 23 '17 05:02 begriffs

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.

huksley avatar Feb 24 '17 06:02 huksley

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;

huksley avatar Feb 27 '17 11:02 huksley

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

begriffs avatar Feb 27 '17 19:02 begriffs

Tentatively closing this issue.

begriffs avatar Mar 05 '17 20:03 begriffs

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.

huksley avatar Mar 29 '17 18:03 huksley

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?

treuherz avatar May 11 '17 14:05 treuherz

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.

nathancahill avatar Mar 25 '20 21:03 nathancahill

Would you consider a PR?

adrian-gierakowski avatar Aug 01 '22 20:08 adrian-gierakowski

Another use case here is that I'm (attempting) to use a reverse proxy to let me serve different schemas at different paths. It's all working except for the base_url in the openapi spec.

magicrobotmonkey avatar Jan 25 '23 17:01 magicrobotmonkey

Reopening. I can see the need for a server-proxy-uri config, would gladly review a PR.

steve-chavez avatar Jan 25 '23 17:01 steve-chavez

I'm currently configuring a PostgREST behind a proxy, it would be great if there would be a config option so that PostgREST would figure out the server-proxy-uri by itself. It is totally possible based on HTTP headers Host and X-Forwarded-Proto.

naquad avatar Mar 23 '23 10:03 naquad

it there is any news about this topic?

yasz avatar Jul 25 '23 15:07 yasz

Setting the context path is a very basic requirement for any API platform.

mingfang avatar Oct 09 '24 05:10 mingfang