nginxgoogle-cloud-platformgoogle-cloud-endpointsapi-gatewaygoogle-cloud-run

APIs in Cloud Run and Nginx reverse proxy in VM


I have deployed 6 different Flask based applications to Google Cloud Run. They work perfectly fine when I access them through the autogenerated URL. Now, I want to unify all 6 services under one domain name with different routes.

For example,
mydomain.com/user -> https://custom-user-asdtgthyju-de.a.run.app
mydomain.com/product -> https://custom-product-asdtgthyju-de.a.run.app

Things I have tried
1. Nginx deployed in a separate VM with reverse proxy to the cloud run URLs
Not working, same configuration-same code deployed in regular VMs work but for cloud run deployments shows route "/user" not found

2. Cloud Endpoints using ESPv2
https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-run
Got this working as per my requirement, but not able to pass the custom headers, like I use X-API-KEY for authentication, it doesn't even get to the Cloud Run. It is being stripped off by ESPv2 itself.

Please help, how I can configure a reverse proxy/ API gateway in front of cloud run services. Has anyone tried External Nginx to Cloud Run mapping?

Thanks


Solution

  • @petomalina's answer is the easiest if your Cloud Run service is public. If it's not, it won't work as per this answer

    If your service is internal, I tried OP's #1 option of putting an nginx reverse proxy in front and was getting the same error: 404.

    The issue is caused by the Host header having the host of the proxy, not of the Cloud Run service. The fix is to configure nginx to override the Host header:

    upstream my-foo {
        server  foo.run.app:443;
    }
    location /foo/ {
        proxy_pass  https://my-foo;
        proxy_set_header Host foo.run.app;
        include /etc/nginx/proxy_params;
        proxy_ssl_session_reuse on;
    }