November 8, 2016

にんじんえっくす

今日とんでもないパワーワードの知見を得たので共有したい。

なんと言うことだろうか。ニンジンエックス。もうこれから nginx がずっとニンジンエックスに見える呪いがかかった。

私は呪いをより完成された物にするため nginx に以下の細工を入れることにした。 ターゲットバージョンは nginx-1.11.5-1.el7.ngx.src.rpm とした。

SRPM をインストールしたら以下内容で nginx-ninginx.patch を作る。

diff -aurN nginx-1.11.5.orig/src/core/nginx.h nginx-1.11.5/src/core/nginx.h
--- nginx-1.11.5.orig/src/core/nginx.h	2016-11-08 12:32:52.621746959 +0000
+++ nginx-1.11.5/src/core/nginx.h	2016-11-08 12:35:51.327414716 +0000
@@ -11,7 +11,7 @@
 
 #define nginx_version      1011005
 #define NGINX_VERSION      "1.11.5"
-#define NGINX_VER          "nginx/" NGINX_VERSION
+#define NGINX_VER          "ninginx/" NGINX_VERSION
 
 #ifdef NGX_BUILD
 #define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
@@ -19,7 +19,7 @@
 #define NGINX_VER_BUILD    NGINX_VER
 #endif
 
-#define NGINX_VAR          "NGINX"
+#define NGINX_VAR          "NINGINX"
 #define NGX_OLDPID_EXT     ".oldbin"
 
 
diff -aurN nginx-1.11.5.orig/src/http/ngx_http_header_filter_module.c nginx-1.11.5/src/http/ngx_http_header_filter_module.c
--- nginx-1.11.5.orig/src/http/ngx_http_header_filter_module.c	2016-11-08 12:32:52.625746953 +0000
+++ nginx-1.11.5/src/http/ngx_http_header_filter_module.c	2016-11-08 12:36:39.691323117 +0000
@@ -46,7 +46,7 @@
 };
 
 
-static char ngx_http_server_string[] = "Server: nginx" CRLF;
+static char ngx_http_server_string[] = "Server: ninginx" CRLF;
 static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
 
 

server_tokens やエラーした時に出る表記を根こそぎ NINGINX に変える。根絶やしである。実行バイナリ名も変えようかと思ったが、それはそれで手間だったのでそのままにした。

そして nginx.spec に足したパッチを組み込んでリビルドする。

diff -aurN nginx.spec nginx.spec_ningin
--- nginx.spec	2016-10-11 15:37:24.000000000 +0000
+++ nginx.spec_nignin	2016-11-08 12:43:49.492124003 +0000
@@ -85,6 +85,8 @@
 
 Provides: webserver
 
+Patch0: nginx-ninginx.patch
+
 %description
 nginx [engine x] is an HTTP and reverse proxy server, as well as
 a mail proxy server.
@@ -100,6 +102,7 @@
     -e 's|%%PROVIDES%%|nginx|g' < %{SOURCE2} > nginx.init
 sed -e 's|%%DEFAULTSTART%%||g' -e 's|%%DEFAULTSTOP%%|0 1 2 3 4 5 6|g' \
     -e 's|%%PROVIDES%%|nginx-debug|g' < %{SOURCE2} > nginx-debug.init
+%patch0 -p1
 
 %build
 ./configure %{BASE_CONFIGURE_ARGS} \

出来上がったらインストールして動かしてみる。

# nginx -V
nginx version: ninginx/1.11.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

ふむ。良さそうである。

# curl -I localhost
HTTP/1.1 200 OK
Server: ninginx/1.11.5
Date: Tue, 08 Nov 2016 12:46:11 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 08 Nov 2016 12:44:36 GMT
Connection: keep-alive
ETag: "5821c8b4-264"
Accept-Ranges: bytes

まごうことなきニンジン具合。

# curl  localhost/404
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>ninginx/1.11.5</center>
</body>
</html>

エラーページのとこもしっかりニンジンしている。オチはない。

まじめなはなし1

nginx の server_tokens はこのようにソースに埋め込まれているので、プロダクト名(= nginx )を隠そうとした場合に、ごく単直的には上のように直接いじる方法が取れる。が、今調べたらより簡単な方法があった。

headers-more-nginx-module を使って以下の様にオーバーライドできるらしい。これは覚えておきたい。

http{
         more_set_headers "Server: Your_New_Server_Name";
         server_tokens off; 
}

まじめなはなし2

NGINX Plus を使う場合には server_tokens にふつーに string で書ける。headers-more-nginx-module も不要。

Syntax: 	server_tokens on | off | string;
Default: 	

server_tokens on;

Context: 	http, server, location

Additionally, as part of our commercial subscription, starting from version 1.9.13 the signature in error messages and the “Server” response header field value can be set explicitly using the string with variables. An empty string disables the emission of the “Server” field.

Module ngx_http_core_module

© Kazuhisa Hara 2014-2022