Skip to content

Serve custom 404 pages from Django #5641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
humitos opened this issue Apr 29, 2019 · 3 comments · Fixed by #5644
Closed

Serve custom 404 pages from Django #5641

humitos opened this issue Apr 29, 2019 · 3 comments · Fixed by #5644
Labels
Accepted Accepted issue on our roadmap Bug A bug Good First Issue Good for new contributors

Comments

@humitos
Copy link
Member

humitos commented Apr 29, 2019

Currently, we are serving the 404.html from NGINX returning a Response that includes the X-Accel-Redirect header:

https://github.com/rtfd/readthedocs.org/blob/537f7b8d2e8d81f8243d1d4a562968e472bc6337/readthedocs/core/views/__init__.py#L208-L210

We found that even marking the response.status_code = 404, the response is 200.

$ curl -IL https://docs.readthedocs.io/notfound
HTTP/1.1 200 OK
Content-Length: 14167
Content-Type: text/html
Last-Modified: Mon, 22 Apr 2019 15:30:57 GMT
Accept-Ranges: bytes
ETag: "5cbdde31-3757"
Vary: Accept-Encoding
Server: nginx
X-Served: Nginx-Sendfile
X-Deity: web01
Strict-Transport-Security: max-age=1800; includeSubDomains
Date: Mon, 29 Apr 2019 07:59:21 GMT

To fix this, we have to serve this file directly from Django instead of using this header.

@humitos humitos added Good First Issue Good for new contributors Bug A bug Accepted Accepted issue on our roadmap labels Apr 29, 2019
@humitos humitos added this to the File serving improvements milestone Apr 29, 2019
@saadmk11
Copy link
Member

Working on it!

@saadmk11
Copy link
Member

I looked into it. I'm a bit confused. can you tell me why this test passes:
self.assertEqual(response.status_code, 404)
https://github.com/rtfd/readthedocs.org/blob/537f7b8d2e8d81f8243d1d4a562968e472bc6337/readthedocs/rtd_tests/tests/test_doc_serving.py#L206-L224

and do we need to use the serve() or render() instead of _serve_file() method from django to serve this?

@humitos
Copy link
Member Author

humitos commented Apr 29, 2019

The test passes because Django is returning a 404 properly. Although, in production, that response is passed to NGINX who looks for the X-Accel-Redirect header and serve that file directly from disk. At this point, because NGINX found that file and was able to serve it properly, it adds the 200 in the response.

As we don't use NGINX at all for our tests, it succees.

and do we need to use the serve() or render() instead of _serve_file() method from django to serve this?

I suppose we need to use serve since it's a regular file, not a template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Accepted issue on our roadmap Bug A bug Good First Issue Good for new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants