如何删除Nginx Docker容器中的服务器头?

时间:2019-04-17 08:02:59

标签: docker nginx docker-compose

我通过docker-compose文件安装了nginx:1.15.6容器,并且我想通过下面的搜索方法从所有nginx响应中删除Server标头 设置“ more_set_headers'服务器:自定义';”在nginx配置中,但是有一个错误要响应。如何在Nginx泊坞窗中删除服务器标头?我认为我应该安装“ 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;
    }
}

1 个答案:

答案 0 :(得分:2)

more_set_headersheaders_more模块的一部分,因此它需要一个附加的nginx软件包才能正常工作。在为nginx容器构建docker映像时,可以安装nginx-extras

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

希望这会有所帮助。