您的位置:首页 > 运维架构 > Nginx

Nginx版本号优化及记录用户请求需要的时间 推荐

2016-11-22 11:03 676 查看
原因:
目前选择用nginx的越来越多了,无论其web处理,反代,负载方面均展现独特的魅力,但是往往很多人喜欢直接用基本的参数实现Nginx功能。这其中还要注意一些细节。
话不多说,切入正题,今天讨论两个问题

1、nginx版本号修改和隐藏
好处:加强安全,防止一些人找到指定版本漏洞进行攻击。
2、记录每个request 花费时间:
更详细知道请求需要的时间。
########################################################################

[b]1、nginx版本号修改和隐藏[/b]
[b]1、1)nginx版本号修改[/b]
查看当前Nginx版本号和编译信息


[root@nginx sbin]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
[root@nginx sbin]#
上面可以看到,Nginx版本是1.9.2,下面还有编译Nginx的一些参数。

那么风险就来了:
1)当后端程序停止,或者压力测试等情况,Nginx的版本号会很快速的暴露。
2)curl或者一些工具很轻松获取你的Nginx版本信息
3)再或者说,你根本不想人家知道你用的Nginx等
处理方式:

编辑nginx源码中的src/core/nginx.h头文件



修改为(自己可任意修改)




需要重新编译安装(线上业务注意不要乱搞,除非自己很熟练,可以做到平滑修改)
[root@nginx nginx-1.9.2]# /usr/local/nginx/sbin/nginx -V
nginx version: IIS/IIS
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
[root@nginx nginx-1.9.2]#







[root@nginx conf]# curl -I 192.168.1.223
HTTP/1.1 200 OK
Server: IIS/IIS
Date: Fri, 25 Mar 2016 21:31:38 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Mar 2016 21:38:26 GMT
Connection: keep-alive
ETag: "56f45e52-264"
Accept-Ranges: bytes

[root@nginx conf]#

1.2)Nginx版本号隐藏
nginx的HttpCoreModule提供了一条叫做server_tokens指令,我这要将这条指令设置为“server_tokens off”就可以了。
1、修改nginx.conf

http区段中插入server_tokens off;

#2、编辑php-fpm配置文件,如fastcgi.conf(更深入的话,当然也可以不做这一步)
找到:
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
改为:
fastcgi_param SERVER_SOFTWARE nginx;
3、重启或者reloadNginx即可。

2记录访问日志中每个请求的时间

[root@nginx ~]# tail -f /usr/local/nginx/logs/access.log
192.168.1.243 - -[26/Mar/2016:05:08:23 +0800] "GET /test HTTP/1.1" 302 160"-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36" "-"
192.168.1.243 - -[26/Mar/2016:05:08:34 +0800] "GET / HTTP/1.1" 304 0"-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36" "-"
192.168.1.243 - -[26/Mar/2016:05:08:41 +0800] "GET /index.php HTTP/1.1" 302 160"-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36" "-"




查看Nginx日志相关访问记录:(默认格式)
log_format access '$remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';

access_log logs/access.log access;
修改为:

log_format access '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"' ' elapsed=${request_time}s';(注意Apache此参数是us)
access_log logs/access.log access;

检测Nginx配置文件,并重新启动服务(重装也可以)

[root@nginx ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# /etc/init.d/nginx reload
[root@nginx ~]#
[root@nginx logs]# tail -f access.log
192.168.1.243 - - [22/Nov/2016:10:45:52 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36" elapsed=0.000s

192.168.1.243 - - [22/Nov/2016:10:45:52 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36" elapsed=0.000s
192.168.1.243 - - [22/Nov/2016:10:45:54 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36" elapsed=0.000s




日志相关参数官网给的解释是:Module ngx_http_log_module

$remote_addr, $http_x_forwarded_for 记录客户端 IP
$remote_user 记录客户端用户名称
[b]$time_local 通用日志格式下的本地时间[/b]
$request 记录请求的 URL 和 HTTP Protocol
$status 记录请求状态
$body_bytes_sent 发送给客户端的 Bytes,不包括 Header 的大小;该变数与 Apache mod_log_config 的 "%B" 相容
$bytes_sent 发送给客户端的 总Bytes数
$connection 连接的序列号
$connection_requests 当前通过一个连接获得的请求数量
$msec 日志写入时间。单位为秒,精度是毫秒
$pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为"p",否则为"."
$http_referer 记录从哪个页面链接访问过来的
$http_user_agent 记录客户端浏览器相关信息
$request_length 请求的长度(包括请求行,请求头和请求正文)
$request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个位元组开始,直到把最后一个字元发送给客户端后进行日志写入为止
$time_iso8601 ISO8601标准格式下的本地时间

Nginx对./connfigue支持的参数(可--help查看)



















查看Nginx安装了那些模块
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# /etc/init.d/nginx reload
[root@nginx ~]#
[root@nginx ~]# /usr/local/nginx/sbin/nginx -V
nginx version: IIS/IIS
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
[root@nginx ~]#

如有问题,可联系博主:http://renzhiyuan.blog.51cto.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  优化 安全 Nginx