docker - 如何删除 nginx docker 容器中的服务器 header ?

标签 docker nginx docker-compose

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

错误 :

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

docker 撰写文件:
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


默认.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;
    }
}

最佳答案

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

FROM nginx:1.15.6
RUN apt-get update && apt-get install -y nginx-extras
希望这可以帮助。

关于docker - 如何删除 nginx docker 容器中的服务器 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55722738/

相关文章:

ssl - 为间歇性服务配置 proxy_pass

docker - docker-compose up -d尝试在docker-compose.yml文件中设置mem_limit时给出 “OCI runtime create failed: wrong rlimit value”

mysql - docker: 无法映射 "/var/run/mysqld/mysqld.sock"?

docker - docker-compose不将文件复制到tmp文件夹中以创建容器

css - NGINX 无法加载 css

authentication - 使用 Nginx 进行 ElasticSearch 集群身份验证

mysql - Flask API 在本地运行 w/docker-compose,但没有响应 (windows)

laravel - 在Docker容器中运行Laravel应用,但autoload.php文件不存在

wordpress - Lando 无法从我系统上的非 Lando 应用程序访问 URL

docker - 在构建过程中在Docker镜像中移动文件