一、配置Nginx隐藏版本号
隐藏Nginx版本号,避免安全漏洞泄露
可以使用Fiddler工具抓取数据包,查看Nginx版本;也可以在CentOS中使用 curl -I http://192.168.100.20 显示响应报文首部信息。
1.1 方法一:修改配置文件方式
1
2
3
4
5
6
7
8
9
10
11
|
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.200.10
|
1.2 修改源码文件,重新编译安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
vim /opt/nginx-1.12.0/src/core/nginx.h #define NGINX_VERSION "1.1.1" #修改版本号 #define NGINX_VER "IIS" NGINX_VERSION #修改服务器类型 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 curl -I http: //192.168.200.10
|
2、修改用户与组
1
2
3
4
5
6
7
|
vim /usr/local/nginx/conf/nginx.conf user nginx nginx; #取消注释,修改用户为nginx,组为nginx systemctl restart nginx ps aux | grep nginx #主进程由root创建,子进程由nginx创建 |
3、缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。一般针对静态网页设置,对动态网页不设置缓存时间
vim /usr/ local/nginx/conf/nginx. conf http { server { location /I { root html; index index.html index. htm; } location ~ \. (gif|jpg|jepg|png|bmp|ico)$ { #加入新的location, 以图片作为缓存对象 root html ; expires 1d; #指定缓存时间,1天 http://www.kgc.com/ wangsicong. ipg systemctl restart nginx
在Linux系统中,打开火狐浏览器,右击点查看元素
选择网络——>选择HTML、WS、其他
4、日志切割
vim /opt/ fenge.sh #! /bin/bash # Filename: fenge. sh 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 ${logs_ path}/kgc. com- access.1og- $day #移动并重命名日志文件 kill -USR1 $(cat $pid_ path) #重建新日志文件 find $logs path -mtime +30 -exec rm -rf {} \; #删除30天之前的日志文件 #find $logs_ path -mtime +30 | xargs rm -rf chmod +x /opt/ fenge.sh /opt/ fenge .sh ls /var/log/nginx ls /usr/local/nginx/1ogs/access. log crontab -e 0 1 * * * /opt/fenge .sh
补充:
5、连接超时
http { ..... keepalive_ timeout 65 180; client_ header_ timeout 80; client_ body_ timeout 80; ...... } systemctl restart nginx
补充:
6、更改进程数
cat /proc/cpuinfo | grep -C "physical id" #查看cpu核数 ps aux| grep nginx #查看nginx主进程中包含几个子进程 vim /usr/local/nginx/conf/nginx. conf worker_processes 2; #修改为核数相同或者2倍. worker_ cpu_ affinity 01 10; #设置每个进程由不同cpu处理,进程数配为4时00010010 0100 1000 systemctl restart nginx
7、配置网页压缩
vim /usr/local/nginx/conf/nginx.conf http { ....... 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_vary on; #支持前端缓存服务器存储压缩页面 gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json; #压缩类型,表示哪些网页文档启用压缩功能 ...... }
在Linux系统中,打开火狐浏览器,右击点查看元素
选择网络——>选择HTML、WS、其他
vim /usr/local/nginx/conf/nginx . conf http { ...... server { ...... location ~* \. (jpg|gif|swf)$ { valid_ referers none blocked *.kgc.com kgc.com; if ( $invalid_referer ) { rewrite ^/ http://www. kgc. com/error.png; #return 403; } } ..... } }
cd /usr/local/nginx/html #将game.jpg、error.png文件传到/usr/local/nginx/html目录下 vim index.html <img src="1.jpg"/> </body> </html> echo "192.168.153.20 www.kgc.com" >> /etc/hosts
盗链网站主机(192.168.80.111):
cd /var/www/html vim index.html ...... <img src="http://www.kgc.com/game.jpg"/> </body> </html> echo "192.168.153.20 www.kgc.com" >> /etc/hosts echo "192.168.153.30 www.clj.com" >> /etc/hosts
在盗图网站主机进行浏览器验证 http://www.clj.com