一、隐藏 Nginx版本号
1、在生产环境中,需要隐藏Ngnx的版本号,以避免安全漏洞的泄漏
2、查看方法
使用fiddler工具在 Windows客户端查看 Nginx版本号
在 Centos系统中使用“curl -I 网址”命令查看Nginx版本号
3、nginx隐藏版本号的方法
修改配置文件法
修改源码法
4、隐藏 Nginx版本号配置命令
方法一:
Nginx的配置文件中的 server_tokens选项的值设置为off
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; ##添加,关闭版本号
}
systemctl restart nginx
curl -I http://192.168.66.44/ ##查看版本号
方法二:
修改源码文件,重新编译安装
vim /opt/nginx-1.12.0/src/core/nginx.h
#define nginx_version 1012000
#define NGINX_VERSION “1.0.0” #将原始的1.12.0修改为1.0.0
#define NGINX_VER “IIS” NGINX_VERSION #将原始的Nginx修改为IIS
#重新编译安装
cd /opt/nginx-1.12.0
./configure
–prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-http_stub_status_module
make && make install
#将方法一中关闭的版本号重新打开
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens on; #打开
#重启服务
systemctl restart nginx.service
#查看版本号是否隐藏
curl -I http://192.168.66.44/
二、修改用户与组
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #将前面的#注释掉,然后修改用户与组为nginx
worker_processes 1;
systemctl restart nginx.service
ps aux | grep nginx #查看用户与组是否修改成功
三、配置缓存时间
http {
include mime.types;
default_type application/octet-stream;
server_tokens on;
…
location / {
root html;
index index.html index.htm;
}
location ~ \.(gif|jpg|jepg|bmp|ico)$ { #复制上面4行并修改
root html;
expires 1d; #设置缓存时间为1天
}
需要添加张图片在nginx首页中
使用浏览器直接访问.图片
http://192.168.66.44/ganfanren.jpg
四、日志切割
需要自己写脚本进行日志的切割
vim /opt/fengge.sh
#!/bin/bash
#rizhi fengge
day=$(date -d “-1 day” “+%Y%m%d”) #显示前一天时间
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path #创建日志文件目录
mv /usr/local/nginx/logs/access.log
l
o
g
s
p
a
t
h
/
g
c
c
.
c
o
m
−
a
c
c
e
s
s
.
l
o
g
−
{logs_path}/gcc.com-access.log-
logspath/gcc.com−access.log−day #移动并重命名日志文件
kill -USR1 $(cat $pid_path) #重建新日志文件
find $logs_path -mtime +30 | xargs rm -rf #删除30天之前的日志文件
chmod +x /opt/fengge.sh
/opt/fengge.sh
ls /var/log/nginx/
ls /usr/local/nginx/logs/access.log
crontab -e #设置定时任务进行日志的分割收集
0 1 * * * /opt/fengge.sh
补充:
在linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime, atime,mtimectime (status time):
当修改文件的权限或者属性的时候,就会更新这个时间, ctime并不是create time,更像是change time,只有当更新文件的属性或者权限的时候才会更新这个时间,但是更改内容的话是不会更新这个时间。
atime (accesstime):
当使用这个文件的时候就会更新这个时间。
mtime (modification time):
当修改文件的内容数据的时候,就会更新这个时间,而更改权限或者属性,mtime不会改变,这就是和ctime的区别。
五、连接超时
HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。
KeepAlive在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens on;
…
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
systemctl restart nginx.service
keepalive timeout:
指定KeepAlive的超时时间(timeout) 。指定每个TCP连接最多可以保持多长时间,服务器将会在这个时间后关闭连接。
Nginx的默认值是65秒,有些浏览器最多只保持60秒,所以可以设定为60秒。若将它设置为0,就禁止了keepalive连接。
第二个参数(可选的)指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数, Nginx不会发送Keep-Alive响应头。
client_header_timeout:
客户端向服务端发送一个完整的request header的超时时间。如果客户端在指定时间内没有发送一个完整的request header,Nginx返回HTTP 408 (Request Timed out)。
六、更改进程数
cat /proc/cpuinfo | grep -c “physical id” #查看CPU核数
ps aux | grep nginx #查看nginx主进程中包含几个子进程
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2; #修改为核数相同或者2倍
worker_cpu_affinity 01 10; #设置每个进程由不同cpu处理,进程数配为2时为0001、0010、0100、1000
systemctl restart nginx.service
七、配置网页压缩
vim /usr/local/nginx/conf/nginx.conf
gzip on; #将改行注释取消掉开启gzip压缩功能,并添加下面内容
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 64k; #压缩缓冲区,大小为4个64k缓冲区
gzip_http_version 1.1; #压缩版本(默认为1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 6; #压缩比率
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json; #压缩类型,表示哪些网页文档启用压缩功能
gzip_vary on; #支持前端缓存服务器存储压缩页面
cd /usr/local/nginx/html/ #提前将game.jpg图片上传到该目录下
vim index.html
Thank you for using nginx.
#添加此行systemctl restart nginx.service
八、防盗链
配置盗链机
yum install -y httpd
vim /var/www/html/index.html
IT WORKS!
echo “192.168.66.44 www.accp.com” >> /etc/hosts
echo “192.168.66.11 www.kgc.com” >> /etc/hosts
systemctl restart httpd
配置源主机(防盗链)
vim /usr/local/nginx/conf/nginx.conf
location ~* .(gif|jpg|jepg|bmp|ico)$ {
valid_referers *.kgc.com kgc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;
}
}
systemctl restart nginx
~* . (jpglgiflswf)s :这段正则表达式表示匹配不区分大小写,以.jpg或.gif或.swf结尾的文件;
valid referers :设置信任的网站,可以正常使用图片;后面的网址或者域名: referer中包含相关字符串的网址;
语句:如果链接的来源域名不在valid referers所列出的列表中, sinvalid referer为1,则执行后面的操作,即进行重写或返回403页面。
九、fpm参数优化
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
vim /usr/local/php/etc/php-fpm.d/www.conf
-----96行------
pm = dynamic #fpm进程启动方式,动态的
-----107行-----
pm.max children = 20 #fpm进程启动的最大进程数-
------112-------
pm.start_servers = 5 #动态方式下启动时默认开启的进程数,在最小和最大之间
------117行-------
pm.min_spare_servers = 2 #动态方式下最小空闲进程数
------122行-------
pm.max_spare_servers = 8 #动态方式下最大空闲进程数
kill -USR2 cat /usr/local/php/var/run/php-fpm.pid
#重启php-fpm
netstat -anpt | grep 9000