调优目录:
1.隐藏版本号
2.身份验证
3.日志切割
4.目录文件访问控制
5.连接超时时间
隐藏版本号:
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost local]# cd nginx-1.12.2/
编译前的优化主要是用来修改程序名等等,例如:
[root@localhost nginx-1.12.2]# curl -I http://www.baidu.com
……
Server: BWS/1.1
……
[root@localhost nginx-1.12.2]# curl -I http://www.sina.com.cn
……
Server: nginx
……
[root@localhost nginx-1.12.2]# curl -I http://www.xuegod.cn
HTTP/1.1200 OK
Server: nginx/1.6.2 #我们目标是将nginx更改名字
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.4.45
Set-Cookie: PHPSESSID=smm0i6u4f9v7bj0gove79ja1g7; path=/
Cache-Control: no-cache
Date: Mon,07 Mar 201606:09:11 GMT
[root@localhost nginx-1.12.2]# vim src/core/nginx.h //目的更改源码隐藏软件名称和版本号
改:
13 #define NGINX_VERSION "1.12.2" #此行修改的是你想要的版本号
14 #define NGINX_VER "nginx/" NGINX_VERSION #此行修改的是你想修改的软件
名称
为:
13 #define NGINX_VERSION "8.8.2"
14 #define NGINX_VER "XWS/" NGINX_VERSION
[root@localhost nginx-1.12.2]# vim src/http/ngx_http_header_filter_module.c
改:49 static u_char ngx_http_server_string[] = "Server: nginx" CRLF; //修改HTTP头信息中的connection字段,防止回显具体版本号
为:49 static u_char ngx_http_server_string[] = "Server: XWS" CRLF;
拓展:通用http头域
通用头域包含请求和响应消息都支持的头域,通用头域包含Cache-Control(缓存控制)、 Connection(连接)、Date(日期)、Pragma(短语)、Transfer-Encoding(传输编码)、Upgrade(升级)、Via。对通用头域的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头域,一般将会作为实体头域处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!
身份验证:
[root@localhost nginx-1.12.2]# vim src/http/ngx_http_special_response.c
//这个文件定义了http错误码的返回,有时候我们页面程序出现错误,Nginx会代我们返回相应的错误代码,回显的时候,会带上nginx和版本号,我们把他隐藏起来
改:22 "<hr><center>" NGINX_VER "</center>" CRLF #这里 就不需要修改了,以前老版本1.10需要修改,这里新版本中直接调用的是一个之前定义的" #define NGINX_VER "XWS/" NGINX_VERSION " 这里不需要修改
为:22 "<hr><center>" XWS "</center>" CRLF #老版本改这里
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /xuegod/{
auth_basic "haha";
auth_basic_user_file /usr/local/nginx/conf/passwd;
}
用户创建,如果没有htpasswd命令,需要手动安装httpd-tools程序包
[root@localhost logs]# rpm -ivh /mnt/Packages/httpd-tools-2.4.6-67.el7.centos.x86_64.rpm
或:使用yum进行安装
[root@localhost logs]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -cb /usr/local/nginx/conf/passwd aaa 123
[root@localhost ~]# chmod 400 /usr/local/nginx/conf/passwd
[root@localhost ~]# chown nginx /usr/local/nginx/conf/passwd
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
日志切割:
脚本:
#!/bin/bash
date=$(date +%F -d -1day)
cd/usr/local/nginx/logs
if[!-d cut ];then
mkdir cut
fi
mv access.log cut/access_$(date +%F -d -1day).log
mv error.log cut/error_$(date +%F -d -1day).log
/usr/local/nginx/sbin/nginx -s reload
tar-jcvf cut/$date.tar.bz2 cut/*
rm-rf cut/access*&&rm-rf cut/error*
cat>>/var/spool/cron/root<<eof
00 00 * * * /bin/sh /usr/local/nginx/logs/cut_nginx_log.sh >/dev/null 2>&1
eof
find-type f -mtime +10|xargs rm -rf
健康检查的日志,不用输入到log中,因为这些日志没有意义,我们分析的话只需要分析访问日志,看看一些页面链接,如200,301,404的状态吗,在SEO中很重要,而且我们统计PV是页面计算,这些都没有意义,反而消耗了磁盘IO,降低了服务器性能,我们可以屏蔽这些如图片,js,css这些不宜变化的内容
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~.*.(js|jpg|jpeg|JPG|JPEG|css|bmp|gif|GIF)$ {
access_log off;
}
日志目录权限优化
[root@localhost ~]# chown -R root.root /xuegod/logs/
[root@localhost ~]# chmod -R 700 /xuegod/logs/
日志格式优化
#vim /usr/local/nginx/conf/nginx.conf
log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘‘$status $body_bytes_sent “$http_referer” ‘‘”$http_user_agent” $http_x_forwarded_for’;
其中,各个字段的含义如下:
1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
2.$remote_user :用来记录客户端用户名称;
3.$time_local : 用来记录访问时间与时区;
4.$request : 用来记录请求的url与http协议;
5.$status : 用来记录请求状态;成功是200,
6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
7.$http_referer :用来记录从那个页面链接访问过来的;
8.$http_user_agent :记录客户端浏览器的相关信息;
目录文件访问控制:
主要用在禁止目录下指定文件被访问,当然也可以禁止所有文件被访问!一般什么情况下用?比如是有存储共享,这些文件本来都只是一些下载资源文件,那么这些资源文件就不允许被执行,如sh,py,pl,php等等
例如:禁止访问images下面的php程序文件
location ~^/images/.*.(php|php5|.sh|.py|.pl)$ {
deny all;
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# mkdir /usr/local/nginx/html/images
[root@localhost ~]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/images/index.php
测试访问
多目录组合配置方法
location ~^/images/(attachment|avatar)/.*.(php|php5|.sh|.py|.py)$ {
deny all;
}
配置nginx禁止访问*.txt文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //server字段中
location ~* .(txt|doc)$ {
if( -f $request_filename){
root /usr/local/nginx/html;
break;
}
deny all;
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
当然,可以重定向到某一个URL
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~* .(txt|doc)$ {
if( -f $request_filename){
root /usr/local/nginx/html;
rewrite ^/(.*)$ http://www.baidu.com last;
break;
}
}
对目录进行限制的方法
[root@localhost ~]# mkdir -p /usr/local/nginx/html/{xuegod,godxue}
[root@localhost ~]# echo xuegod > /usr/local/nginx/html/xuegod/index.html
[root@localhost ~]# echo god > /usr/local/nginx/html/godxue/index.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /xuegod/ {return404;}
location /godxue/ {return403;}
上面是直接给了反馈的状态码,也可以把他能够过匹配deny all方式做
上面是直接给了反馈的状态码,也可以哦他能够过匹配deny all方式做
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~^/(xuegod)/{
deny all;
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
连接超时时间:
主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的,TCP的三次握手四次挥手等,我们一般断掉的是那些建立连接但是不做事儿,也就是我建立了链接开始,但是后续的握手过程没有进行,那么我们的链接处于等待状态的,全部断掉!
同时我们也希望php建议短链接,消耗资源少
35 keepalive_timeout 65;
tcp_nodelayon;
client_header_timeout15;
client_body_timeout15;
send_timeout15;
keepalived_timeout 客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
tcp_nodelay;也是防止网络阻塞,不过要包涵在keepalived参数才有效
client_header_timeout 客户端请求头读取超时时间,如果超过设个时间没有发送任何数据,nginx将返回request time out的错误
client_body_timeout 客户端求主体超时时间,超过这个时间没有发送任何数据,和上面一样的错误提示
send_timeout 响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接