Dave Perrett

Customize the Nginx Server Header

linux, nginx, programming, web

Unfortunately the only way to change the server header in nginx is to actually recompile it from source. Luckily this is quite easy.

First, download the latest version of nginx (0.7.61 at the time of writing) :

1
2
3
> wget http://sysoev.ru/nginx/nginx-0.7.61.tar.gz
> tar xvzf nginx-0.7.61.tar.gz
> cd nginx-0.7.61

Changing the server string

The server header is in a file called ngx_http_header_filter_module.c :

1
> vi src/http/ngx_http_header_filter_module.c

Edit the following line (currently line 48 in version 0.7.61) :

1
static char ngx_http_server_string[] = "Server: nginx" CRLF;

… and change the nginx in ‘Server: nginx’ to whatever you want:

1
static char ngx_http_server_string[] = "Server: recurser" CRLF;

Next, edit src/core/nginx.h :

1
> vi src/core/nginx.h

Edit the following line (currently line 13 in version 0.7.61) :

1
#define NGINX_VER          "nginx/" NGINX_VERSION

… and change the server string to whatever you want:

1
#define NGINX_VER          "recurser/" NGINX_VERSION

Changing the version number

You might also want to change the version number to confuse attackers, or just for fun :) Edit src/core/nginx.h :

1
> vi src/core/nginx.h

Edit the following line (currently line 12 in version 0.7.61) :

1
#define NGINX_VERSION      "0.7.61"

… and change the version to whatever you want:

1
#define NGINX_VERSION      "1.0"

You can stop nginx from displaying the version entirely if you want, by including the server_tokens directive in your nginx.conf :

1
server_tokens off;

Compiling

You can set the prefix as appropriate - I keep it in /opt/ on Ubuntu.

1
2
3
> ./configure --prefix=/opt/nginx-0.7.61 --with-http_ssl_module
> make
> sudo make install

After this, use .bashrc or similar to add /opt/nginx-0.7.61/sbin to your path, then you can start your new nginx install using :

1
> sudo ngingx

To stop it, use ‘-s’ to send the stop signal :

1
> sudo nginx -s stop

Results

If you check the reponse headers, you should see your custom server string :

1
2
3
4
5
6
7
8
9
10
11
12
Server: recurser/1.0, recurser
Date: Tue, 11 Aug 2009 02:47:31 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.2.4-2ubuntu5.6
X-Pingback: http://recurser.com/xmlrpc.php
Expires: Tue, 18 Aug 2009 02:47:31 GMT
Last-Modified: Tue, 11 Aug 2009 02:47:31 GMT
Cache-Control: max-age=604800
Pragma: no-cache
Content-Encoding: gzip