2016-02-19 46 views
1

我已經啓用在我的Django部署的設置SECURE_SSL_REDIRECT,所以現在這些報頭越來越發送到客戶端:如何將SECURE_SSL_REDIRECT與Cache-Control標頭一起使用?

< HTTP/1.1 301 MOVED PERMANENTLY 
< Date: Fri, 19 Feb 2016 15:57:50 GMT 
< Server: Apache/2.2.15 (Red Hat) 
< Location: https://www.example.com/ 
< Content-Length: 0 
< Content-Type: text/html; charset=utf-8 

與301的主要缺點重定向是,他們往往被緩存在很長時間由瀏覽器,所以我很想添加一個Cache-Control: max-age=604800, must-revalidate標題。最好是,我想要一種不涉及重新實施SECURE_SSL_REDIRECT的方式。

回答

1

您可以嘗試覆蓋Django的SecurityMiddleware以添加您需要的http標頭。下面是一個全面實施的中間件:

class CustomSecurityMiddleware(SecurityMiddleware): 

    def process_request(self, request): 
     response = super(CustomSecurityMiddleware, self).process_request(request) 

     # SecurityMiddleware returns an HttpResponsePermanentRedirect only if 
     # the request should be redirected 
     if response is not None: 
      response['Cache-Control'] = 'max-age=604800, must-revalidate' 
      return response 

此實現保留一切Django的SecurityMiddleware確實已經,同時加入您需要的自定義HTTP標頭。

定製中間件應該替換settings.MIDDLEWARE_CLASSES中的SecurityMiddleware。