How to prevent Gunicorn from returning a 'Server' http header?

后端 未结 6 1099
走了就别回头了
走了就别回头了 2021-02-05 06:51

I would like to mask the version or remove the header altogether.

6条回答
  •  失恋的感觉
    2021-02-05 07:32

    To change the 'Server:' http header, in your conf.py file:

     import gunicorn
     gunicorn.SERVER_SOFTWARE = 'Microsoft-IIS/6.0'
    

    And use an invocation along the lines of gunicorn -c conf.py wsgi:app

    To remove the header altogether, you can monkey-patch gunicorn by replacing its http response class with a subclass that filters out the header. This might be harmless, but is probably not recommended. Put the following in conf.py:

    from gunicorn.http import wsgi
    
    class Response(wsgi.Response):
        def default_headers(self, *args, **kwargs):
            headers = super(Response, self).default_headers(*args, **kwargs)
            return [h for h in headers if not h.startswith('Server:')]
    
    wsgi.Response = Response
    

    Tested with gunicorn 18

提交回复
热议问题