如何从 EB 响应中删除标头

Posted

技术标签:

【中文标题】如何从 EB 响应中删除标头【英文标题】:How to remove header from EB response 【发布时间】:2018-06-11 10:19:45 【问题描述】:

我正在尝试从 EB 服务中删除响应标头。

EB 配置: 64bit Amazon Linux 2017.03 v2.7.1 running Docker 17.03.1-ce

运行:FROM python:3.6.1

也使用flask

我将以下代码添加到flask

def after_request_callback(response):
    response.headers["server"] = "SomeNonFingerprintValue"
    return response


def create_application():
    application = Flask(__name__)
    application.after_request(after_request_callback)

当我在本地运行时这工作正常,但当我部署到 EB 时,我不断获得指纹值:server → nginx/1.10.3

知道如何删除\修改已部署服务的此值吗?

【问题讨论】:

可能 nginx 正在覆盖服务器标头。如果您可以修改 nginx 配置 this answer 可能会有所帮助。 是的,看到了,不确定它是否可能以及如何为 EB...问题的一部分 【参考方案1】:
    在项目的根目录中创建一个“.ebextensions”文件夹 添加一个名为“nginx.config”的文件(可以调用任何名称,只要它以 .config 结尾即可)

    将以下内容放入文件中(注意 proxy_set_header 行):

    files:
    "/tmp/000_my_nginx_config.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs 
          server 127.0.0.1:8081;
          keepalive 256;
      
    
      log_format combined_no_query '$remote_addr - $remote_user [$time_local] '
        '"$uri" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent"';
    
      server 
        listen 8080;
    
        if ($time_iso8601 ~ "^(\d4)-(\d2)-(\d2)T(\d2)") 
            set $year $1;
            set $month $2;
            set $day $3;
            set $hour $4;
        
    
        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
        access_log  /var/log/nginx/access.log  combined_no_query;
    
        location / 
            proxy_pass  http://nodejs;
            proxy_set_header   Connection "";
            proxy_http_version 1.1;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For 
            proxy_set_header        Server NonFingerprint;
        
    
        gzip on;
        gzip_comp_level 4;
        gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
      
    
      "/tmp/45_replace_config.sh":
    mode: "000644"
    owner: root
    group: root
    content: |
      #! /bin/bash
      cp -rfv /tmp/000_my_nginx_config.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
    
    container_commands:
       00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_replace_config.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
      01_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_replace_config.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
      02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_replace_config.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_replace_config.sh
      03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_replace_config.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_replace_config.sh
    

【讨论】:

以上是关于如何从 EB 响应中删除标头的主要内容,如果未能解决你的问题,请参考以下文章

如何从响应标头中的内容类型中删除 charset=utf-8

如何从放心的响应中删除xml标头

如何删除 HTTP 响应标头?

如何从 fetch 中获取响应的标头

如何从 Angular 2 响应中获取所有标头?

如何将标头从请求传递到集成响应?