Customize Server Header using NginX

Whenever you browse a website, you can check the type of web server running by retrieving the response server header. For example, by using this website, http://serverheader.com/header/form.html following server response return for website www.opensource.org :

HTTP/1.1 200 OK
Date: Tue, 13 Mar 2012 03:44:54 GMT
Server: Apache/2.2.22 (FreeBSD) DAV/2 SVN/1.7.3 mod_ssl/2.2.22 OpenSSL/1.0.0g
Set-Cookie: SESSe6df14a6557d381f7820d30df9723b5b=tul1j9kd7mme2r01up3lbq8326; expires=Thu, 05-Apr-2012 07:18:14 GMT; path=/; domain=.opensource.org
Last-Modified: Tue, 13 Mar 2012 03:32:52 GMT
ETag: "ab9ee40bb417fbfde83d38a6d6f4754b"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: must-revalidate
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

From the information we can know that the web server is running on Apache version 2.2.22, run on FreeBSD and have certain module enabled like webdav, svn, mod_ssl and openSSL.

Actually, by using NginX, you can personalize and customize your server header to the name that you want. For example, instead of showing the real web server name which is “nginx“, I want users to see the web server name as “My Web Server“.

I will using following variables to test:

OS: CentOS 6.2 64bit
NginX version: 1.0.13
Web directory: /home/mywebs/public_html
Domain: mywebserver.net

1. Install all dependencies using yum:

$ yum install -y lynx pcre* openssl* zlib*

2. Download and install NginX and its requirement from source:

$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.0.13.tar.gz
$ tar -xzf  nginx-1.0.13.tar.gz
$ cd nginx-*
$ ./configure
$ make
$ make install

3.  Download NginX headers-more module from this website: https://github.com/agentzh/headers-more-nginx-module/tags. I will be using the latest version at this moment which is v0.17rc1 and need to download this module using lynx:

$ cd /usr/local/src
$ lynx https://github.com/agentzh/headers-more-nginx-module/zipball/v0.17rc1

You will need to press “D” > Enter > and select “Save to disk” using arrow on keyboard to download the file > Enter > press ‘q’ to quit and ‘y’ to confirm the action. You should see a new .zip file once completed.

4. We need to unzip, rename and put move the directory to modules directory under NginX directory. Since this directory is not exist in the first place, we need to create it first:

$ mkdir /usr/local/nginx/mod
$ unzip agentzh-headers-more-nginx-module-v0.17rc1-0-g3580526.zip
$ mv agentzh-headers-more-nginx-module-3580526 headers-more
$ mv headers-more /usr/local/nginx/mod

5.  Now we need to recompile NginX with the module. Actually, you can just install it directly with NginX and point it to the module directory separately. But I want NginX and module installation to be organized so I install NginX first and then I recompile it again with this  module.

$ cd /usr/local/src/nginx*
$ ./configure --add-module=/usr/local/nginx/mod/headers-more/
$ make
$ make install

6.  As what variables stated above, I will need to configure NginX to listen to domain mywebserver.net and mapped to /home/myweb/public_html. So I will create a user who own this directory:

$ useradd -m mywebs
$ mkdir /home/mywebs/public_html | mkdir /home/mywebs/logs
$ touch /home/mywebs/logs/access_log | touch /home/mywebs/logs/error_log
$ chown mywebs.mywebs * -R
$ chmod 755 /home/mywebs

7. Make some changes to NginX configuration files which located under /usr/local/nginx/conf/nginx.conf as below:

user  nobody;
worker_processes  1;
 
error_log  logs/error.log  info;
 
events {
    worker_connections  1024;
}
 
http {
    #this is your web server name
    more_set_headers    "Server: My Web Server";
    server_names_hash_max_size 2048;
    include       mime.types;
    default_type  application/octet-stream;
 
    log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
 
    sendfile        on;
    tcp_nopush     on;
 
    keepalive_timeout  10;
 
    gzip  on;
 
 server {
 
  # this is your access logs location
  access_log /home/mywebs/logs/access_log;
  # this is your error logs location
  error_log  /home/mywebs/logs/error_log warn;
  listen    80;
  # change to your domain
  server_name  mywebserver.net www.mywebserver.net;
 
  location / {
   # this is your public_html directory
   root   /home/mywebs/public_html;
   index  index.html index.htm;
  }
 }
}

8. Before we start the NginX, make sure we check the configuration syntax:

$ /usr/local/nginx/sbin/nginx -t

9. If everything is okay, we can start the web server:

$ /usr/local/nginx/sbin/nginx

Done. Now you can check the server header and you will notice that your customize web server name has appeared at the “Server” section. I have checked using Chrome extension called “Domain Details” and my new server header is:

Date: Tue, 13 Mar 2012 04:50:14 GMT
Connection: keep-alive
Content-Length: 23
Last-Modified: Tue, 13 Mar 2012 04:29:33 GMT
Server: My Web Server
Content-Type: text/html
Accept-Ranges: bytes

If I add following line into NginX configuration files:

more_clear_headers   "Content-Type: ";
more_clear_headers   "Accept-Ranges: ";
more_clear_headers   "Content-Length: ";

After NginX restart, the full server header will be returned as below:

Date: Tue, 13 Mar 2012 04:50:14 GMT
Connection: keep-alive
Last-Modified: Tue, 13 Mar 2012 04:29:33 GMT
Server: My Web Server

For more information on the more-headers usage, you can refer to this page:

http://wiki.nginx.org/HttpHeadersMoreModule