一、调整配置文件参数
- 调整参数隐藏nginx版本信息
[root@web01 extra]# curl -I www.suffergtf.com
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3 #####可以查看到nginx版本号
Date: Tue, 05 Jun 2018 17:02:56 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="auth test" [root@web01 extra]# vim www.conf server { server_tokens off; ####添加该字段,隐藏版本号 listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; auth_basic "auth test"; auth_basic_user_file /application/nginx/conf/htpasswd; } } server { listen 80; server_name suffergtf.com; rewrite ^/(.*) http://www.suffergtf.com/$1 permanent; }
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# curl -I www.suffergtf.com
HTTP/1.1 401 Unauthorized
Server: nginx ####不显示版本号了
Date: Tue, 05 Jun 2018 17:04:40 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="auth test"
- 修改源码隐藏使用的软件名
[root@web01 extra]# vim /server/tools/nginx-1.6.3/src/core/nginx.h ####编辑源码文件
13 #define NGINX_VERSION "1.6.3" #####在第13行修改如下
14 #define NGINX_VER "nginx/" NGINX_VERSION ####修改如下
16 #define NGINX_VAR "NGINX" ####修改如下
13 #define NGINX_VERSION "2.2.2" ####2.2.2或者其他,自己定义
14 #define NGINX_VER "apache/" NGINX_VERSION #####自己定义
16 #define NGINX_VAR "APACHE" ####自己定义
[root@web01 extra]# vim /server/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c
49 static char ngx_http_server_string[] = "Server: nginx" CRLF; ####修改如下
49 static char ngx_http_server_string[] = "Server: apache" CRLF;
[root@web01 extra]# vim /server/tools/nginx-1.6.3/src/http/ngx_http_special_response.c
22 "<hr><center>" NGINX_VER "</center>" CRLF ####修改如下
29 "<hr><center>nginx</center>" CRLF ####修改如下
22 "<hr><center>" NGINX_VER "<http://www.suffergtf.com>" CRLF
29 "<hr><center>apache</center>" CRLF
重新编译安装到其他的目录
[root@web01 ~]# cd /server/tools/nginx-1.6.3
[root@web01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3.test/ --with-http_stub_status_module --with-http_ssl_module
[root@web01 nginx-1.6.3]# make && make install
[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -s stop
[root@web01 nginx-1.6.3]# /application/nginx-1.6.3.test/sbin/nginx
[root@web01 nginx-1.6.3]# curl -I localhost
HTTP/1.1 200 OK
Server: apache/2.2.2
Date: Tue, 05 Jun 2018 17:41:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 05 Jun 2018 17:36:39 GMT
Connection: keep-alive
ETag: "5b16ca27-264"
Accept-Ranges: bytes

- 更改Nginx服务的默认用户
[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -V nginx version: apache/2.2.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module ###编译时指定
[root@web01 nginx-1.6.3]# vim /application/nginx/conf/nginx.conf.default ###因为nginx.conf已经做过配置,这里拿nginx.conf.default默认配置参考
1
2 #user nobody; ####如果已经编译安装,没有加上面的参数,可以修改配置文件,将注释去掉改为 user nginx;
- 优化nginx服务的worker进程个数
[root@web01 ~]# grep processor /proc/cpuinfo |wc -l ####查看cpu核心数 2
[root@web01 ~]# vim /application/nginx/conf/nginx.conf
1 worker_processes 1; ###修改如下
1 worker_processes 2; ####nginx进程数,根据服务器的核心数来修改,如本服务器为单cpu双核心,所以进程数修改为2;
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
[root@web01 ~]# ps aux|grep nginx
root 1456 0.0 0.3 45172 1132 ? Ss 02:10 0:00 nginx: master process /application/nginx/sbin/nginx
nginx 1457 0.0 0.4 45616 1728 ? S 02:10 0:00 nginx: worker process
nginx 1458 0.0 0.4 45616 1784 ? S 02:10 0:00 nginx: worker process
root 1460 0.0 0.2 103260 840 pts/0 S+ 02:10 0:00 grep nginx
- 绑定不同的nginx进程到不同的CPU上
默认情况下,nginx的多个进程有可能在同一CPU或CPU的某一核上,导致Nginx进程资源分配不均匀
[root@web01 ~]# vim /application/nginx/conf/nginx.conf
1 worker_processes 2;
2 worker_cpu_affinity 0001 0010; ###添加这个字段。worker_cpu_affinity是配置nginx进程与cpu亲和力的参数,0001 0010是掩码,表示1、2号CPU
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
- 事件处理模型优化
默认情况下,nginx会自动选择最佳的事件处理模型服务,这里我们手动指定
[root@web01 ~]# vim /application/nginx/conf/nginx.conf
1 worker_processes 2;
2 worker_cpu_affinity 0001 0010;
3 error_log logs/error.log error;
4 events {
5 use epoll; ####添加此行内容
6 worker_connections 1024;
7 }
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
- 调整nginx单个进程允许的客户端最大连接数
[root@web01 ~]# vim /application/nginx/conf/nginx.conf 6 worker_connections 1024; ###修改第6行,如下
6 worker_connections 20480;
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
- 配置nginx进程最大打开文件数
[root@web01 ~]# vim /application/nginx/conf/nginx.conf 1 worker_processes 2; 2 worker_rlimit_nofile 65535; ###添加此行内容 3 worker_cpu_affinity 0001 0010;
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
- 开启高效文件传输模式
[root@web01 ~]# vim /application/nginx/conf/nginx.conf
9 http {
10 include mime.types;
11 default_type application/octet-stream;
12 sendfile on; ###1.6.3默认开启
13 tcp_nopush on; ###添加此行
14 keepalive_timeout 65;
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
- 优化nginx连接参数,调整连接超时时间
连接超时:当服务器建立的连接没有接收处理请求时,让此连接自动退出减少服务器资源消耗,当nginx和fastcgi服务建立连接请求php时,如果fastcgi服务无法给nginx返回数据时,自动退出连接,不让用户一直等
[root@web01 ~]# vim /application/nginx/conf/nginx.conf ###增加如下内容
9 http {
10 include mime.types;
11 default_type application/octet-stream;
12 sendfile on;
13 tcp_nopush on;
14 keepalive_timeout 65; ###默认65即可,连接保持时间,
15 tcp_nodelay on; ####默认开启,当发生数据发送时,内核并不会马上发送,可能会等待更多的字节组成一个数据包,提高I/0性能,但是,在每次发送很少字节的业务场景中,使用tcp_nodelay功能,等待时间较长
16 client_header_timeout 15; ####服务器读取客户端请求头的超时时间
17 client_body_timeout 15; ####服务端读取客户端请求主体的超时时间
18 send_timeout 25; ####服务端传送HTTP响应信息到客户端的超时时间
- 上传文件大小的限制
[root@web01 ~]# vim /application/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; client_header_timeout 15; client_body_timeout 15; send_timeout 25; client_max_body_size 8m; ###客户端请求主体的大小,可放置在http模块,server模块,location模块,根据具体需求调整,默认为1m
- nginx gzip性能优化
[root@web01 ~]# vim /application/nginx/conf/nginx.conf 18 send_timeout 25; 19 client_max_body_size 8m; 20 gzip on; #####开启gzip功能 21 gzip_min_length 1k; ######允许压缩的页面最小字节,小于1k,不压缩 22 gzip_buffers 4 16k; #####压缩缓冲区大小,表示申请4个16k的内存作为压缩结果流缓存 23 gzip_http_version 1.1; #####压缩版本 24 gzip_comp_level 2; #####压缩比例,1-9依次增强 25 gzip_types text/css text/xml application/javascript; ####指定压缩类型 26 gzip_vary on; #####vary header支持,该选项可以让前端的缓存服务器经过gzip压缩的页面 27 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- nginx expires缓存实现性能优化
nginx expires的功能是为用户访问的网站内容设定一个过期时间,当用户第一次访问,会把内容缓存到本地,用户下次访问可以直接浏览缓存,直到过期
放置在location模块,可以通过文件扩展名,URI路径,单个文件来进行设置,语法如下
expires 30d; 缓存30天
expires 20y; 缓存30年
二、nginx日志优化
- 日志切割
[root@web01 ~]# vim /server/scripts/www_cutlog.sh ###这是一个切割日志脚本,是将/application/nginx/logs/access_www.log重命名为以当前日期命名的文件,
在重新加载nginx生成新的access_www.log,并且将60天以前的日志删除 #!/bin/bash ######this a cutlog shell for www.suffergtf.com####### Dateformat=$(date +%Y%m%d) Basedir="/application/nginx" Nginxlogdir="$Basedir/logs" Logname="access_www" [ -d $Nginxlogdir ]&&cd $Nginxlogdir||exit 1 [ -f ${Logname}.log ] ||exit 1 /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log $Basedir/sbin/nginx -s reload find ${Nginxlogdir} -mtime +60 -name *_${Logname}.log |xargs rm -f
[root@web01 ~]# crontab -l ####定时任务,每天凌晨运行
####logrotate for www.suffergtf.com by suffergtf
00 00 * * * /bin/sh /server/scripts/www_logrotate.sh >/dev/null 2>&1
- 不记录不需要的访问日志
[root@web01 ~]# vim /application/nginx/conf/extra/www.conf server { server_tokens off; listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; auth_basic "auth test"; auth_basic_user_file /application/nginx/conf/htpasswd; } location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)${ #####添加该模块内容,匹配不记录日志的元素扩展名,关掉日志 access_log off; } }
- 访问日志的权限设置
[root@web01 ~]# chown -R root.root /application/nginx/logs [root@web01 ~]# chmod -R 700 /application/nginx/logs
三、nginx站点目录及文件URL访问控制
- 禁止解析指定目录下的指定程序
[root@web01 extra]# vim www.conf server { server_tokens off; listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; auth_basic "auth test"; auth_basic_user_file /application/nginx/conf/htpasswd; } location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)${ access_log off; } location ~ /images/.*\.(php|php5|sh|pl|py)$ { ###添加该location,禁止解析.php .php5 .sh .pl .py;此限制必须写在nginx处理php服务配置的前面 deny all; } }
- 禁止访问.txt和.doc
[root@web01 extra]# vim www.conf
server {
server_tokens off;
listen 80;
server_name www.suffergtf.com;
access_log logs/access_www.log main gzip buffer=32k flush=5s;
location / {
root html/www;
index index.html index.htm;
auth_basic "auth test";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)${
access_log off;
}
location ~ /images/.*\.(php|php5|sh|pl|py)$ {
deny all;
}
location ~* \.(txt|doc)$ { ####禁止访问html/www下的.txt .doc文件
root html/www
deny all;
}
}
- 禁止访问指定目录下的所有文件和目录
[root@web01 extra]# vim www.conf server { server_tokens off; listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; auth_basic "auth test"; auth_basic_user_file /application/nginx/conf/htpasswd; } location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)${ access_log off; } location ~ /images/.*\.(php|php5|sh|pl|py)$ { deny all; } location ~* \.(txt|doc)$ { root /html/www deny all; } location ~ ^/(static)/ { deny all; } }
- 禁止访问目录并返回HTTP状态码
location /admin/ { return 404; }
- 限制网站来源IP访问
location / { allow 192.168.0.0/24; deny all; }
- 禁止非法域名解析访问企业网站
server { listen 80 default_server; server_name _; return 403; ####使用IP访问网站,返回403错误 #rewrite ^(.*) http://www.suffergtf.com/$1 permanent; ####或者跳转到主页 }
若有域名恶意解析到公司的服务器IP,可以在server标签里添加以下代码
server {
server_tokens off;
listen 80;
server_name www.suffergtf.com;
access_log logs/access_www.log main gzip buffer=32k flush=5s;
if ($host !~ ^www\.suffergtf\.com){
rewrite ^(.*) http://www.suffergtf.com$1 permanent;
}
四、nginx图片及目录防盗链
location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { #####根据图片后缀,跳转指定图片 valid_referers none blocked *.suffergtf.com suffergtf.com; if ($invalid_referer) { rewrite ^/ http:/www.suffergtf.com/img/nolink.jpg; } } location /images { #####根据目录,返回403 root html/www/images; valid_referers none blocked *.suffergtf.com suffergtf.com; if ($invalid_referer) { return 403; } }
五、站点目录文件及目录权限优化
目录 root.root 755
文件 root.root 644
六、防爬虫优化
[root@web01 extra]# vim www.conf server { server_tokens off; listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; if ($http_user_agent ~* LWP::Simple|BBBike|wget){ ####阻止下载协议代理 return 403; } if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googelbot-Mobile|Googlebot-Image|Mdediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot"){ return 403; ######阻止爬虫代理 } if ($http_user_agent ~* "Firefox|MSIE"){ #####禁止不同浏览器访问 rewrite ^(.*) http://blog.suffergtf.com/$1 permanent; } if ($host !~ ^www\.suffergtf\.com){ rewrite ^(.*) http://www.suffergtf.com$1 permanent; }
七、限制HTTP的请求方法
if ($request_method ~* ^(GET)$){ ####如果请求方法为GET,则返回501 return 501; }
八、控制nginx并发连接数
语法
limit_conn_zone key zone=name:size ###key可以是字符串,nginx自带变量等,name为内存区域的名称,size为内存区域的大小
http模块添加 limit_conn_zone $binary_remote_addr zone=addr:10m; #######以请求客户端的ip作为key,内存区域命名为addr,分配10m内存空间 limit_conn_zone $server_name zone=perserver:10m; server模块添加
limit_conn addr 1; ######限制单IP的并发连接数为1
#limit_conn perserver 2; ####虚拟主机连接总数为2
九、控制客户端请求nginx的速率
语法: limit_req_zone key zone=name:size rate=rate; ####key可以是字符串,nginx自带变量;size为内存区域的大小,rate为速率
http模块
limit_conn_zone $binary_remote_addr zone=addr:10m rate=10r/s #####以请求的客户端的ip作为key,内存区域命名为addr,分配10m内存空间,访问速率限制为10次/秒
server模块
limit_req zone=addr burst=5; ###使用前面定义的addr内存空间,队列值为5,可以有5个请求排队等待