如何从EB回复中删除标题

时间:2018-01-01 09:47:35

标签: python amazon-web-services docker flask elastic-beanstalk

我试图从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

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

1 个答案:

答案 0 :(得分:1)

  1. 创建' .ebextensions'项目根目录中的文件夹
  2. 添加名为' nginx.config'的文件(可以被称为任何东西,只要它以.config结尾)
  3. 将以下内容放入文件中(注意 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 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
            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