diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -399,6 +399,12 @@ class hgweb(object):
                 tag = 'W/"%d"' % self.mtime
                 if req.headers.get('If-None-Match') == tag:
                     res.status = '304 Not Modified'
+                    # Content-Type may be defined globally. It isn't valid on a
+                    # 304, so discard it.
+                    try:
+                        del res.headers[b'Content-Type']
+                    except KeyError:
+                        pass
                     # Response body not allowed on 304.
                     res.setbodybytes('')
                     return res.sendresponse()
diff --git a/tests/test-hgweb.t b/tests/test-hgweb.t
--- a/tests/test-hgweb.t
+++ b/tests/test-hgweb.t
@@ -890,9 +890,7 @@ HTTP 304 works with hgwebdir (issue5844)
   200 Script output follows
   content-length: 2677
   content-type: text/css
-  500 Internal Server Error
-  transfer-encoding: chunked
-  [1]
+  304 Not Modified
 
   $ killdaemons.py