Repository: readthedocs/readthedocs.org

Issue:
docs.opnfv.org is 'Not Found.'
## Details

The entire documentation site for OPNFV is missing. The most recent builds have succeeded, and as far as I know DNS hasn't changed recently.

* Read the Docs project URL: opnfvdocsdemo.readthedocs.io
* Build URL (if applicable): https://readthedocs.org/projects/opnfvdocsdemo/builds/
* Read the Docs username (if applicable):

## Expected Result

Going to https://docs.opnfv.org/ returns the documentation site.

## Actual Result
```
curl -i -L https://opnfvdocsdemo.readthedocs.io/
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: http://docs.opnfv.org/en/stable-hunter/
Server: nginx
X-Frame-Options: DENY
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
X-Served: Django-Proxito
X-Deity: web04
Strict-Transport-Security: max-age=31536000; includeSubDomains
Date: Wed, 29 Jan 2020 23:13:29 GMT
Content-Length: 0

HTTP/1.1 301 Moved Permanently
Server: CloudFront
Date: Wed, 29 Jan 2020 23:13:29 GMT
Content-Type: text/html
Content-Length: 183
Connection: keep-alive
Location: https://docs.opnfv.org/en/stable-hunter/
X-Cache: Redirect from cloudfront
Via: 1.1 5ab5dc09da67e3ea794ec8a82992cc89.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: HIO50-C1
X-Amz-Cf-Id: 0_rJ9aN8nFAFm6M9VPcWPWHa7B8QOaSW1_Y3Llttz31ZTaK03cTaYQ==

HTTP/2 404 
content-type: text/html; charset=utf-8
content-length: 10
server: nginx
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-served: Proxito-404
x-deity: web03
strict-transport-security: max-age=0
date: Wed, 29 Jan 2020 23:13:30 GMT
x-cache: Miss from cloudfront
via: 1.1 1b0911478686968732f973d6e5e31d11.cloudfront.net (CloudFront)
x-amz-cf-pop: HIO50-C1
x-amz-cf-id: sRmKIeU3LyXtKb93316GUwkxqiChktuq227k3nhDcOPqU-78E7JFTA==

Not Found.
```



Discussion:
Seems like you're serving a CDN in front of this? What is the request you're sending to the backend? 
Hitting our servers directly with the request works fine:

```
-> curl -IL -H "Host: docs.opnfv.org" 104.208.221.96/en/stable-hunter/
HTTP/1.1 200 OK
```
I can't debug this because your CDN is caching 404 statues. 
We did just deploy some server changes on our side, but I can't really debug it without knowing more information about what the request is from the CDN, or turning off the 404 caching. 
I'd recommend pointing your domain at our servers to resolve this, and then we can try to fix it another way. 
@bramwelt I've dumped traffic on our web servers, and I see you're sending this header:

```
X-RTD-SLUG: opnfv
```

This is causing the docs to not be found. If you remove this header, it should work. 

Resolving patch:
diff --git a/readthedocs/proxito/middleware.py b/readthedocs/proxito/middleware.py
--- a/readthedocs/proxito/middleware.py
+++ b/readthedocs/proxito/middleware.py
@@ -11,7 +11,7 @@
 from django.shortcuts import render
 from django.utils.deprecation import MiddlewareMixin
 
-from readthedocs.projects.models import Domain
+from readthedocs.projects.models import Domain, Project
 
 log = logging.getLogger(__name__)  # noqa
 
@@ -40,39 +40,38 @@ def map_host_to_project_slug(request):
     # Explicit Project slug being passed in
     if 'HTTP_X_RTD_SLUG' in request.META:
         project_slug = request.META['HTTP_X_RTD_SLUG'].lower()
-        request.rtdheader = True
-        log.info('Setting project based on X_RTD_SLUG header: %s' % project_slug)
+        if Project.objects.filter(slug=project_slug).exists():
+            request.rtdheader = True
+            log.info('Setting project based on X_RTD_SLUG header: %s', project_slug)
+            return project_slug
 
-    elif public_domain in host or host == 'proxito':
+    if public_domain in host or host == 'proxito':
         # Serve from the PUBLIC_DOMAIN, ensuring it looks like `foo.PUBLIC_DOMAIN`
         if public_domain_parts == host_parts[1:]:
             project_slug = host_parts[0]
             request.subdomain = True
             log.debug('Proxito Public Domain: host=%s', host)
-        else:
-            # TODO: This can catch some possibly valid domains (docs.readthedocs.io.com) for example
-            # But these feel like they might be phishing, etc. so let's block them for now.
-            log.warning('Weird variation on our hostname: host=%s', host)
-            return render(
-                request, 'core/dns-404.html', context={'host': host}, status=400
-            )
+            return project_slug
+        # TODO: This can catch some possibly valid domains (docs.readthedocs.io.com) for example
+        # But these feel like they might be phishing, etc. so let's block them for now.
+        log.warning('Weird variation on our hostname: host=%s', host)
+        return render(
+            request, 'core/dns-404.html', context={'host': host}, status=400
+        )
 
     # Serve CNAMEs
-    else:
-        domain = Domain.objects.filter(domain=host).first()
-        if domain:
-            project_slug = domain.project.slug
-            request.cname = True
-            log.debug('Proxito CNAME: host=%s', host)
-        else:
-            # Some person is CNAMEing to us without configuring a domain - 404.
-            log.debug('CNAME 404: host=%s', host)
-            return render(
-                request, 'core/dns-404.html', context={'host': host}, status=404
-            )
-
-    log.debug('Proxito Project: slug=%s', project_slug)
-    return project_slug
+    domain = Domain.objects.filter(domain=host).first()
+    if domain:
+        project_slug = domain.project.slug
+        request.cname = True
+        log.debug('Proxito CNAME: host=%s', host)
+        return project_slug
+
+    # Some person is CNAMEing to us without configuring a domain - 404.
+    log.debug('CNAME 404: host=%s', host)
+    return render(
+        request, 'core/dns-404.html', context={'host': host}, status=404
+    )
 
 
 class ProxitoMiddleware(MiddlewareMixin):
@@ -91,6 +90,8 @@ def process_request(self, request):  # noqa
         if hasattr(ret, 'status_code'):
             return ret
 
+        log.debug('Proxito Project: slug=%s', ret)
+
         # Otherwise set the slug on the request
         request.host_project_slug = request.slug = ret
 


Test patch: