【问题标题】:How can I remove server header in nginx docker container?如何删除 nginx docker 容器中的服务器标头?
【发布时间】:2019-04-17 08:02:58
【问题描述】:

我通过 docker-compose 文件安装 nginx:1.15.6 容器,我想从所有 nginx 响应中删除 Server 标头,通过搜索我发现如下方式 设置“more_set_headers '服务器:自定义';”在 nginx 配置中但有一个错误响应。如何删除 nginx docker 中的服务器标头?我想我应该安装“headers-more-nginx-module-0.33”模块,但我不知道如何安装它:(

错误:

[emerg] 1#1: unknown directive "more_set_headers" in /etc/nginx/conf.d/default.conf:22

docker-compose 文件:

version: '3'

services:
  web:
    build:
      context: nginx
    container_name: r_nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./code:/code
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/ssl:/etc/ssl
      - ./nginx/logs:/var/log/nginx
    restart: always
    depends_on:
      - php
  php:
    build: phpfpm
    container_name: r_php
    restart: always
    volumes:
      - ./phpfpm/raya.ini:/opt/bitnami/php/etc/conf.d/custom.ini
      - ./code:/code

default.conf:

server_tokens off;

server {
    listen 80;
    listen 443 ssl;
    ssl on;
    ssl_protocols     TLSv1.1  TLSv1.2;
    ssl_certificate /etc/ssl/cert_chain.crt;
    ssl_certificate_key /etc/ssl/private.key;

    index index.php index.html;
    #server_name php-docker.local;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    error_page 404 403 402 401 422 = /errors/error.php;
    error_page 500 501 502 503 504 = /errors/error.php;

#   bellow line get error :
#   more_set_headers "Server: custom";

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }


    location ~ /assets/ {
    }

    location / {
      rewrite ^(.*)$ /index.php;
    }
}

【问题讨论】:

  • 你能分享一下你的nginx的Dockerfile和nginx的配置吗?
  • docker 文件仅用于测试,只有一行:FROM nginx:1.15.6

标签: docker nginx docker-compose


【解决方案1】:

more_set_headersheaders_more 模块的一部分,所以它需要一个额外的 nginx 包才能正常工作。 nginx-extras 可以在为 nginx 容器构建 docker 镜像时安装:

FROM nginx:1.15.6
RUN apt-get update && apt-get install -y nginx-extras

希望这会有所帮助。

【讨论】:

  • 谢谢它的工作,但我需要手动安装模块,你能帮我从包中安装模块吗? “headers-more-nginx-module-0.33.tar.gz”
  • 如果我理解正确,您可以使用Dockerfile 中的ADD instruction 自动将本地存档作为目录解压到容器中,然后按照模块安装的instructions(使用RUN指令)。
  • nginx alpine 没有 apt-get,我正在尝试使用 apk,但无法安装 nginx-extras。请帮忙..
  • @amesh 我相信 alpine 中的模块被称为 nginx-mod-http-headers-more 我知道这已经有一段时间了,但我们在团队中偶然发现了同样的问题,希望它仍然有帮助:)
  • 嘿开发者,你能分享你的 dockerfile 让你安装这个吗?我在 dockerfie 中运行 apt-get 命令时出错:)
【解决方案2】:

这里有一个对我有用的解决方案。我取出了版本号,只删除了真正需要的配置。

Dockerfile

FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
RUN apt-get install libnginx-mod-http-headers-more-filter

nginx.conf

server_tokens off; # hides version on 404 or 500 pages
more_clear_headers 'Server'; # removes Server header from response headers

server {
    ...
}

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2020-11-05
    • 2022-10-06
    • 2016-11-11
    • 1970-01-01
    • 2017-07-11
    • 2021-08-04
    • 2016-05-23
    相关资源
    最近更新 更多