Home > Enterprise >  using subdomains on Cloud Run with nginx
using subdomains on Cloud Run with nginx

Time:01-15

The idea:

  1. I want to run a static site deployed on NGINX server in Cloud Run.
  2. This server is going to have two subdomains - basically, I want to have two domains assigned to one Cloud Run instance.

The reason for this is to have two slightly different sites within one container/cloud run instance (since they are only slightly different, having completely separate services does not seem sensible).

The problem:

Let's say I have two subdomains: sub1.example.com and sub2.example.com. I described them both in my nginx config, seemingly the config is correct - the server starts up in the container and I can even reach one of the subdomains (let's say sub1.example.com). When I try to access sub2.example.com though, I get a 500 error. I do not think it is an issue with my nginx config, because the logs show something strange (obviously all the project info has been edited out by me):

{
  "insertId": "63bfe64f00062b37157ff3d1",
  "httpRequest": {
    "requestMethod": "GET",
    "requestUrl": "http://sub2.example.com/.well-known/acme-challenge/yjin9yFmV0NlXspNoP1_vLRP-0UBONJGPtl61l5I_gPuUsubDg2o88NS1GO8oA_Q",
    "requestSize": "409",
    "status": 500,
    "responseSize": "814",
    "userAgent": "Google-Certificates-Bridge",
    "remoteIp": <remote-ip>,
    "serverIp": <server-ip>,
    "latency": "0.010053946s",
    "protocol": "HTTP/1.1"
  },
  "resource": {
    "type": "cloud_run_revision",
    "labels": {
      "service_name": "service-name",
      "location": "europe-west1",
      "project_id": "project-id",
      "configuration_name": "configuration-name",
      "revision_name": "revision-name"
    }
  },
  "timestamp": "2023-01-12T10:51:59.404279Z",
  "severity": "ERROR",
  "labels": {
    "instanceId": "0071bb48153ed55117f678f5d9609fe439a5eb69edd72da13aefdc58878f4c75f5b2345b96cf921b11f9f93e6833db2fa9339c9905b1de7ab8798984b4e121ca7f"
  },
  "logName": "projects/project-name/logs/run.googleapis.com/requests",
  "trace": "projects/project-name/traces/3548cd1a6ced04d963dc94419e208959",
  "receiveTimestamp": "2023-01-12T10:51:59.676145078Z",
  "spanId": "244873288111162464",
  "traceSampled": true
}

The question is - what exactly is going on and how do I deal with it to achieve the desired result?

My NGINX config, just in case:

server {
    listen $PORT;
    listen  [::]:$PORT;
    server_name sub1.example.com;

    location / {
        root   /usr/share/nginx/html/sub1;
        include  /etc/nginx/mime.types;
        try_files $uri /index.html;
        index  index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/sub1;
    }
}

server {
    listen $PORT;
    listen  [::]:$PORT;
    server_name sub2.example.com;

    location / {
        root   /usr/share/nginx/html/sub2;
        include  /etc/nginx/mime.types;
            try_files $uri /index.html;
            index  index.html;
        }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/sub2;
    }
}

CodePudding user response:

I ended up configuring a load balancer with certificates, the solution was basically to wait until DNS finishes its propagation and make appropriate certificates. I also did not have to map anything in cloud run itself - I pointed both subdomains at my load balancer and configured routing to my app. The error itself was silly enough - my second subdomain lacked an index.html and did not like that, throwing a 500 instead of 404.

  • Related