Answers.Body: <p>As I previously answered, and as suggested by Brett Moore, this was indeed a database connectivity issue.</p>
  <p>The DB timeout was being swallowed and logged to the filesystem, and the less helpful error that the primary site couldn't be found was surfaced to the UI.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T22:36:32.847Z
Answers.LastActivityDate: 2024-06-20T22:36:32.847Z
Answers.UserDisplayName: Zhaph - Ben Duguid
Body: <p>I've been asked to deploy a Craft instance via an image in Azure Kubernetes Service, and I'm having a bit of an issue getting the site up and running - we're seeing a &quot;Site Not Found&quot; <code>SiteNotFoundException</code> when attempting to access the site.</p>
  <p>I've set an environment variable <code>PRIMARY_SITE_URL</code> set to the required domain, and we're setting the <code>@web</code> alias based on that:</p>
  <pre class="lang-php prettyprint-override"><code>'aliases' =&gt; [
    '@web' =&gt; rtrim(APP::env('PRIMARY_SITE_URL'), '/'),
    '@webroot' =&gt; dirname(__DIR__) . '/web',
  ]
  </code></pre>
  <p>However we're still seeing this issue - I assume this is because the setup we have includes a load balancer that's handling the initial request for us, and instead mapping the domain into the <code>x-forwarded-host</code> header of the request.</p>
  <p>Checking the request headers with a small test service, the <code>Host</code> is the internal IP address (and port) of the AKS pod, and the originally requested domain (that's set in both <code>PRIMARY_SITE_URL</code> and <code>DEFAULT_SITE_URL</code>) is only on the <code>x-forwarded-host</code> header.</p>
  <p>I assume I'm getting the SiteNotFound exception because the value on the host (IP and Port) doesn't match the value set in the Environment variable? Is there any way to get Craft to read the <code>x-forwarded-host</code> value when trying to match the request.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-06-11T17:15:22.793Z
Comments.Text: Looks like this is more a database connectivity error, rather than a `PRIMARY_SITE_URL` issue - working with the hosting team, logs are showing DB timeouts rather than anything else.
Comments.UserDisplayName: Zhaph - Ben Duguid
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-06-11T18:00:42.677Z
Comments.Text: Can you connect to the database server within the container? This error can be the result of numerous things but it is most likely a database connectivity issue.
Comments.UserDisplayName: Brett Moore
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-06-20T22:32:36.397Z
Comments.Text: Sorry @BrettMoore, at the point I raised this and my answer (that's now a comment) the application couldn't reach the database at all, no. But the DB timeout was swallowed and I just had the "Primary Site Not Found" error raised instead.
Comments.UserDisplayName: Zhaph - Ben Duguid
Tags: <craft4><load-balancer>
Title: Site not found error when running Craft CMS behind a loadbalancer in a container
ThreadText: # Site not found error when running Craft CMS behind a loadbalancer in a container
  
  (Asked by: username_0 on 2024-06-04)
  
  I've been asked to deploy a Craft instance via an image in Azure Kubernetes Service, and I'm having a bit of an issue getting the site up and running - we're seeing a "Site Not Found" `SiteNotFoundException` when attempting to access the site.
  
  I've set an environment variable `PRIMARY_SITE_URL` set to the required domain, and we're setting the `@web` alias based on that:
  
  ```php
  'aliases' => [
    '@web' => rtrim(APP::env('PRIMARY_SITE_URL'), '/'),
    '@webroot' => dirname(__DIR__) . '/web',
  ]
  
  ```
  
  However we're still seeing this issue - I assume this is because the setup we have includes a load balancer that's handling the initial request for us, and instead mapping the domain into the `x-forwarded-host` header of the request.
  
  Checking the request headers with a small test service, the `Host` is the internal IP address (and port) of the AKS pod, and the originally requested domain (that's set in both `PRIMARY_SITE_URL` and `DEFAULT_SITE_URL`) is only on the `x-forwarded-host` header.
  
  I assume I'm getting the SiteNotFound exception because the value on the host (IP and Port) doesn't match the value set in the Environment variable? Is there any way to get Craft to read the `x-forwarded-host` value when trying to match the request.
  
  **Tags:** craft4, load-balancer
  
  > **Comments:**
  > **username_0**: Looks like this is more a database connectivity error, rather than a `PRIMARY_SITE_URL` issue - working with the hosting team, logs are showing DB timeouts rather than anything else.  
  > **username_1**: Can you connect to the database server within the container? This error can be the result of numerous things but it is most likely a database connectivity issue.  
  > **username_0**: Sorry @username_1, at the point I raised this and my answer (that's now a comment) the application couldn't reach the database at all, no. But the DB timeout was swallowed and I just had the "Primary Site Not Found" error raised instead.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-06-20. Score: 0 | Accepted answer
  
  As I previously answered, and as suggested by username_1 username_1, this was indeed a database connectivity issue.
  
  The DB timeout was being swallowed and logged to the filesystem, and the less helpful error that the primary site couldn't be found was surfaced to the UI.
  
  
  ---
  
  