vatandoost
vatandoost

Reputation: 3737

How can I remove server header in nginx docker container?

I install nginx:1.15.6 container by docker-compose file and I want to remove Server header from all nginx responses, by the search I found bellow way set "more_set_headers 'Server: custom';" in nginx configuration but there is an error to respond . How can I remove server header in nginx docker? I think I should install "headers-more-nginx-module-0.33" module but I dont know how can i install it :(

error :

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

docker-compose file:

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;
    }
}

Upvotes: 8

Views: 21091

Answers (2)

cdreek
cdreek

Reputation: 1

Here a solution that worked for me. I took out the version numbers and stripped down the config with only what is really needed.

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 {
    ...
}

Upvotes: 0

Illia
Illia

Reputation: 369

more_set_headers is a part of the headers_more module, so it needs an additional nginx package to work properly. nginx-extras could be installed while building docker image for nginx container:

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

Hope this helps.

Upvotes: 16

Related Questions