how to change nginx server string?

by on Jul.06, 2010, under bash, code, solution

If you want to hide web server string which is shown each time you perform request, this might be howto for you ;)
Usual nginx response looks like that:

HTTP/1.1 200 OK
Date: Tue, 06 Jul 2010 08:14:19 GMT
Server: nginx/0.7.67

To get rid of it you need to get source code, change it and recompile. Files which requre some small modifications are located at:

  • src/core/nginx.h
  • src/http/ngx_http_header_filter_module.c
  • conf/fastcgi_params

I’ve made bash script to automate whole process. To use it, just save source to some file and launch ./script.sh <directory_of_nginx>. Please have look at it, test and post comment in case it is not working. Enjoy!

#!/bin/bash

DIR=${1}
SERVERNAME=Paranoid
SERVERVERSION=0.4.2

if [[ $# < 1 ]]
then
    echo "`basename ${0}` {dir}"
    echo -e "\n\tExample: `basename ${0}` nginx-0.7.64"
    exit 1
fi

if [[ -e ${DIR}/src/core/nginx.h ]]
then
    sed -i "/#define NGINX_VERSION/ s,\"[^\"]*\",${SERVERVERSION}," ${DIR}/src/core/nginx.h
    sed -i "/#define NGINX_VER/ s,nginx,${SERVERNAME}," ${DIR}/src/core/nginx.h
    sed -i "/#define NGINX_VAR/ s,\"NGINX\",\"`echo ${SERVERNAME}| tr \"[a-z]\" \"[A-Z]\"`\"," ${DIR}/src/core/nginx.h
else
    echo "Can not find ${DIR}/src/core/nginx.h"
fi

if [[ -e ${DIR}/src/http/ngx_http_header_filter_module.c ]]
then
    sed -i "s,Server: nginx,Server: ${SERVERNAME}," ${DIR}/src/http/ngx_http_header_filter_module.c
else
    echo "Can not find ${DIR}/src/http/ngx_http_header_filter_module.c"
fi

if [[ -e ${DIR}/conf/fastcgi_params ]]
then
    sed -i "s,nginx/$nginx_version,${SERVERNAME}/$nginx_version," ${DIR}/conf/fastcgi_params
else
    echo "Can not find ${DIR}/conf/fastcgi_params"
fi
:, , ,

1 Trackback or Pingback for this entry

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...