24

我想屏蔽版本或完全删除标题。

4

7 回答 7

28

要更改 'Server:' http 标头,请在 conf.py 文件中:

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

并使用沿线的调用gunicorn -c conf.py wsgi:app

要完全删除标头,您可以通过将其 http 响应类替换为过滤掉标头的子类来对 gunicorn 进行猴子补丁。这可能是无害的,但可能不推荐。将以下内容放入 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

用 gunicorn 18 测试

于 2014-01-22T21:37:56.473 回答
4

对于较新的版本 (20.0.4):gunicorn.conf.py在运行gunicorn命令的目录中创建一个包含以下内容的文件:

import gunicorn
gunicorn.SERVER_SOFTWARE = 'My WebServer'
于 2021-01-29T05:30:05.863 回答
3

这里没有写清楚,所以我要确认最新版本的 Gunicorn (20.1.x) 最简单的方法是将以下行添加到配置文件中:

import gunicorn 
gunicorn.SERVER = 'undisclosed'
于 2021-07-27T16:02:39.020 回答
2

将其更改为独特的东西比将其删除要好。您不想冒险,例如,蜘蛛认为您不合规。将其更改为您不使用的软件的名称可能会导致类似的问题。使其独一无二将防止做出相同的假设。我推荐这样的东西:

import gunicorn
gunicorn.SERVER_SOFTWARE = 'intentionally-undisclosed-gensym384763'
于 2019-05-21T16:42:20.410 回答
1

您可以编辑 __init__.py 以将 SERVER_SOFTWARE 设置为您想要的任何内容。但是我真的很想用一个标志来禁用它,所以我升级时不需要重新应用补丁。

于 2013-07-26T20:05:57.533 回答
1

我的模拟补丁免费解决方案涉及包装 default_headers 方法:

import gunicorn.http.wsgi
from six import wraps


def wrap_default_headers(func):
    @wraps(func)
    def default_headers(*args, **kwargs):
        return [header for header in func(*args, **kwargs) if not header.startswith('Server: ')]
    return default_headers


gunicorn.http.wsgi.Response.default_headers = wrap_default_headers(gunicorn.http.wsgi.Response.default_headers)
于 2018-07-24T18:14:20.770 回答
0

这并不能直接回答这个问题,但也可以解决这个问题,而无需猴子修补 gunicorn。

如果您在反向代理后面使用 gunicorn,通常情况下,您可以在来自后端的下游响应标头中设置、添加、删除或执行替换。在我们的例子中,Server标题。

我想每个网络服务器都应该有一个等效的功能。


例如,在Caddy 2(目前处于测试阶段)中,它会很简单:

https://localhost {
    reverse_proxy unix//tmp/foo.sock {
        header_down Server intentionally-undisclosed-12345678
    }
}

为了完整起见,即使在手动 http-> https 重定向过程中,我仍然添加了一个最小(但完全有效)Caddyfile来处理服务器标头修改(如果您不覆盖它,Caddy 2 会自动执行它),这可能有点难以理解正确输出。

http://localhost {
    # Fact: the `header` directive has less priority than `redir` (which means
    # it's evaluated later), so the header wouldn't be changed (and Caddy would
    # shown instead of the faked value).
    #
    # To override the directive ordering only for this server, instead of
    # change the "order" option globally, put the configuration inside a
    # route directive.
    # ref.
    #   https://caddyserver.com/docs/caddyfile/options
    #   https://caddyserver.com/docs/caddyfile/directives/route
    #   https://caddyserver.com/docs/caddyfile/directives#directive-order
    route {
        header Server intentionally-undisclosed-12345678
        redir https://{host}{uri}
    }
}

https://localhost {
    reverse_proxy unix//tmp/foo.sock {
        header_down Server intentionally-undisclosed-12345678
    }
}

要检查它是否有效,只需使用curlascurl --insecure -I http://localhostcurl --insecure -I http://localhost(--insecure,因为 localhost 证书会自动生成为自签名)。

它的设置非常简单,您也可以考虑在开发中使用它(使用gunicorn --reload),尤其是当它类似于您的登台/生产环境时。

于 2020-03-22T10:10:54.843 回答