jjzjj

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

任志远Ray 2023-03-28 原文
原因:

目前选择用nginx的越来越多了,无论其web处理,反代,负载方面均展现独特的魅力,但是往往很多人喜欢直接用基本的参数实现Nginx功能。这其中还要注意一些细节。

话不多说,切入正题,今天讨论两个问题

1、nginx版本号修改和隐藏

好处:加强安全,防止一些人找到指定版本漏洞进行攻击。

2、记录每个request 花费时间:

更详细知道请求需要的时间。

########################################################################


1、nginx版本号修改和隐藏

1、1)nginx版本号修改

查看当前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 记录客户端用户名称

$time_local 通用日志格式下的本地时间

$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版本号优化及记录用户请求需要的时间的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  5. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  6. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  7. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

  8. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  9. ruby-on-rails - 如果我将 ruby​​ 版本 2.5.1 与 rails 版本 2.3.18 一起使用会怎样? - 2

    如果我使用ruby​​版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby​​1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更

  10. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

随机推荐