nginx调优操作之nginx隐藏其版本号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx调优操作之nginx隐藏其版本号相关的知识,希望对你有一定的参考价值。

1.nginx下载

  下载网址:nginx.org

2.解压nginx

[[email protected] soft]# ls
nginx-1.10.3.tar.gz
[[email protected] soft]# tar xf nginx-1.10.3.tar.gz 

3.修改源码

[[email protected] soft]# ls
nginx-1.10.3  nginx-1.10.3.tar.gz
#进入nginx源码包 [[email protected] soft]# cd nginx
-1.10.3 [[email protected] nginx-1.10.3]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [[email protected] nginx-1.10.3]#

  修改nginx.h文件

[[email protected] nginx-1.10.3]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[[email protected] nginx-1.10.3]# cd src/core/
[[email protected]4i8oy1reej7o8pmZ core]# vim nginx.h 
[[email protected] core]# 

  修改后如下:

  8 #ifndef _NGINX_H_INCLUDED_
  9 #define _NGINX_H_INCLUDED_
 10 
 11 
 12 #define nginx_version      1010003      
 13 #define NGINX_VERSION      "0.0.1"            #NGINX对外显示的版本号
 14 #define NGINX_VER          "LiWang/" NGINX_VERSION   #NGINX对外显示的服务器名
 15 
 16 #ifdef NGX_BUILD
 17 #define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
 18 #else
 19 #define NGINX_VER_BUILD    NGINX_VER
 20 #endif
 21 
 22 #define NGINX_VAR          "NGINX"
 23 #define NGX_OLDPID_EXT     ".oldbin"
 24 
 25 
 26 #endif /* _NGINX_H_INCLUDED_ */

  修改ngx_http_special_response.c内容

#进入src/http文件夹下
[[email protected] http]# pwd
/root/soft/nginx-1.10.3/src/http
#利用vim编辑ngx_http_special_response.c文件 [[email protected] http]# vim ngx_http_special_response.c
 #修改结果如下
14 static ngx_int_t ngx_http_send_error_page(ngx_http_request_t *r, 15 ngx_http_err_page_t *err_page); 16 static ngx_int_t ngx_http_send_special_response(ngx_http_request_t *r, 17 ngx_http_core_loc_conf_t *clcf, ngx_uint_t err); 18 static ngx_int_t ngx_http_send_refresh(ngx_http_request_t *r); 19 20 21 static u_char ngx_http_error_full_tail[] = 22 "<hr><center>" NGINX_VER "Server:LiWang blog:www.cnblogs.com/wang-li</center>" CRLF 23 "</body>" CRLF 24 "</html>" CRLF 25 ; 26 27 28 static u_char ngx_http_error_tail[] = 29 "<hr><center>nginx</center>" CRLF 30 "</body>" CRLF 31 "</html>" CRLF 32 ;

  编辑vim ngx_http_header_filter_module.c 文件

[[email protected] http]# pwd
/root/soft/nginx-1.10.3/src/http
[[email protected] http]# vim ngx_http_header_filter_module.c 

  修改结果如下

 47 
 48 
 49 static char ngx_http_server_string[] = "Server: LiWang" CRLF;
 50 static char ngx_http_server_full_string[] = "http://www.cnblogs.com/wang-li" NGINX_VER CRLF;
 51 
 52 
 53 static ngx_str_t ngx_http_status_lines[] = {
 54 
 55     ngx_string("200 OK"),
 56     ngx_string("201 Created"),
 57     ngx_string("202 Accepted"),
 58     ngx_null_string,  /* "203 Non-Authoritative Information" */
 59     ngx_string("204 No Content"),
 60     ngx_null_string,  /* "205 Reset Content" */
 61     ngx_string("206 Partial Content"),
 62 
 63     /* ngx_null_string, */  /* "207 Multi-Status" */
 64 
 65 #define NGX_HTTP_LAST_2XX  207
 66 #define NGX_HTTP_OFF_3XX   (NGX_HTTP_LAST_2XX - 200)

4.开始进行源码编译

  安装必要的插件

[[email protected] nginx-1.10.3]# yum install -y pcre pcre-devel zlib zlib-devel

  添加nginx用户

[[email protected] nginx-1.10.3]# useradd nginx -s /sbin/nologin 
[[email protected] nginx-1.10.3]# tail -n 1 /etc/passwd
nginx:x:500:500::/home/nginx:/sbin/nologin

  开始进行编译

[[email protected] nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10 --user=nginx --group=nginx

  出现如下信息则为制作ok

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/usr/local/nginx-1.10"
  nginx binary file: "/usr/local/nginx-1.10/sbin/nginx"
  nginx modules path: "/usr/local/nginx-1.10/modules"
  nginx configuration prefix: "/usr/local/nginx-1.10/conf"
  nginx configuration file: "/usr/local/nginx-1.10/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.10/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-1.10/logs/error.log"
  nginx http access log file: "/usr/local/nginx-1.10/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

  开始make and make install

[[email protected] nginx-1.10.3]# make && make install

5.开始配置

  配置参照网址:

  http://www.cnblogs.com/wang-li/p/6689615.html

  配置过后进行访问

[[email protected] conf]# curl -I www.wang-li.top:1234
HTTP/1.1 200 OK
Server: LiWang
Date: Tue, 11 Apr 2017 14:11:03 GMT
Content-Type: text/html
Content-Length: 560
Last-Modified: Sun, 26 Mar 2017 13:51:24 GMT
Connection: keep-alive
ETag: "58d7c75c-230"
Accept-Ranges: bytes

[[email protected] conf]# 

 





以上是关于nginx调优操作之nginx隐藏其版本号的主要内容,如果未能解决你的问题,请参考以下文章

Nginx安全优化与性能调优

Nginx 调优

Nginx优化之基本安全优化(隐藏Nginx软件版本号信息,更改源码隐藏Nginx软件名及版本号,更改Nginx服务的默认用户)

Nginx 调优

Nginx之隐藏版本号,优化缓存,日志分割

Nginx服务优化之隐藏版本号修改用户与组配置页面缓存与时间日志分割以及设置连接超时